Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions examples/basics/digital_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

Author(s): Brent Rubell, Todd Treece
"""

# Import standard python modules
import time

Expand All @@ -28,9 +29,9 @@
# 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
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)

Expand All @@ -40,16 +41,28 @@


while True:
TIME_TO_SLEEP = 0.5
try:
data = aio.receive(digital.key)
except RequestError as re:
pass # feed with no data will return 404
if int(data.value) == 1:
print('received <- ON\n')
elif int(data.value) == 0:
print('received <- OFF\n')

# set the LED to the feed value
led.value = int(data.value)
if int(data.value) == 1:
print("received <- ON\n")
elif int(data.value) == 0:
print("received <- OFF\n")

# set the LED to the feed value
led.value = int(data.value)

except RequestError as e:
# feed with no data will return 404
if "not found" in str(e).lower():
print(
"Feed 'digital' has no data yet!\n"
+ "Try adding some at https://io.adafruit.com/{0}/feeds/digital".format(
ADAFRUIT_IO_USERNAME
)
)
TIME_TO_SLEEP = 5 # allow a few seconds for user to add some data
else:
print("Error retrieving data from feed 'digital': {0}".format(e))
# timeout so we dont flood adafruit-io with requests
time.sleep(0.5)
time.sleep(TIME_TO_SLEEP)