[conn-dev wk8] HTTP…? HTML…?

I don’t think I have a good enough understanding of Wifi / HTTP yet, so I’m really struggling this week. Here are some stuffs I did before I got stuck.

Setting up a localhost server with Node.js and Express:

Arduino WiFiNINA + HttpClient libraries, GET request

This took me a while mostly because my Arduino NANO 33 couldn’t get online. After moving around and registering it to the NYU network, Jezzy pointed out that THE ANTENNA WAS MISSING?!?! I soldered it back on… and it works.

In this application, the Arduino code makes an HTTP client from my hosted Node.js server on Glitch, then a GET request. The respond is printed out, and presented in the console with the html code (the page on the route).

Testing Sandbox370 signal strength with LCD display. This WiFi signal in general meander between -50 to -80 dBm, but once I put the Arduino still it tends to settle on a smaller, presumably more precise range. From where I’ve been, lowest RSSI of seems to be around middle of the floor shop/admin area, where it’s nearly -80 dBm, while highest reading was, surprisingly, outside of the main door. Next to Aiden’s clock it almost consistently reads below -60 dBm. I have yet to find where the router is.

I was attempting to use data from a DHT temperature sensor, send it over to the server via HTTP protocol in JSON (?). I referenced the Get JSON example code, but didn’t really understand what I was doing. The best I was getting was “400 Bad Request”, which means client error.

Here is incomplete and possibly incorrect Arduino code:

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include <Arduino_JSON.h>

#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

// server address:
const char server[] = "viola-trying-node.glitch.me";
String path = "/data/";
int port = 80;

WiFiClient netSocket;
HttpClient client = HttpClient(netSocket, server, port);


long lastRequest = 0;
int interval = 10000;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  dht.begin();
  while ( WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);     // print the network name (SSID);

    WiFi.begin(ssid, pass);
    delay(2000);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  if (millis() - lastRequest > interval ) {
    String path = "/data/";

    // send the GET request
    Serial.println("making GET request");
    client.get(path);

    float h = dht.readHumidity();
    float t = dht.readTemperature();
  
    Serial.print(h);
    Serial.print(", ");
    Serial.println(t);

    // read the status code and body of the response
    int statusCode = client.responseStatusCode();
    String response = client.responseBody();
    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);

    // parse the string into a JSONVar object:
    JSONVar myObject = JSON.parse(response);

    // if you know the object's keys, you can ask for their values:
    Serial.print("temperature:\t");
    Serial.println(myObject["temperature"]);
    Serial.print("humidity:\t");
    Serial.println(myObject["humidity"]);

    // myObject.keys() can be used to get an array
    // of all the keys in the object
    JSONVar keys = myObject.keys();
    
    // read over the object, one key at a time:
    for (int i = 0; i < keys.length(); i++) {
      JSONVar value = myObject[keys[i]];
      // what's the key:
      Serial.print(keys[i]);
      Serial.print("\t");
      // and what's the value of the key:
      Serial.print(value);
      // and what's the type of the value:
      Serial.print("\ttype: ");
      Serial.println(JSON.typeof(value));
    }
    lastRequest = millis();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *