0

Python digital input not updating toggle block
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Python digital input not updating toggle block

by doubletap on Sun Aug 18, 2019 8:37 am

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)

doubletap
 
Posts: 11
Joined: Wed Sep 11, 2013 11:23 pm

Re: Python digital input not updating toggle block

by brubell on Mon Aug 19, 2019 9:59 am

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 44 times


And for toggling it off, you'd send the string value OFF:
off.png
off.png (66.28 KiB) Viewed 44 times


In the Python library, this'd be the same as:
Code: Select all | TOGGLE FULL SIZE
aio.send(digital.key, "ON")
and
Code: Select all | TOGGLE FULL SIZE
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 44 times


- brent r

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

Please be positive and constructive with your questions and comments.