Connecting ESP32 to ThingsBoard Cloud: A Complete Beginner's Guide
Introduction
click on this link : https://thingsboard.io?fpr= pigirltech
The Internet of Things (IoT) allows devices to collect and exchange data over the internet. One of the most popular open-source IoT platforms is ThingsBoard, which enables you to monitor, visualize, and manage connected devices through customizable dashboards.
In this tutorial, you'll learn how to:
Connect an ESP32 to the ThingsBoard Cloud
Create a device in ThingsBoard
Get the device access token
Upload Arduino code to the ESP32
Send random temperature data to the cloud
Create a dashboard to visualize live data
By the end of this guide, your ESP32 will continuously send temperature values to ThingsBoard, where you can monitor them in real time.
Prerequisites
Before getting started, make sure you have:
ESP32 development board
USB cable
Arduino IDE installed
Wi-Fi connection
ThingsBoard Cloud account
Basic knowledge of Arduino programming
Step 1: Create a ThingsBoard Cloud Account
Open your web browser.
Visit https://thingsboard.cloud.
Sign up for a free account.
Verify your email and log in.
After logging in, you'll see the ThingsBoard dashboard.
Step 2: Create a New Device
From the left menu, select Devices.
Click + Add Device.
Enter a device name (for example, ESP32 Temperature Sensor).
Click Add.
Your ESP32 device is now created.
Step 3: Get the Device Access Token
Each ESP32 authenticates using a unique access token.
Open your newly created device.
Go to the Credentials tab.
Copy the Access Token.
Keep this token safe because it will be used in the Arduino code.
Example:
Access Token:
AbCdEf123456789XYZ
Step 4: Install Required Arduino Libraries
Open the Arduino IDE and install:
WiFi
ArduinoJson
PubSubClient
To install:
Open Sketch → Include Library → Manage Libraries
Search and install the required libraries.
Step 5: ESP32 Arduino Code
Update the following values:
Wi-Fi SSID
Wi-Fi Password
ThingsBoard Access Token
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqttServer = "thingsboard.cloud";
const int mqttPort = 1883;
const char* token = "YOUR_ACCESS_TOKEN";
WiFiClient espClient;
PubSubClient client(espClient);
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void reconnect() {
while (!client.connected()) {
client.connect("ESP32", token, NULL);
}
}
void setup() {
Serial.begin(115200);
connectWiFi();
client.setServer(mqttServer, mqttPort);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float temperature = random(200, 350) / 10.0;
String payload = "{\"temperature\":";
payload += temperature;
payload += "}";
client.publish("v1/devices/me/telemetry", payload.c_str());
Serial.println(payload);
delay(5000);
}
Step 6: Upload the Code
Connect the ESP32 to your computer.
Select the correct board:
Tools → Board → ESP32 Dev Module
Select the correct COM port.
Click Upload.
Open the Serial Monitor to verify the device is sending data.
Example output:
{"temperature":26.4}
{"temperature":28.1}
{"temperature":24.7}
Step 7: Verify Incoming Data
Return to the ThingsBoard portal.
Open your device.
Select Latest Telemetry.
You should see:
temperature
26.4
temperature
28.1
temperature
24.7
This confirms that the ESP32 is successfully sending data.
Step 8: Create a Dashboard
To visualize the data:
Open Dashboards.
Click Create Dashboard.
Enter a dashboard name.
Open the dashboard.
Click Edit Dashboard.
Click Add Widget.
Step 9: Add a Temperature Widget
Choose any widget such as:
Gauge
Digital Card
Line Chart
Timeseries Chart
Configure the widget:
Entity: Your ESP32 Device
Data Key:
temperature
Save the widget.
Step 10: View Live Temperature Data
Once the widget is added, your dashboard will display live temperature updates every few seconds.
The gauge or chart will automatically refresh whenever the ESP32 sends new telemetry.
How It Works
The complete workflow is:
ESP32 connects to Wi-Fi.
ESP32 authenticates with ThingsBoard using the Access Token.
The ESP32 generates random temperature values.
Telemetry is published using MQTT.
ThingsBoard stores the data.
The dashboard displays the temperature in real time.
Expected Output
After completing all the steps, you will have:
An ESP32 connected to ThingsBoard Cloud
A registered device with a unique access token
Live telemetry data uploaded every 5 seconds
A dashboard showing random temperature values using widgets
Conclusion
Connecting an ESP32 to ThingsBoard Cloud is an excellent way to build IoT applications. Using MQTT and the Arduino IDE, the ESP32 can securely send sensor data to the cloud. ThingsBoard makes it easy to create interactive dashboards that display real-time telemetry without requiring complex backend development.
You can extend this project by connecting real sensors such as the DHT11, DHT22, BMP280, or DS18B20 to monitor actual environmental conditions. You can also add alarms, notifications, and remote device control using ThingsBoard's powerful features.

Post a Comment