|
| 1 | +""" |
| 2 | +'adafruitio_05_type_conversion.py' |
| 3 | +========================================= |
| 4 | +Example of sending and receiving |
| 5 | +different data types to/from Adafruit |
| 6 | +IO using the Adafruit IO Python Client |
| 7 | +
|
| 8 | +Author(s): Brent Rubell, Todd Treece for Adafruit Industries |
| 9 | +""" |
| 10 | + |
| 11 | +# import Adafruit IO REST client. |
| 12 | +from Adafruit_IO import Client, Feed, RequestError |
| 13 | + |
| 14 | +# Set to your Adafruit IO key. |
| 15 | +# Remember, your key is a secret, |
| 16 | +# so make sure not to publish it when you publish this code! |
| 17 | +ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' |
| 18 | + |
| 19 | +# Set to your Adafruit IO username. |
| 20 | +# (go to https://accounts.adafruit.com to find your username) |
| 21 | +ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME' |
| 22 | + |
| 23 | +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 24 | + |
| 25 | +try: # if we have a 'type' feed |
| 26 | + type = aio.feeds('type') |
| 27 | +except RequestError: # create a type feed |
| 28 | + feed = Feed(name="type") |
| 29 | + type = aio.create_feed(feed) |
| 30 | + |
| 31 | +# Values to send to Adafruit IO |
| 32 | +char_val = 'a' |
| 33 | +string_val = 'adafruit' |
| 34 | +bool_val = True |
| 35 | +int_val = 3 |
| 36 | +long_val = 0x80000000 |
| 37 | +double_val = 3.1415926535897932 |
| 38 | +float_val = +1E6 |
| 39 | + |
| 40 | + |
| 41 | +""" |
| 42 | +Let's send some values to our feed |
| 43 | +and properly receive them |
| 44 | +""" |
| 45 | +print("Sending to Adafruit IO...") |
| 46 | + |
| 47 | +# Char |
| 48 | +print('\tSending Character: ', char_val) |
| 49 | +aio.send(type.key, char_val) |
| 50 | +data = aio.receive(type.key).value |
| 51 | +print('\t\tReceived Character: ', str(data)) |
| 52 | + |
| 53 | +# String |
| 54 | +print('\n\tSending String: ', string_val) |
| 55 | +aio.send(type.key, string_val) |
| 56 | +data = aio.receive(type.key).value |
| 57 | +print('\t\tReceived String: ', str(data)) |
| 58 | + |
| 59 | +# Boolean |
| 60 | +print('\n\tSending Bool: ', bool_val) |
| 61 | +aio.send(type.key, bool_val) |
| 62 | +data = aio.receive(type.key).value |
| 63 | +print('\t\tReceived Bool: ', bool(data)) |
| 64 | + |
| 65 | +# Integer |
| 66 | +print('\n\tSending Int: ', int_val) |
| 67 | +aio.send(type.key, int_val) |
| 68 | +data = aio.receive(type.key).value |
| 69 | +print('\t\tReceived Int: ', int(data)) |
| 70 | + |
| 71 | +# Long |
| 72 | +print('\n\tSending Long: ', long_val) |
| 73 | +aio.send(type.key, long_val) |
| 74 | +data = aio.receive(type.key).value |
| 75 | +print('\t\tReceived Long: ', int(data)) |
| 76 | + |
| 77 | +# Double |
| 78 | +print('\n\tSending Double: ', double_val) |
| 79 | +aio.send(type.key, double_val) |
| 80 | +data = aio.receive(type.key).value |
| 81 | +print('\t\tReceived Double: ', float(data)) |
| 82 | + |
| 83 | +# Float |
| 84 | +print('\n\tSending Float: ', float_val) |
| 85 | +aio.send(type.key, float_val) |
| 86 | +data = aio.receive(type.key).value |
| 87 | +print('\t\tReceived Float: ', float(data)) |
0 commit comments