Using VarVault with NodeMCU

With NodeMCU, you can read data from sensors such as temperature, humidity, light, motion, and many others. HTTPs request can be quite a challenge in developing since it is an HTTP with added security features provided by encryption protocols and there is a need for CA certificate.

DS18b20 (temperature sensor) connected to NodeMCU

WifiClientSecure is an Arduino Library that supports the HTTPs request and can be uploaded to the NodeMCU. Using the WifiClientSecure, you can specify the CA certificates.

Source Code Link: WiFiClientSecure.ino

Steps on putting the right CA certificate

  1. Access this website's SSL certificate details from your browser. This is done by clicking on the padlock icon next to the website's URL in the browser's address bar and then navigating to the certificate details.
  2. Once you're viewing the SSL certificate details, look for the option to export the certificate. This might vary depending on your browser, but there should be an option to export the certificate in a format like PEM or DER.
  3. Export the CA certificate from the SSL certificate chain. This is often labeled as the "root" or "intermediate" certificate. Make sure to export it in PEM format, as it's easier to work with.
  4. Open the exported CA certificate file with a text editor and copy its contents.
  5. Replace the contents of the test_root_ca variable in your code with the contents of the CA certificate you obtained. Make sure to paste it between the "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" lines.

Once you've done this, your NodeMCU/ESP8266 should be able to establish a secure connection with your website using HTTPS.

POST Request using WifiClientSecure Library

This code snippet constructs a POST request to send sensor data, specifically temperature readings, to a server. It initializes the temperature reading to 37.5 degrees Celsius, then formats this reading along with authentication token and variable name into a URL-encoded string. The string is then included in the body of the HTTP request, along with necessary headers such as Content-Type and Content-Length, and sent to the designated server endpoint using the HTTP/1.1 protocol.

c++ content_copy Copy

                
//Sensor Reading:
Temperature_Reading = 37.5; //Sample temperature reading

// Construct the POST request
String token = "500fbhajgccfei";
String variable_name = "Temperature";
String value = String(Temperature_Reading, 2);
String postData = "variable_name=" + urlEncode(variable_name) + "&" +
                  "value=" + urlEncode(value);

// POST Request
client.println("POST " + endpoint + " HTTP/1.1");
client.println("Host: " + server);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Authorization: Bearer ");
client.println(token);
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.println(postData);
POST request to this API's endpoint `/update-variable`