[conn-dev wk 9] HTTPS POSTed some weather data

Continuing with last week’s failed attempt to send temperature / humidity data to Glitch, I’m glad we’re allowed another week to connect a microcontroller client to a server. Before I started, I took some time to find and read documentation from Node.js and Express.js — a big improvement for my usually very impatient self. When creating a server it’s essential to callback a request and a response function, but it seems like even without a request, the server can still return a response to the client.

I studied my Arduino code from last week, and it didn’t seem right that I was using GET — I feel like I’m only supposed to use GET to, like, get messages from a client. I also read somewhere about query string, and felt strongly about setting up one as my route / path, but didn’t know how to proceed after the fact.

String queryString = String("?temperature=") + String(t) + String("&humidity=") + String(h);

After discussing with my classmate Yonny, I was very impressed by how he used GET on Arduino. However, I still wanted to look into POST as a potential method, despite feeling a bit clueless. A very kind Jason Tse, who happened to have taken this class AND was on the floor this fine Monday night, helped me dig out an HttpSSLClient example code from Tom’s GitHub. Combining that with the simple post example code, I was hoping to make some progress. It seems like this new code only differentiate from the GET method in a few lines:

1) Has to be a WiFiSSLClient

2) Has to use port 443 (? why)

3) Apart from defining the route, it also requires content type, and of course the content itself

…Which leads to a very big challenge: writing content in its correct data format. Assuming the server side would want to receive JSON, sending JSON within a String seems impossible, especially since Arduino doesn’t recognize JSON. {"name": "Alice"} was very obviously wrong.

(in this sketch, line 53 was also wrong. “/” should be “/data”, or whatever destination called in Server.js)

With the help of Jason, we figured out the correct way to send JSON from Arduino:

String contentType = "application/json";
String postData = "{\"name\": \"Alice\"}";

Double quote inside of double quote is illegal!!! A back dash is required before each quotation mark to …separate it I guess. Now, the console returns proper JSON: { name: ‘Alice’ }.

Applying this new knowledge to fetch temperature and humidity data from the DHT sensor, I successfully sent a JSON file with two objects to the Glitch server. Meanwhile, with a response.send("ok!"); in JavaScript, Arduino receives an “ok!” back.

The simplest code for POST on the server side:

server.post("/data", postData);

function postData(request, response) {
  if (request.body.temperature) {
    // update the temperature value
    myData.temperature = request.body.temperature;
  if (request.body.humidity) {
    // update the humidity value
    myData.humidity = request.body.humidity;

Final result: Arduino makes a POST request every 10 seconds, and sensor values update. When button is clicked, website update with current time and latest readings.

Leave a Reply

Your email address will not be published.