Skip to main content

Arduino code for ESP8266 to take readings from DHT11 and upload them to InitialState.com

www.initialstate.com is a great data broker and visualization platform for IoT devices. Up until now it was out of reach for ESP8266 fans because the Arduino environment for ESP8266 did not support SSL/TLS connections. But now it does. So here is how to get HTTPS working on ESP8266 using Arduino IDE and upload readings from DHT11 every 10 seconds.

ESP8266 and DHT11
 Steps for Windows computers:

  1. Install CP2102 drivers for the USB-UART chip onboard the ESP8266 NodeMcu v1.0 board
  2. Get the latest offline Arduino IDE Windows Installer from https://www.arduino.cc/en/Main/Software
  3. Install Board support for ESP8266 in your Arduino IDE, follow instructions here:
    https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon
  4. Assemble your ESP8266 and DHT11 circuit. You will need 3 jumper cables and a breadboard to connect 3 signals from ESP8266 to DHT11 module. Connect pin marked D2 on ESP8266 to DHT11's data pin. Also supply 3.3V and GND from ESP8266 to DHT11's Vcc and GND.
  5. Plug your ESP8266 into your computer's USB port using a microUSB cable.
  6. Get a free account on www.initialstate.com, create a new bucket and make a note of the Bucket key and Access key.
  7. Download shown below and open it in your Arduino IDE
  8. Modify 4 things by supplying your own:
    1. WLAN_SSID
    2. WLAN_PASS
    3. INITIALSTATE_ACCESS_KEY
    4. INITIALSTATE_BUCKET_KEY
  9. Download the code into your ESP8266 board and watch it go! Keep the Serial Monitor within Arduino IDE open to watch what data is being sent to www.initialstate.com
Data received on initialstate.com
Watch the code go on Serial Monitor
InitialStateDHT11.ino:
#include <ESP8266WiFi.h>
#include "DHT.h"
////////////////////DHT11
//Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11 //DHT11
//#define DHTTYPE DHT21 //DHT21 (AM2301)
//#define DHTTYPE DHT22 //DHT22 (AM2302), AM2321
const int DHTPin = 4;
DHT dht(DHTPin, DHTTYPE);
float humidity, temperature;
////////////////////DHT11
////////////////////WiFi Router
#define WLAN_SSID "Lithiumhead24"
#define WLAN_PASS "mypassword"
////////////////////WiFi Router
////////////////////SSL and Initial State
#define INITIALSTATE_ACCESS_KEY "v4n5fg435h4u3h56jkh6DFEWTRF5rEWRfr344"
#define INITIALSTATE_BUCKET_KEY "SDGDGFGDFSGDFG"
#define INITIALSTATE_API "groker.initialstate.com"
#define INITIALSTATE_PORT 443
#define INITIALSTATE_ENDPOINT "/api/events"
WiFiClientSecure client;
//groker.initialstate.com SHA1 fingerprint
const char* fingerprint = "D4 2A 25 CC 50 60 64 2C D9 D1 DD 4B 26 0B 0E DE 57 FC 12 05";
const char* host = INITIALSTATE_API;
////////////////////SSL and Initial State
////////////////////JSON & HTTPS
String jsonBuf = "";
String httpRequestContent = "";
String httpRequestContent_Part1 = "";
////////////////////JSON & HTTPS
void setup() {
//Preform the HTTP header
httpRequestContent_Part1 += "POST ";
httpRequestContent_Part1 += INITIALSTATE_ENDPOINT;
httpRequestContent_Part1 += " HTTP/1.0\r\n";
httpRequestContent_Part1 += "User-Agent: esp8266/2.3.0 \r\n";
httpRequestContent_Part1 += "Host: ";
httpRequestContent_Part1 += INITIALSTATE_API;
httpRequestContent_Part1 += "\r\n";
httpRequestContent_Part1 += "Accept: */*\r\n";
httpRequestContent_Part1 += "Content-Type: application/json\r\n";
httpRequestContent_Part1 += "X-IS-AccessKey: ";
httpRequestContent_Part1 += INITIALSTATE_ACCESS_KEY;
httpRequestContent_Part1 += "\r\n";
httpRequestContent_Part1 += "X-IS-BucketKey: ";
httpRequestContent_Part1 += INITIALSTATE_BUCKET_KEY;
httpRequestContent_Part1 += "\r\n";
httpRequestContent_Part1 += "Accept-Version: 0.0.4\r\n";
httpRequestContent_Part1 += "Content-Length: ";
//Initialize Serial
Serial.begin(115200);
delay(10);
//Initialize DHT11
dht.begin();
//Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.print(WLAN_SSID);
Serial.print("...");
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(5000);
Serial.println();
Serial.println("WiFi LAN IP address: ");
Serial.println(WiFi.localIP());
}
long loop_counter = 0;
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
loop_counter++;
//Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT11 sensor!");
Serial.print("_________________________________________Iteration #");
Serial.print(loop_counter);
Serial.println(" Ended");
delay(10000);
} else { //DHT11 read successfully!
Serial.print("Connecting to ");
Serial.print(INITIALSTATE_API);
Serial.print(":");
Serial.print(INITIALSTATE_PORT);
Serial.print("...");
if (! client.connect(host, INITIALSTATE_PORT)) {
Serial.println("Connection failed. Will retry...");
Serial.print("_________________________________________Iteration #");
Serial.print(loop_counter);
Serial.println(" Ended.");
client.stop();
}
if (client.verify(fingerprint, host)) {
Serial.println("Connection secure. Uploading data using HTTPS...");
jsonBuf = "";
jsonBuf += "[";
jsonBuf += "{";
jsonBuf += " \"key\": \"Temperature\",";
jsonBuf += " \"value\": \"";
jsonBuf += String(temperature);
jsonBuf += "\"";
jsonBuf += " },";
jsonBuf += "{";
jsonBuf += " \"key\": \"Humidity\",";
jsonBuf += " \"value\": \"";
jsonBuf += String(humidity);
jsonBuf += "\"";
jsonBuf += " }";
jsonBuf += "]";
httpRequestContent = "";
httpRequestContent += httpRequestContent_Part1;
httpRequestContent += String(jsonBuf.length());
httpRequestContent += "\r\n\r\n";
httpRequestContent += jsonBuf;
Serial.println(httpRequestContent);
client.print(httpRequestContent);
Serial.println("Done uploading, disconnecting...");
Serial.print("_________________________________________Iteration #");
Serial.print(loop_counter);
Serial.println(" Ended");
client.stop();
} else {
Serial.println("Connection insecure, disconnecting, will retry...");
Serial.print("_________________________________________Iteration #");
Serial.print(loop_counter);
Serial.println(" Ended");
client.stop();
}
delay(10000);
}
}

Comments

  1. Please check in code in Github also.
    Very much appreciated your work.

    ReplyDelete
  2. i tried to simplify this but when i run it, on the Serial Monitor it goes that part where it says the connection is insecure.

    "Connection insecure, disconnecting, will retry..."

    I think it has something to do on Line 120 where it checks if the line is Secure. The parameter in there is the SHA 1 Fingerprint.

    May i ask how did you get that SHA1 fingerprint ? is it still updated?

    ReplyDelete

Post a Comment