0

Getting initial values from adafruit.io via MQTT
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Getting initial values from adafruit.io via MQTT

by kmodric on Sat Oct 15, 2016 6:02 am

For me this also not working. Can't get initial value after reboot.

:-(

Is there some workaround how to get this working?

Thanks.

kmodric
 
Posts: 9
Joined: Sun Aug 28, 2016 2:33 pm

Re: Getting initial values from adafruit.io via MQTT

by JonMayer on Thu Nov 03, 2016 8:25 pm

I was shocked that this didn't work as expect for me either. Upon connection, retained values are sent from the MQTT server but the ESP8266 doesn't get these values unless they are 'pushed out' again.

JonMayer
 
Posts: 1
Joined: Fri Mar 06, 2015 8:29 am

Re: Getting initial values from adafruit.io via MQTT

by kodiakklub on Sat Nov 05, 2016 3:38 pm

in addition to not getting an initial state push, the subscription seems to time out and not send any state after maybe 10 minutes or less. the connection to the server holds up fine. this seems like a major function of what a lot of people are trying to incorporate into their projects.

kodiakklub
 
Posts: 1
Joined: Thu Oct 27, 2016 8:54 pm

Re: Getting initial values from adafruit.io via MQTT

by viscons on Tue Jan 24, 2017 12:09 pm

Surely there must be fix for this fundamental issue. I like using the dashboards and widgets but I may have to move to another service. I have been using a workaround whereby I send the current status to a thingspeak channel and upon reboot it reads the status back, but how awful is that! Please Adafruit get this working.

viscons
 
Posts: 2
Joined: Mon Jan 25, 2016 4:19 pm

Re: Getting initial values from adafruit.io via MQTT

by blitzsss on Tue Jan 24, 2017 10:49 pm

using the Adafruit IO library I have come across the same issue but it seems when setting up multiple feeds i.e

AdafruitIO_Feed *counter = io.feed("counter");
AdafruitIO_Feed *counter_two = io.feed("counter-two");
AdafruitIO_Feed *light = io.feed("light");

it's only the very last feed in the list i.e light which will read back the initial value after a reboot without requiring the feed to be updated.

blitzsss
 
Posts: 8
Joined: Fri Dec 02, 2016 1:19 am

Re: Getting initial values from adafruit.io via MQTT

by mannesq on Wed Jul 12, 2017 4:00 am

Any news?

Having the same problem here

mannesq
 
Posts: 5
Joined: Sun Mar 12, 2017 11:00 am

Re: Getting initial values from adafruit.io via MQTT

by ivux on Sat Jul 15, 2017 7:45 pm

I also have the same problem - last/initial (ON, OFF) value is not sent upon re-conection.
I am using Arduino Yun with Adafruit MQTT example.
Only when re-triggered the switch the value is sent (also quite unreliable - sometimes I get the change, sometimes not)
Bit frustrating.

ivux
 
Posts: 1
Joined: Sat Jul 15, 2017 7:24 pm

Re: Getting initial values from adafruit.io via MQTT

by haku15 on Fri Dec 21, 2018 6:18 pm

I also don't get the initial value during the first MQTT connection. I am even using the "get" topic modifier, as explained by abachman in viewtopic.php?f=56&t=135898&p=678013&hilit=mqtt+get+current+value#p676480. I am also following his example in the same thread.
I am using an ATmega1284P connected to Adafruit Winc1500 breakout board.
Libraries:Adafruit MQTT 0.20.3, WiFi101 0.15.2
Arduino IDE 1.8.1.
Winc1500 FW 19.5.4

I did 2 test, one with wireless connection always active (with or without WiFi.lowPowerMode(), does not matter) and one with wifi power off (Wifi.end()).
The code is below, the only difference between the 2 test is uncomment Wifi.end(). Anyway we can concentrate on the first one, as I think the cause is the same.

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>

#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi101.h>

#define WINC_CS   14
#define WINC_IRQ  10
#define WINC_RST  4
#define WINC_EN   24   

char ssid[] = "ssid";     //  your network SSID (name)
char pass[] = "pass";    // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
int sliderValue = 0;

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  8883 
#define AIO_USERNAME    "aio_user"
#define AIO_KEY         "aio_key"

WiFiSSLClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

#ifdef DEBUG_ON
#define halt(s) { Serial.println(F( s )); while(1);  }
#endif
#define halt(s) {  while(1);  }

/****************************** Feeds ***************************************/

Adafruit_MQTT_Subscribe sliderFeed = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/test.slider");
Adafruit_MQTT_Publish sliderFeed_get = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test.slider/get");

void setup() {
   
    Serial.begin(115200);
    WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);   
 
    if (WiFi.status() == WL_NO_SHIELD) {
     
      Serial.println("WINC1500 not present");
      // don't continue:
      while (true);
    }
    Serial.println("ATWINC OK!");
 
    WiFi.lowPowerMode();
   
    mqtt.subscribe(&sliderFeed);
}

void loop() {

  MQTT_connect();
  getIOData();
  Serial.print(F("Slider Value: "));
  Serial.println(sliderValue);
  WiFi.end();
  delay(5000);
}

void getIOData(){
 
  Adafruit_MQTT_Subscribe *subscription;
  Serial.println(F("Start read Subscription"));
   
  while ((subscription = mqtt.readSubscription(5000))) {
   
    if (subscription == &sliderFeed) {
     
      sliderValue = atoi((char *)sliderFeed.lastread);
   
      Serial.print(F("Got: "));
      Serial.println(sliderValue);

    }
  }
  Serial.println(F("End read Subscription "));
}

void MQTT_connect() {
  int8_t ret;

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    uint8_t timeout = 10;
    while (timeout && (WiFi.status() != WL_CONNECTED)) {
      timeout--;
      delay(1000);
    }

     // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT Connected!");
  }
  sliderFeed_get.publish((int32_t)0);
}

Test Results:
I set an initial value of 19 on the slider in AIO. Wait 5 secs and the boot my device.
I waited two loop cycle and as you can see, I did not get any initial value (0 instead of 19).
I then moved the slider to 23 during mqtt.readSubscription(), and I got 23.
Then outside mqtt.readSubscription() window, I moved to 26 and I received that value correctly as soon as the next mqtt.readSubscription() window started.
Console Output:
Code: Select all | TOGGLE FULL SIZE
ATWINC OK!
Attempting to connect to SSID: ssid
Connecting to MQTT... MQTT Connected!
Start read Subscription
End read Subscription
Slider Value: 0
Start read Subscription
End read Subscription
Slider Value: 0
Start read Subscription (Here I moved the slider to 23 during readSubscription)
Got: 23
End read Subscription
Slider Value: 23  (Here I moved the slider  to 26 outside readSubscription)
Start read Subscription
Got: 26
End read Subscription
Slider Value: 26

In the WiFi.end() test, the slider value are only received if the slider is moved during mqtt.readSubscription().

Could you please check if there is a problem?

Thank you

haku15
 
Posts: 72
Joined: Mon Jun 25, 2012 12:45 pm

Please be positive and constructive with your questions and comments.