knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

Arduino mqtt subscriction happen only after restart mqtt server #352

Open hwarang098tkd opened 7 years ago

hwarang098tkd commented 7 years ago

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");

  client.publish("tele/esp/LWT/3", "Online");
  client.subscribe("cmnd/esp/power/3");
} else {
  delay(1000);
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(6000);
}

} }

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...

duemchen commented 6 years ago

the clientid must be unique. if I make some reconnects, then i have a problem. I have generate the clientid with random(), and all is good.