Я использую MQTT для связи между ESP8266 и Raspberry Pi , и я отправляю несколько строк из ESP8266 в Raspberry Pi. Поэтому я хочу знать, есть ли какие-либо функции или что-то, что может обнаружить ошибку при отправке данных (String) в Raspberry Pi (если произошла ошибка). И если так, то как я могу обработать эту ошибку ?
Это мой код в 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();
}