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();
}