diff --git a/examples/basics/digital_out.py b/examples/basics/digital_out.py index 5da4f99..3660f21 100644 --- a/examples/basics/digital_out.py +++ b/examples/basics/digital_out.py @@ -6,6 +6,7 @@ Author(s): Brent Rubell, Todd Treece """ + # Import standard python modules import time @@ -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) @@ -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)