Help regarding ThingsBoard and ESP8266 With Raspbery pi

Hello everyone,

I'm working on a project involving the ESP8266 Arduino Mega Wi-Fi board from Wemos, coupled with ThingsBoard for data management. My setup employs a Raspberry Pi Model 4 to serve as the ThingsBoard server. Initially, I successfully utilized a code to send temperature data from the ESP8266/Wemos Mega Wi-Fi board to the Raspberry Pi, which effectively received the data.


This board combines an Arduino Mega and an ESP8266 microcontroller. In the beginning, I established a ThingsBoard server on my Raspberry Pi, following a guide that I've linked here. This guide assisted me in configuring the Raspberry Pi 4 as a dedicated ThingsBoard server. Subsequently, I embarked in a different direction to enable the DHT11 sensor data to be displayed on a ThingsBoard dashboard. This process rendered the device active and operational.

Unfortunately, I misplaced the initial code, leading me to follow another guide to control the GPIO pins of the ESP8266. I successfully created a comprehensive dashboard, utilizing the provided JSON files and modifying them in line with instructions from the guide (linked here).

https://thingsboard.io/docs/samples/esp8266/gpio/

I adjusted the JSON files, imported the dashboard into ThingsBoard, and adapted the provided code by incorporating the necessary keys. However, upon uploading and executing the code, I encountered an error message indicating a failure to connect the NodeMCU to ThingsBoard.

This is the Example code

#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

#define WIFI_AP "YOUR_WIFI_AP"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

#define TOKEN "ESP8266_DEMO_TOKEN"

#define GPIO0 0
#define GPIO2 2

#define GPIO0_PIN 3
#define GPIO2_PIN 5

char thingsboardServer[] = "demo.thingsboard.io";

WiFiClient wifiClient;

PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;

// We assume that all GPIOs are LOW
boolean gpioState[] = {false, false};

void setup() {
  Serial.begin(115200);
  // Set output mode for all GPIO pins
  pinMode(GPIO0, OUTPUT);
  pinMode(GPIO2, OUTPUT);
  delay(10);
  InitWiFi();
  client.setServer( thingsboardServer, 1883 );
  client.setCallback(on_message);
}

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

  client.loop();
}

// The callback for when a PUBLISH message is received from the server.
void on_message(const char* topic, byte* payload, unsigned int length) {

  Serial.println("On message");

  char json[length + 1];
  strncpy (json, (char*)payload, length);
  json[length] = '\0';

  Serial.print("Topic: ");
  Serial.println(topic);
  Serial.print("Message: ");
  Serial.println(json);

  // Decode JSON request
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data = jsonBuffer.parseObject((char*)json);

  if (!data.success())
  {
    Serial.println("parseObject() failed");
    return;
  }

  // Check request method
  String methodName = String((const char*)data["method"]);

  if (methodName.equals("getGpioStatus")) {
    // Reply with GPIO status
    String responseTopic = String(topic);
    responseTopic.replace("request", "response");
    client.publish(responseTopic.c_str(), get_gpio_status().c_str());
  } else if (methodName.equals("setGpioStatus")) {
    // Update GPIO status and reply
    set_gpio_status(data["params"]["pin"], data["params"]["enabled"]);
    String responseTopic = String(topic);
    responseTopic.replace("request", "response");
    client.publish(responseTopic.c_str(), get_gpio_status().c_str());
    client.publish("v1/devices/me/attributes", get_gpio_status().c_str());
  }
}

String get_gpio_status() {
  // Prepare gpios JSON payload string
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data = jsonBuffer.createObject();
  data[String(GPIO0_PIN)] = gpioState[0] ? true : false;
  data[String(GPIO2_PIN)] = gpioState[1] ? true : false;
  char payload[256];
  data.printTo(payload, sizeof(payload));
  String strPayload = String(payload);
  Serial.print("Get gpio status: ");
  Serial.println(strPayload);
  return strPayload;
}

void set_gpio_status(int pin, boolean enabled) {
  if (pin == GPIO0_PIN) {
    // Output GPIOs state
    digitalWrite(GPIO0, enabled ? HIGH : LOW);
    // Update GPIOs state
    gpioState[0] = enabled;
  } else if (pin == GPIO2_PIN) {
    // Output GPIOs state
    digitalWrite(GPIO2, enabled ? HIGH : LOW);
    // Update GPIOs state
    gpioState[1] = enabled;
  }
}

void InitWiFi() {
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

  WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    status = WiFi.status();
    if ( status != WL_CONNECTED) {
      WiFi.begin(WIFI_AP, WIFI_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 Device", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
      // Subscribing to receive RPC requests
      client.subscribe("v1/devices/me/rpc/request/+");
      // Sending current GPIO status
      Serial.println("Sending current GPIO status ...");
      client.publish("v1/devices/me/attributes", get_gpio_status().c_str());
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.print( client.state() );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}

This is the error I am presented with

I'm reaching out to the community in hopes of receiving assistance regarding this issue. Specifically, I'm seeking guidance on the process of GPIO pin control through ThingsBoard using a Raspberry Pi as the dedicated server. Additionally, I'm curious about the comparative advantages of using a Raspberry Pi as the server versus utilizing the ThingsBoard demo server. It would be great if someone helped me set this up.....
My goal is to make a dashboard for an electric vehicle which can be accessed from anywhere so I am starting with GPIO Pin Controlling and finding my way through...
I am ready to start the code and dashboards from the basics.

Your insights and support would be greatly appreciated. Thank you in advance for your assistance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.