Air Temperature and Humidity with ESP8266 and SHT31-D

In this example, we will create a device that measures air temperature and humidity, transmits the data to the VizIoT server, tracks value dynamics, and builds graphs. The device will also send the Wi-Fi signal strength (RSSI) every 20 seconds.


What You'll Need


Adding and Configuring the Device in VizIoT

  1. Create a new device, for example: "Test Air Temperature and Humidity"
  2. Add parameters:
    • rssiSignal Strength, dBm
    • temTemperature, °C
    • humHumidity, %
  3. In the "General Settings" section, copy:
    • Access Key
    • Access Password

You will need these in the firmware.


ESP8266 Firmware

1. Create a New Project in Arduino IDE

2. Copy the Code:

#include <ESP8266WiFi.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

HTTPClient http;
Adafruit_SHT31 sht31 = Adafruit_SHT31();

// Wi-Fi credentials
const char* ssid     = "........";
const char* password = "........";

// VizIoT Server
const char* http_server = "VizIoT.com";
const int   http_port   = 48656;

// VizIoT Access Keys
String VizIoT_Device_key  = "................";
String VizIoT_Device_pass = "....................";

void setup() {
  Serial.begin(115200);
  setup_wifi();

  Serial.println("Searching for SHT31-D sensor");
  if (!sht31.begin(0x44)) {
    Serial.println("Can't find SHT31-D");
  }
}

void loop() {

  String url = String("/update?key=") + VizIoT_Device_key +
               "&pass=" + VizIoT_Device_pass;

  // Temperature and Humidity
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (!isnan(t)) {
    Serial.print("Temperature °C = ");
    Serial.println(t);
    url += String("&tem=") + t;
  } else {
    Serial.println("Failed to read temperature");
  }

  if (!isnan(h)) {
    Serial.print("Humidity % = ");
    Serial.println(h);
    url += String("&hum=") + h;
  } else {
    Serial.println("Failed to read humidity");
  }

  // RSSI
  long rssi = WiFi.RSSI();
  url += String("&rssi=") + rssi;

  // Send to server
  if (sendGetRequest(url) == true) {
    Serial.println("Data successfully sent to the server");
  } else {
    Serial.println("Error sending data");
  }

  delay(20000); // 20 seconds
}

void setup_wifi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");
}

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();
      return payload.equals("OK");
    } else {
      Serial.println("[HTTP] GET... failed");
      http.end();
      return false;
    }
  } else {
    return false;
  }
}

Library Installation

Install the Adafruit SHT31 Library: https://github.com/adafruit/Adafruit_SHT31


Enter Your Data in the Code

  • ssid — your Wi-Fi network name
  • password — Wi-Fi password
  • VizIoT_Device_key — device key
  • VizIoT_Device_pass — device password

Connecting the SHT31-D Sensor

Connect using I²C:

Sensor Pin ESP8266 Pin
VIN 3V3
GND G
SCL D1
SDA D2

After connecting, upload the sketch to your ESP8266.


Adding Widgets in VizIoT

Create a dashboard: "Test Temp Humidity Panel"

Add widgets:

1) Wi-Fi Signal Strength

  • type: Graph
  • parameter: rssi

2) Air Temperature

  • type: Graph
  • parameter: tem

3) Air Humidity

  • type: Graph
  • parameter: hum

4) Temperature and Humidity Changes

  • type: Changes Histogram
  • parameters: tem, hum

All Set!

The device is fully configured. Now, just wait for the ESP8266 to connect to Wi-Fi and start sending data to the VizIoT server.