Greatings !!!
I have the followed problem. I build three Arduino modules with the same code . I noticed that if I restart my arduinos without restarting my mqt server they can't subcribe to topics but can sent messages to topics. When I restart mqt server and tha arduinos are connecting everything OK. Is this normal?
I am using esp 01 and NdeMCU
and here is asimple example of my code:
`
Greatings !!! I have the followed problem. I build three Arduino modules with the same code . I noticed that if I restart my arduinos without restarting my mqt server they can't subcribe to topics but can sent messages to topics. When I restart mqt server and tha arduinos are connecting everything OK. Is this normal?
I am using esp 01 and NdeMCU and here is asimple example of my code: `
include
include
const char ssid = "David"; const char password = "hh6945441201"; const char* mqtt_server = "192.168.1.3"; WiFiClient espClient; PubSubClient client(espClient);
byte willQoS = 0; const char willTopic = "tele/esp/LWT/3"; const char willMessage = "Offline"; boolean willRetain = true; int mess = 9;
void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }
void callback(char topic, byte payload, unsigned int length) {
Serial.print("Message arrived ["); Serial.print(topic); Serial.println("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); if (length - i == 1) { mess = payload[i] - 48; } } Serial.println(); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.println("Attempting MQTT connection..."); // Create a random client ID String clientId = "David_ESP_3"; if (client.connect(clientId.c_str())) { Serial.println("connected");
} }
void setup() {
Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); client.connect("David_ESP_3", willTopic, willQoS, willRetain, willMessage); client.publish("tele/esp/LWT/3", "Online"); Serial.println("System_ESP_3"); pinMode(2,OUTPUT); //relay }
void loop() { if (!client.connected()) { reconnect(); } client.loop(); if (mess == 1) { client.publish ("stat/esp/power/3", "ON"); digitalWrite(2,HIGH); mess = 9; } else if (mess == 2) { client.publish ("stat/esp/power/3", "OFF"); digitalWrite(2,LOW); mess = 9; } delay(500);
} ` thnx in advance...