ESP8266 HTTP -> Air temperature and humidity

Consider a specific example, which will make it possible to monitor air temperature and humidity, keep track of variations in metering values per hour and plot a graph of Wi-Fi signal strength with the data refreshing rate of 20 seconds.

Required materials:

  • ESP8266 Wemos d1 mini microcontroller
  • SHT31-D sensor
  • Arduino IDE development framework (you can look for settings here)

Device adding and setting:

  1. Add a new device under the name of «Air temperature and humidity testing» in VizIoT
  2. Configure device settings. Add three parameters:
    1. rssi key of «Signal strength, dBm» type;
    2. tem key of «Temperature, °C» type;
    3. hum key of «Humidity, %» type.
  3. Copy pass key and access password in «Basic settings» of the device.

Firmware ESP8266:

  1. Create a new project in Arduino IDE.
  2. Copy the example below to the created project.
    #include <ESP8266WiFi.h>
    #include <Wire.h>
    #include "Adafruit_SHT31.h"
    #include <WiFiClient.h>
    #include <ESP8266HTTPClient.h>
    
    HTTPClient http;
    
    Adafruit_SHT31 sht31 = Adafruit_SHT31();
    
    //ssid and access password for connection to WI-FI
    const char* ssid = "........";
    const char* password = "........";
    
    //server address and port
    const char* http_server = "VizIoT.com";
    const int http_port = 48656;
    
    //VizIoT access key and password Devices (can be found in device settings)
    String VizIoT_Device_key = "................";
    String VizIoT_Device_pass = "....................";
    
    void setup() {
      //Turn on information output in Serial Monitor
      Serial.begin(115200);
    
      //Connect to WI-FI
      setup_wifi();
    
      Serial.println("Seeking for SHT31-D sensor");
      if (! sht31.begin(0x44)) {   // put 0x45 for the alternative i2c sensor address
        Serial.println("SHT31-D cannot be found");
      }
    }
    
    
    void loop() {
    
      String url = String("/update?key=") + VizIoT_Device_key + "&" + "pass=" + VizIoT_Device_pass ;
    
      //read sensor values
      float t = sht31.readTemperature();
      float h = sht31.readHumidity();
    
      //check information on temperature
      if (! isnan(t)) {
        Serial.print("Temperature *C = ");
        Serial.println(t);
        url += String("&tem=") + t;
      } else {
        Serial.println("Temperature reading error");
      }
    
      //check information on air humidity
      if (! isnan(h)) {
        Serial.print("Hum. % = ");
        Serial.println(h);
        url += String("&hum=") + h;
      } else {
        Serial.println("Humidity reading error");
      }
    
      if (sendGetRequest(url) == true) {
        Serial.println("Data has been sent to the server");
      } else {
        Serial.println("Error while sending data");
      }
    
      //wait 20 seconds
      delay(20000);
    }
    
    //Wi-Fi connection function
    void setup_wifi() {
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        //To be connected to WI-FI
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
    }
    
    //Function sending data to the server 
    int sendGetRequest(String url) {
      if (WiFi.status() == WL_CONNECTED) {
        http.begin(http_server, http_port, url);
        int httpCode = http.GET();
        if (httpCode > 0) {
          String payload = http.getString();
          http.end();
          if (payload.equals("OK")) {
            return true;
          } else {
            return false;
          }
        } else {
          Serial.println("[HTTP] GET... failed, error!!!\r\n");
          Serial.print("httpCode = ");
          Serial.println(httpCode);
          http.end();
          return false;
        }
      } else {
        return false;
      }
    }
    
  3. Install the library «Adafruit SHT31 Library» for working with SHT31-D sensor.
  4. Set values of the variables:
    1. ssid – your Wi-Fi point name
    2. password – your Wi-Fi point password
    3. VizIoT_Device_key – copied device access key
    4. VizIoT_Device_pass – copied device access password
  5. Connect SHT31-D sensor to ESP8266 according to the connection pattern:
    1. VIN pin of sensor to esp 3V3 pin
    2. GND pin of sensor to esp G pin
    3. SCL pin of sensor to esp D1 pin
    4. SDA pin of sensor to esp D2 pin
  6. Connect ESP8266 to computer and send firmware to microcontroller

Adding widgets:

  1. Create a new dashboard under the name «Air temperature and humidity testing».
  2. Add widgets:
    1. Signal strength
      • type: «Chart»;
      • device: «Air temperature and humidity testing»;
      • parameter: rssi;
    2. Air temperature
      • type: «Chart»;
      • device: «Air temperature and humidity testing»;
      • parameter: tem;
    3. Air humidity
      • type: «Chart»;
      • device: «Air temperature and humidity testing»;
      • parameter: hum;
    4. Temperature and humidity fluctuations
      • type: «Change over an interval bar chart»;
      • device: «Air temperature and humidity testing»;
      • parameter: tem and hum;

Now the setup is complete and you have to wait until your device connected to the server.