|
| 1 | +""" |
| 2 | +'adafruitio_04_location.py' |
| 3 | +================================== |
| 4 | +Example of sending GPS data points |
| 5 | +to an Adafruit IO Feed using the API |
| 6 | +
|
| 7 | +Author(s): Brent Rubell, Todd Treece |
| 8 | +""" |
| 9 | +# Import standard python modules |
| 10 | +import time |
| 11 | + |
| 12 | +# Import Adafruit IO REST client. |
| 13 | +from Adafruit_IO import Client, Feed, RequestError |
| 14 | + |
| 15 | +# Set to your Adafruit IO key. |
| 16 | +# Remember, your key is a secret, |
| 17 | +# so make sure not to publish it when you publish this code! |
| 18 | +ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' |
| 19 | + |
| 20 | +# Set to your Adafruit IO username. |
| 21 | +# (go to https://accounts.adafruit.com to find your username) |
| 22 | +ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME' |
| 23 | + |
| 24 | +# Create an instance of the REST client. |
| 25 | +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 26 | + |
| 27 | +# Assign a location feed, if one exists already |
| 28 | +try: |
| 29 | + location = aio.feeds('location') |
| 30 | +except RequestError: # Doesn't exist, create a new feed |
| 31 | + feed = Feed(name="location") |
| 32 | + location = aio.create_feed(feed) |
| 33 | + |
| 34 | +# limit feed updates to every 3 seconds, avoid IO throttle |
| 35 | +loop_delay = 5 |
| 36 | + |
| 37 | +# We dont' have a GPS hooked up, but let's fake it for the example/test: |
| 38 | +# (replace this data with values from a GPS hardware module) |
| 39 | +value = 0 |
| 40 | +lat = 40.726190 |
| 41 | +lon = -74.005334 |
| 42 | +ele = 6 # elevation above sea level (meters) |
| 43 | + |
| 44 | + |
| 45 | +while True: |
| 46 | + print('\nSending Values to location feed...\n') |
| 47 | + print('\tValue: ', value) |
| 48 | + print('\tLat: ', lat) |
| 49 | + print('\tLon: ', lon) |
| 50 | + print('\tEle: ', ele) |
| 51 | + # Send location data to Adafruit IO |
| 52 | + aio.send_location_data(location.key, value, lat, lon, ele) |
| 53 | + # shift all values (for test/demo purposes) |
| 54 | + value += 1 |
| 55 | + lat -= 0.01 |
| 56 | + lon += -0.02 |
| 57 | + ele += 1 |
| 58 | + |
| 59 | + # Read the location data back from IO |
| 60 | + print('\nData Received by Adafruit IO Feed:\n') |
| 61 | + data = aio.receive(location.key) |
| 62 | + print('\tValue: {0}\n\tLat: {1}\n\tLon: {2}\n\tEle: {3}' |
| 63 | + .format(data.value, data.lat, data.lon, data.ele)) |
| 64 | + # wait loop_delay seconds to avoid api throttle |
| 65 | + time.sleep(loop_delay) |
0 commit comments