0

I am using MQTT to communicate between ESP8266 and Raspberry Pi and I am sending multiple strings from ESP8266 to Raspberry Pi. So i want to know that is there any functions or something that can detect error while sending data(String) to Raspberry Pi (if there occurred an error). And if so then how can i handle that error ?

This is my code in NodeMCU

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

SoftwareSerial NodeMCU(D2,D3);  

const char* ssid = "raspi-webgui";           // wifi ssid
const char* password =  "ChangeMe";         // wifi password
const char* mqttServer2= "192.168.43.164";  // IP adress Raspberry Pi
const int mqttPort = 1883;
const char* mqttUser = ""; 
const char* mqttPassword = "";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  Serial.begin(115200);
  NodeMCU.begin(4800);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

if(!WiFi.status()){
    Serial.println("Connected to the WiFi network with ");
    }else{
      Serial.print("Failed to Connect to the raspberry pi with IP : ");
      Serial.println(mqttServer2);
      }


  client.setServer(mqttServer2, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to raspberry pi...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

      Serial.print("connected to the raspberry pi whith IP : ");
      Serial.println(mqttServer2);
      Serial.print("Loca IP Address of NodeMCU : ");
      Serial.println(WiFi.localIP());

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

}



void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message from raspberry pi : ");

  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String Topic_Str(topic);

  if( Topic_Str == "t_data"){

        String a_data = "Send String to raspberry pi.";

        //
        //
        //
        //
        //
        **// So here i am sending string to raspberry pi. Now how could i know that the raspberry pi had got the actual value or string
        // and there is no error , no bit has changed during communication.**
        //
        //
        //
        //
        //


        delay(5000);
        client.publish("a_data", (char*) a_data.c_str());

    }

}

void loop() {

    client.subscribe("t_data");
    delay(1000);
    client.loop();

}
Mridul Das
  • 101
  • 2
  • 5
  • 1
    [Edit](https://stackoverflow.com/posts/60912912/edit) the question to show your code and then somebody can probably explain how to add some error handling. Also what type of errors are you expecting? – hardillb Mar 29 '20 at 12:03

1 Answers1

1

First up, a 5 second delay in you message callback is a REALLY bad idea, these callbacks should run as quickly as possible, you are currently injecting 6 seconds between each client.loop when a message is received.

Second from the docs:

boolean publish (topic, payload)

Publishes a string message to the specified topic.

Parameters:

  • topic - the topic to publish to (const char[])
  • payload - the message to publish (const char[])

Returns

  • false - publish failed, either connection lost, or message too large
  • true - publish succeeded

So client.publish() will return true if the publish was successful and false if it failed.

(I modified the doc slightly, the it says the return type should be int, but it's actually a boolean when checking the src and it makes sense for the listed return options)

Community
  • 1
  • 1
hardillb
  • 54,545
  • 11
  • 67
  • 105