adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
571 stars 292 forks source link

Subscription callback fired when no messages sent. data has random strings. #149

Open rickblair opened 5 years ago

rickblair commented 5 years ago

I am using AdafruitIO Here is the callback: void onoffcallback(char *data, uint16_t len) {

Serial.print("Hey we're in a onoff callback, the button len -- value is: "); Serial.print(len); Serial.print(" -- "); Serial.println(data); String input = String(data); if(oldAlert.equals(input)) {

Serial.println("No Change Punt");

return;

} oldAlert = input; if(input.startsWith("ON")) {

String msg = "BARCODE: ";
hour += 1;
String hr = String(hour);
msg.concat(hr);
;
Serial.print("publishing: ");
Serial.println(msg);
barcodePub.publish(msg.c_str());

showAlert = true; } else { showAlert = false; } }

Here is the subscription code:

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/alert",MQTT_QOS_1); //Note tried all different QOS settings No change.

Here is loop:

MQTT_connect(); // onoffPub.publish(oldAlert.c_str()); while(1) { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect();

// wait 10 seconds for subscription messages // since we have no other tasks in this example. mqtt.processPackets(10000);

// keep the connection alive mqtt.ping(); } }

Every 10 seconds or a multiple of 10 seconds I get this output.

Hey we're in a onoff callback, the button len -- value is: 99 -- OFFARCODE: 1aa5da0357e4785b49b0202463d236c

It should just be either OFF or ON

I publish an ON message which shows up as expected: Hey we're in a onoff callback, the button len -- value is: 2 -- ON

Then an off message as expected: Hey we're in a onoff callback, the button len -- value is: 3 -- OFF

Then after about 20 secs I start getting: Hey we're in a onoff callback, the button len -- value is: 99 -- OFFARCODE: 2aa5da0357e4785b49b0202463d236c

I do have a barcode topic: Adafruit_MQTT_Publish barcodePub = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/barcode");

Where I publish a message in the callback. Somehow the string I use in the barcode is being used in the buffer in the subscribe code?

I can work around this, but it is disturbing to see random data showing up in a phantom subscription.

rickblair commented 5 years ago

A bit more info: I am running the code as a task pinned to a core: xTaskCreatePinnedToCore( codeForMQTT, "MQTTTask", 5000, NULL, 1, &padTask, 1);

That is why there is a while(1) in the loop code above. I got the same results when the code was running in loop instead of a pinned task.