Published
- 2 min read
- 582
Esp32 to ThingsBoard

π€ Introduction
We are going to connect an ESP32 to a specific Wi-Fi network and it will send temperature and humidity data, obtained through a DHT22 sensor, to a ThingsBoard server via MQTT, using an access token for authentication. The device will be responsible for keeping the Wi-Fi connection active, continuously collecting sensor data, and sending it to the server. This entire process will be carried out in a constant execution loop. Additionally, for this development, we will be using an ESP32 emulation on the Wokwi platform.
βοΈ Tech Stack
- π Esp32
- π Arduino Framework
- π Thingsboard
- π MQTT Protocol
- π ArduinoJson Library
π Features
- πWi-Fi Connection: The device connects to a specific Wi-Fi network using the provided credentials.
- π Temperature and Humidity Sensor: It utilizes a DHT22 sensor to gather temperature and humidity data from the environment.
- π MQTT Communication: Sends the collected data to the ThingsBoard server via the MQTT protocol, using an access token for authentication.
- π Connection Management: It ensures to keep the Wi-Fi connection active and automatically reconnects if the connection is lost.
- π Constant Execution Loop: The device continuously collects sensor data and sends it to the server in a constant execution loop.
- π Wokwi Emulation: The code is developed and tested using an ESP32 emulation on the Wokwi platform.
π¬ Youtube tutorial
See tutorial videoΒ here
β¨οΈ Code
You can access the code here:
Alternatively, Iβm also leaving it here:
#include < DHTesp.h >
#include < WiFi.h >
#include < ThingsBoard.h >
#include < Arduino_MQTT_Client.h >
#define pinDht 15
DHTesp dhtSensor;
#define WIFI_AP "Wokwi-GUEST"
#define WIFI_PASS ""
#define TB_SERVER "thingsboard.cloud"
#define TOKEN "O7HuziT3C7PLIs1PQ8OX"
constexpr uint16_t MAX_MESSAGE_SIZE = 128U;
WiFiClient espClient;
Arduino_MQTT_Client mqttClient(espClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
WiFi.begin(WIFI_AP, WIFI_PASS, 6);
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nFailed to connect to WiFi.");
} else {
Serial.println("\nConnected to WiFi");
}
}
void connectToThingsBoard() {
if (!tb.connected()) {
Serial.println("Connecting to ThingsBoard server");
if (!tb.connect(TB_SERVER, TOKEN)) {
Serial.println("Failed to connect to ThingsBoard");
} else {
Serial.println("Connected to ThingsBoard");
}
}
}
void sendDataToThingsBoard(float temp, int hum) {
String jsonData = "{\"temperature\":" + String(temp) + ", \"humidity\":" + String(hum) + "}";
tb.sendTelemetryJson(jsonData.c_str());
Serial.println("Data sent");
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(pinDht, DHTesp:: DHT22);
connectToWiFi();
connectToThingsBoard();
}
void loop() {
connectToWiFi();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temp = data.temperature;
int hum = data.humidity;
Serial.println(temp);
Serial.println(hum);
if (!tb.connected()) {
connectToThingsBoard();
}
sendDataToThingsBoard(temp, hum);
delay(3000);
tb.loop();
}
π Deploy
See online here