0

can you read the value of a toggle block ?
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.

can you read the value of a toggle block ?

by jimk123 on Mon Mar 29, 2021 12:27 pm

This is somewhat related to a previous post viewtopic.php?f=56&t=177247 I was wondering if I could read the value of a toggle block ? I defined two toggle blocks on a dasdboard with values of on and off and in the Arduino code defined both as a subscribe and a publish definition. I was wondering if you can 'read' the current or last value published of a feed ? I have always published or subscribed to a feed but was wondering if you can get the last value ?

thanks

Code: Select all | TOGGLE FULL SIZE
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);

// define objects on the dasdboard to receive commands from like a toggle button or keypad
Adafruit_MQTT_Subscribe onoffbutton  = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.deckrelay"); // on or off
Adafruit_MQTT_Subscribe onoffbutton2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.dockrelay"); // on or off


// redefine the toggle blocks so we can publish to them.  this is in case the power is reset and the relays
// go back to an off state we can reset the toggle state on the dashboard
Adafruit_MQTT_Publish deckrelaypublish  = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.deckrelay");
Adafruit_MQTT_Publish dockrelaypublish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.dockrelay");

jimk123
 
Posts: 280
Joined: Sun Dec 26, 2010 7:04 pm

Re: can you read the value of a toggle block ?

by brubell on Mon Mar 29, 2021 12:55 pm

You'll need to access the /get topic:
https://io.adafruit.com/api/docs/mqtt.h ... -get-topic

We have an example for using this topic with Adafruit IO Arduino here:
https://github.com/adafruit/Adafruit_IO ... d_read.ino

brubell
 
Posts: 1377
Joined: Fri Jul 17, 2015 10:33 pm

Re: can you read the value of a toggle block ?

by jimk123 on Mon Mar 29, 2021 3:53 pm

I was looking at the code example you referenced #include "config.h" which defines the SSID/pwd etc but I already an doing that in my current code so pretty sure that will not compile. I did however stumble on this property (.lastread) which seems to do what I need to do:

Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.deckrelay"); // on or off

later in the main loop this seems to tell the current state of the toggle on the dasdboard - Serial.println((char *)onoffbutton.lastread);

I just have one sketch that is running on a Adafruit ESP32 feather using wifi and accepting subscribe commands using two toggle blocks to toggle two relays and you showed me in a previous topic how to also publish to the same feed name for error handling in case the relay lost power I could keep the dasdboard in sync:

// redefine the toggle blocks so we can publish to them. this is in case the power is reset and the relays
// go back to an off state we can reset the toggle state on the dashboard
Adafruit_MQTT_Publish deckrelaypublish = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/boatsubscribe.deckrelay");

The reason to get the 'lastread' value of the toggle block was so I could tell if I needed to publish the latest state of the toggle block, based on the status of the relay somehow got out of sync from the dashboard. I'm sure this sounds confusing as I try and describe it, lol and under normal conditions none of it would execute, it is only for hopefully a rare condition where the relay malfunctioned.

thanks
I think I am all set.

jimk123
 
Posts: 280
Joined: Sun Dec 26, 2010 7:04 pm

Please be positive and constructive with your questions and comments.