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 ('\n Sending Values to location feed...\n ' )
40
+ print ('\t Value: ' , value )
41
+ print ('\t Lat: ' , lat )
42
+ print ('\t Lon: ' , lon )
43
+ print ('\t Ele: ' , 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 ('\n Data from Adafruit IO Feed:\n ' )
56
+ print ('\t Value: {0}\n \t Lat: {1}\n \t Lon: {2}\n \t Ele: {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