Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Reading (not subscribing to) a topic
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.
Please be positive and constructive with your questions and comments.

Reading (not subscribing to) a topic

by dmikhalsky on Wed Jan 13, 2016 3:18 pm

If I just want to read topic value, what shall I do?

Standard
Code: Select all | TOGGLE FULL SIZE
  while (subscription = mqtt.readSubscription(1000)) {

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

      // convert mqtt ascii payload to int
      char *value = (char *)lamp.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);
      int current = atoi(value);

      // write the current state to the power switch tail
      digitalWrite(LAMP, current == 1 ? HIGH : LOW);

    }

  }


routine doesn't work as no value is changed

dmikhalsky
 
Posts: 3
Joined: Fri Nov 27, 2015 7:16 am

Re: Reading (not subscribing to) a topic

by dmikhalsky on Thu Jan 14, 2016 10:01 am

I am using ESP8266 module as my hardware.
Publishing works fine, even subscriping works fine IF the topic is being changed.

Given I use the module for counting water consumtion, if I restart, I need to know what was the value before restart in order not to start counting from scratch.
For this, I need to READ topic value

dmikhalsky
 
Posts: 3
Joined: Fri Nov 27, 2015 7:16 am

Re: Reading (not subscribing to) a topic

by jwcooper on Fri Jan 15, 2016 11:47 am

When you initially subscribe to the topic, you should get the last value back immediately. From then on, you will get any new values that are sent. The saved value for the topic does have an expiration based on a FIFO queue. I'd guess it would stay in memory for quite some time though.

jwcooper
 
Posts: 677
Joined: Tue May 01, 2012 9:08 pm

Please be positive and constructive with your questions and comments.