|
28 | 28 | # Create an instance of the REST client. |
29 | 29 | aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
30 | 30 |
|
31 | | -try: # if we have a 'digital' feed |
32 | | - digital = aio.feeds('digital') |
33 | | -except RequestError: # create a digital feed |
| 31 | +try: # if we have a 'digital' feed |
| 32 | + digital = aio.feeds("digital") |
| 33 | +except RequestError: # create a digital feed |
34 | 34 | feed = Feed(name="digital") |
35 | 35 | digital = aio.create_feed(feed) |
36 | 36 |
|
|
40 | 40 |
|
41 | 41 |
|
42 | 42 | while True: |
| 43 | + TIME_TO_SLEEP = 0.5 |
43 | 44 | try: |
44 | 45 | data = aio.receive(digital.key) |
45 | | - except RequestError as re: |
46 | | - pass # feed with no data will return 404 |
47 | | - if int(data.value) == 1: |
48 | | - print('received <- ON\n') |
49 | | - elif int(data.value) == 0: |
50 | | - print('received <- OFF\n') |
51 | | - |
52 | | - # set the LED to the feed value |
53 | | - led.value = int(data.value) |
| 46 | + if int(data.value) == 1: |
| 47 | + print("received <- ON\n") |
| 48 | + elif int(data.value) == 0: |
| 49 | + print("received <- OFF\n") |
| 50 | + |
| 51 | + # set the LED to the feed value |
| 52 | + led.value = int(data.value) |
| 53 | + |
| 54 | + except RequestError as e: |
| 55 | + # feed with no data will return 404 |
| 56 | + error_message = getattr(e, "message", str(e)) |
| 57 | + if getattr(e, "status_code", None) == 404 and "not found" in error_message.lower(): |
| 58 | + print( |
| 59 | + "Feed 'digital' has no data yet!\n" |
| 60 | + + "Try adding some at https://io.adafruit.com/{0}/feeds/digital".format( |
| 61 | + ADAFRUIT_IO_USERNAME |
| 62 | + ) |
| 63 | + ) |
| 64 | + TIME_TO_SLEEP = 5 |
| 65 | + else: |
| 66 | + print("Error retrieving data from feed 'digital': {0}".format(e)) |
54 | 67 | # timeout so we dont flood adafruit-io with requests |
55 | | - time.sleep(0.5) |
| 68 | + time.sleep(TIME_TO_SLEEP) |
0 commit comments