MQTT subscription returns no value

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
jmpantelis
 
Posts: 14
Joined: Fri Mar 20, 2015 2:15 pm

MQTT subscription returns no value

Post by jmpantelis »

I have a feed called 'pacer' I am subscribing to from an Arduino sketch using MQTT API in a Huzzah-ESP12 board. I want to get the feeder connected to a slider block in a dashboard with settings of increments of 5, from 5 to 600. I have three other blocks for three different sensor I am publishing data for to the same dashboard. The idea is to set the slider in IO Adafruit to modify the rate (through the pacer feed) in which my sketch publishes data in the other feeds linked to sensors I have connected to the Huzzah. The subscription seems unstable. The publishers are very stable. Sometimes my sketch subscription handle recognizes I made a change in the slider in IO Adafruit, sometimes not. Sometimes not at all. Also I seem to be getting null values back through that pacer feed. I checked the length of payload and it is always 5, but the last value read seems to come back as null always. Is there an issue with this feature or the library? Or perhaps I have something messed up in my code?

#define ECHO true

const char PACER_FEED[] PROGMEM = AIO_USERNAME "/feeds/pacer";
Adafruit_MQTT_Subscribe pacer = Adafruit_MQTT_Subscribe(&mqtt, PACER_FEED);

In setup:

mqtt.subscribe(&pacer);

// connect to adafruit io
connect();

In loop:

int pace= 300; // 300 seconds default
Adafruit_MQTT_Subscribe *subscription;

// check if still connected to adafruit io; else reconnect
if( !mqtt.ping(3) ) {
if( !mqtt.connected() ) {
connect();
}
}

// this is our 'wait for incoming subscription packets' busy sub loop
// do I need to adjust readSubscription parameter based on my last pace value?
while (subscription = mqtt.readSubscription(1000)) {
if(ECHO) Serial.println(F("Attempting to read pacer value..."));

// we only care about the pacer events
if (subscription == &pacer) {

// convert mqtt ascii payload to int
char *avalue = (char *)pacer.lastread;

if(ECHO){
Serial.print(F("Pacer value received: "));
Serial.println(avalue); // No avalue comes out here. though pacer.length is always 5
}

pace = atoi(avalue); // convert to integer
}
}

delay( pace*1000 );

User avatar
awall
 
Posts: 24
Joined: Sat Sep 20, 2014 10:40 am

Re: MQTT subscription returns no value

Post by awall »

I started with Adafruit IO today, so I'm still beginning my tests. However I've also noticed some unstable / unreliable behavior using the MQTT subscription samples.
The publish code runs very realiably. I'm using a Huzzah-ESP12 board as well.
My loop() contains the following code:

Code: Select all

void loop()
{
  Adafruit_MQTT_Subscribe *subscription;

  // ping Adafruit IO a few times to make sure we remain connected
  if(! mqtt.ping(3))
  {
   if(! mqtt.connected())
      connect();
  }

  while (subscription = mqtt.readSubscription(1000))
  {
    if (subscription == &mySubscription)
    {
      char *value = (char *)mySubscription.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);
      current = atoi(value);   // it's 0 or 1
    }
    
    digitalWrite(Red, current);
    delay(1);        // delay in between reads for stability
  }
}
Sometimes the message is received twice (for example when I change the value from a widget control in the dashboard) sometimes once (when I cahange the value with Rest API), sometimes it's not even received once...

All libraries (ESP8266 and Adafruit IO) are up to date.

Any idea?

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: MQTT subscription returns no value

Post by jwcooper »

We deployed a change yesterday to fix the duplicate values being sent from the dashboard. Let me know if you continue to see it.

Are you still seeing subscription issues with MQTT?

User avatar
awall
 
Posts: 24
Joined: Sat Sep 20, 2014 10:40 am

Re: MQTT subscription returns no value

Post by awall »

No more duplicated values from the dashboard now, thanks.
I have to test again the subscription with a real board which I don't have at hand right now.

However, the problem I faced with subscriptions was quite certainly in the ESP8266 library, not server side.
A few days ago I had 4 different systems publishing and subscribing at the same time on the same topics, and the Huzzah was the only client missing about 10% of the messages it was subscribed to.
Didn't have the same issue with other subscribers (AIO Dashboard, MQTT Spy, MQTT.fx, MyMQTT Android App).

Andrea

User avatar
jmpantelis
 
Posts: 14
Joined: Fri Mar 20, 2015 2:15 pm

Re: MQTT subscription returns no value

Post by jmpantelis »

It is more responsive, indeed. I am getting values for feeders I subscribe to. I also realized I was using the ESP staging library, and not the stable one. I do think there is an issue with the ESP code distribution related to the subscription implementation though looking at other people's experience with it. I get values back but not on a consistent basis, and only if I change a value in the dashboard block. Is there a way to force a read on the ESP Huzzah side? Not sure what it is yet. Will do some more trouble shooting once I am done with a couple of other projects I am working on.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”