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.
You will need these in the firmware.
#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;
}
}
Install the Adafruit SHT31 Library: https://github.com/adafruit/Adafruit_SHT31
ssid — your Wi-Fi network namepassword — Wi-Fi passwordVizIoT_Device_key — device keyVizIoT_Device_pass — device passwordConnect using I²C:
| Sensor Pin | ESP8266 Pin |
|---|---|
| VIN | 3V3 |
| GND | G |
| SCL | D1 |
| SDA | D2 |
After connecting, upload the sketch to your ESP8266.
Create a dashboard: "Test Temp Humidity Panel"
Add widgets:
The device is fully configured. Now, just wait for the ESP8266 to connect to Wi-Fi and start sending data to the VizIoT server.