Sending data to thingsboard using ESP8266 or NodeMCU

On thingsboard , device is the place where your actual data comes. Thingsboard uses MQTT protocol to receive data  from users . Here we will see how to send data to thingsboard using NodeMCU or ESP8266. Assuming you have created a device on thingsboard. If you have not created a device on thingsboard, please click on the following link.
Creating a device on thingsboard

Send data to thingsboard using ESP8266 or NodeMCU

Steps for sending data to thingsboard using ESP8266 or NodeMCU
1)Here we will send data on Display device . If you don’t know how to create a device on thingsboard please click on above mentioned link.

2)When we create a device on thingsboard, access token gets assign to the created device. An access token is required when we send data. Access token helps users to identify a device. Users identify a particular device on thingsboard while sending data using access tokens of the device, not by their actual name. To see your device access token please click on Manage credentials.

3)Data comes to LATEST TELEMETRY field of Device. To see data, click anywhere on the device and then click on the LATEST TELEMETRY field of the device. If you see below, there is no data at the moment.

4)In order to send data to thingsboard using NodeMCU or ESP8266 , you need to install pubsubclient-master and ESP8266WiFi.h libraries . Please click on the following link for installing pubsubclient-master and ESP8266WiFi.h libraries.
ESP8266 as MQTT Publisher

5)Assuming you have successfully installed pubsubclient-master and ESP8266WiFi.h libraries in your Arduino IDE.

6)Download the following code to send data to thingsboard using ESP8266 or NodeMCU and please modify the following fields with respective of your device.
I)ssidReplace ssid with your WiFi name

II)passwordReplace password with your WiFi password.

III)TOKENReplace TOKEN with token of your device on which you want to send data.

IV)If you want to modify data , please modify function getAndSendTemperatureAndHumidityData()

download Thingsboard data post code using NodeMCU (1634 downloads)
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "joker";
const char* password = "joker143";

#define TOKEN "ad4YHXCdWS3RV6zVU7cw" //Access token of device Display
char ThingsboardHost[] = "demo.thingsboard.io";

WiFiClient wifiClient;
PubSubClient client(wifiClient);
int status = WL_IDLE_STATUS;

void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("connected to");
  Serial.println(ssid);
  client.setServer( ThingsboardHost, 1883 );
}

void loop()
{
if ( !client.connected() ) 
{
    reconnect();
}
getAndSendTemperatureAndHumidityData();
  delay(5000);
}

void getAndSendTemperatureAndHumidityData()
{
  
 
  // Prepare a JSON payload string
  String payload = "{";
 payload += "\"Humidity\":";payload += 60; payload += ",";
 payload += "\"Temperature\":";payload += 25; 
  payload += "}";

  char attributes[1000];
  payload.toCharArray( attributes, 1000 );
  client.publish( "v1/devices/me/telemetry",attributes);
  Serial.println( attributes );
   
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    status = WiFi.status();
    if ( status != WL_CONNECTED) {
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("Connected to AP");
    }
    Serial.print("Connecting to ThingsBoard node ...");
    // Attempt to connect (clientId, username, password)
    if ( client.connect("Esp8266", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.println( " : retrying in 5 seconds]" );
      delay( 500 );
    }
  }
}

7)Upload the above code in ESP8266 or NodeMCU.

8)Now check your device you should get data data in in LATEST TELEMETRY field of your device.

You may also like...

Leave a Reply