Skip to content

Commit adee6be

Browse files
author
brentru
committed
aiobasics: location
1 parent 1936193 commit adee6be

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 python system libraries
10+
import time
11+
# Import Adafruit IO REST client.
12+
from Adafruit_IO import Client, Feed, RequestError
13+
14+
# Set to your Adafruit IO key.
15+
ADAFRUIT_IO_USERNAME = 'YOUR-USERNAME'
16+
ADAFRUIT_IO_KEY = 'YOUR-KEY'
17+
18+
# Create an instance of the REST client.
19+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
20+
21+
# Assign a location feed, if one exists already
22+
try:
23+
location = aio.feeds('location')
24+
except RequestError: # Doesn't exist, create a new feed
25+
feed = Feed(name="location")
26+
location = aio.create_feed(feed)
27+
28+
# limit feed updates to every 3 seconds, avoid IO throttle
29+
loop_delay = 5
30+
31+
# We dont' have a GPS hooked up, but let's fake it for the example/test:
32+
# (replace this data with values from a GPS hardware module)
33+
value = 0
34+
lat = 40.726190
35+
lon = -74.005334
36+
ele = 6 # elevation above sea level (meters)
37+
38+
while True:
39+
print('\nSending Values to location feed...\n')
40+
print('\tValue: ', value)
41+
print('\tLat: ', lat)
42+
print('\tLon: ', lon)
43+
print('\tEle: ', ele)
44+
# Send location data to Adafruit IO
45+
aio.send_location_data(location.key, value, lat, lon, ele)
46+
# shift all values (for test/demo purposes)
47+
value += 1
48+
lat -= 0.01
49+
lon += -0.02
50+
ele += 1
51+
52+
# Read the location data back from IO
53+
data = aio.receive(location.key)
54+
print(data)
55+
print('\nData from Adafruit IO Feed:\n')
56+
print('\tValue: {0}\n\tLat: {1}\n\tLon: {2}\n\tEle: {3}'.format(data.value, data.lat, data.lon, data.ele))
57+
# wait loop_delay seconds to avoid throttle
58+
time.sleep(loop_delay)

0 commit comments

Comments
 (0)