Python digital input not updating 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.
Locked
User avatar
DoubleTap
 
Posts: 36
Joined: Wed Sep 11, 2013 11:23 pm

Python digital input not updating toggle block

Post by DoubleTap »

I've run into an odd problem while trying to use a dashboard Toggle block to reflect the status of a fan. Basically if the fan is turned on I want the toggle block to show ON and vice-versa if the fan turned off. But what is happening is the python script only updates the toggle to Off, never On.
For simplicity's sake I've included only the heart of the code that is to control the toggle block status, which is just a modified version of what I found in your documentation for digital input. The code creates the feed if it doesn't exist then runs the while True: loop where it is supposed to set the toggle to ON, wait 2 seconds and then set the toggle to Off. It will do that until a keyboard interrupt is detected. I've tried the 1 and 0 as both strings and INT. Neither work.
Any help here would be greatly appreciated. Thank you!

# Import standard python modules
import time
# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed, RequestError

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'userKey'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = 'username'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

try: # if we have a 'digital' feed
digital = aio.feeds('digital')
except RequestError: # create a digital feed
feed = Feed(name="digital")
digital = aio.create_feed(feed)


while True:

aio.send(digital.key, str(1))
time.sleep(2)
aio.send(digital.key, str(0))

# avoid timeout from adafruit io
time.sleep(2)

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: Python digital input not updating toggle block

Post by brubell »

The toggle block toggles when the data in the feed matches the Button off text or the button on text.

If you're sending your feed the string ON:
on.png
on.png (66.29 KiB) Viewed 122 times
And for toggling it off, you'd send the string value OFF:
off.png
off.png (66.28 KiB) Viewed 122 times
In the Python library, this'd be the same as:

Code: Select all

aio.send(digital.key, "ON")
and

Code: Select all

aio.send(digital.key, "OFF")



If you want to send an integer value (such as 1 or 0), you'd need to change the Button On Text or Button Off Text
integers.png
integers.png (61.04 KiB) Viewed 122 times
- brent r

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”