Connecting ESP32 to ThingsBoard Cloud: A Complete Beginner's Guide

 

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

  1. Open your web browser.

  2. Visit https://thingsboard.cloud.

  3. Sign up for a free account.

  4. Verify your email and log in.

After logging in, you'll see the ThingsBoard dashboard.


Step 2: Create a New Device

  1. From the left menu, select Devices.

  2. Click + Add Device.

  3. Enter a device name (for example, ESP32 Temperature Sensor).

  4. Click Add.

Your ESP32 device is now created.


Step 3: Get the Device Access Token

Each ESP32 authenticates using a unique access token.

  1. Open your newly created device.

  2. Go to the Credentials tab.

  3. 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:

  1. Open Sketch → Include Library → Manage Libraries

  2. 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

  1. Connect the ESP32 to your computer.

  2. Select the correct board:

    • Tools → Board → ESP32 Dev Module

  3. Select the correct COM port.

  4. 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.

  1. Open your device.

  2. 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:

  1. Open Dashboards.

  2. Click Create Dashboard.

  3. Enter a dashboard name.

  4. Open the dashboard.

  5. Click Edit Dashboard.

  6. 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:

  1. ESP32 connects to Wi-Fi.

  2. ESP32 authenticates with ThingsBoard using the Access Token.

  3. The ESP32 generates random temperature values.

  4. Telemetry is published using MQTT.

  5. ThingsBoard stores the data.

  6. 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

My Instagram

Made with by Pi-girl | Copyright © 2020 Pi-girl