knolleary / pubsubclient

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

MQTT Callback not invoked when topic is received from external client #967

Closed henkhoven closed 1 year ago

henkhoven commented 1 year ago

I am writing code that receives energy prices for the upcoming day, I have Mosquitto broker running locally and using MQTT Explorer to verify if messages are received/published. When publishing to the subscribed topic "global/prices/energy/frank" loccally using MQTT Explorer connected to the local broker everything works as expected, the callback function is called and prices for the upcoming day are processed. However, when an external source publishes to the topic on the loval broker the callback function is not invoked allthough I see the message arrived using MQTT Explorer.

The client.loop() function is called regurarly from within loop().

MQTT Connect: int mqttConnect::Connect(PubSubClient& mymqttClient, char* mqttconfig[]) { errornr = -1; unsigned long deltaTimer = 0; if(setupBroker == 0) { Serial.println("setupBroker == 0, reconfiguring MQTT"); begin_mqtt(mymqttClient, mqttconfig); } else { Serial.println("setupBroker != 0, skipping MQTT configuration"); } mymqttClient.disconnect(); for (int i=0; i<3; i++) { CTOTimer = millis(); deltaTimer = millis(); mymqttClient.connect(mqttconfig[2], mqttconfig[3], mqttconfig[4]); while(millis() - CTOTimer < WTIMEOUT) { if (mymqttClient.connected() == true) { i = 3; CTOTimer = millis() + WTIMEOUT; Serial.println("Connected to MQTT Broker"); } } if (mymqttClient.connected() == false) { errornr = 1; } else { mymqttClient.setCallback(mqttprot.mqttcallback); mymqttClient.subscribe("global/prices/energy/frank"); } } return errornr; }

Any suggestions where to look?

henkhoven commented 1 year ago

Problem solved, was caused by a duplicate clientID, the remote client had the same clientID as the local client. The broker responded to the clientID when a message was received on the topic but relaying the information back to the clientID failed due to the duplicate clientID.

I will close this topic.