Skip to content

Commit d2bb729

Browse files
author
brentru
committed
update example for new send_data
1 parent 770bb96 commit d2bb729

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

Adafruit_IO/client.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ def __init__(self, username, key, proxies=None, base_url='https://io.adafruit.co
5858
# constructing the path.
5959
self.base_url = base_url.rstrip('/')
6060

61+
@staticmethod
62+
def _create_payload(value, metadata):
63+
if metadata is not None:
64+
payload = Data(value=value,lat=metadata['lat'], lon=metadata['lon'],
65+
ele=metadata['ele'], created_at=metadata['created_at'])
66+
return payload
67+
return Data(value=value)
6168

6269
def _compose_url(self, path, is_time=None):
6370
if is_time: # return a call to https://io.adafruit.com/api/v2/time/{unit}
6471
return '{0}/api/{1}/{2}'.format(self.base_url, 'v2', path)
6572
else:
6673
return '{0}/api/{1}/{2}/{3}'.format(self.base_url, 'v2', self.username, path)
6774

68-
6975
def _handle_error(self, response):
7076
# Throttling Error
7177
if response.status_code == 429:
@@ -109,16 +115,6 @@ def _delete(self, path):
109115
proxies=self.proxies)
110116
self._handle_error(response)
111117

112-
@staticmethod
113-
def _create_payload(value, metadata):
114-
"""
115-
"""
116-
if metadata is not None:
117-
payload = Data(value=value,lat=metadata['lat'], lon=metadata['lon'],
118-
ele=metadata['ele'], created_at=metadata['created_at'])
119-
return payload
120-
return Data(value=value)
121-
122118
# Data functionality.
123119
def send_data(self, feed, value, metadata=None, precision=None):
124120
"""Helper function to simplify adding a value to a feed. Will append the
@@ -162,16 +158,6 @@ def append(self, feed, value):
162158
"""
163159
return self.create_data(feed, Data(value=value))
164160

165-
def send_location_data(self, feed, lat, lon, ele, value=None):
166-
"""Sends locational data to a feed.
167-
:param string feed: Name/Key/ID of Adafruit IO feed.
168-
:param int lat: Latitude.
169-
:param int lon: Longitude.
170-
:param int ele: Elevation.
171-
:param int value: Optional value to send, defaults to None.
172-
"""
173-
return self.create_data(feed, Data(value=value,lat=lat, lon=lon, ele=ele))
174-
175161
def receive_time(self, time):
176162
"""Returns the time from the Adafruit IO server.
177163
:param string time: Time to be returned: `millis`, `seconds`, `ISO-8601`.

examples/api/location.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""
22
'location.py'
33
==================================
4-
Example of sending location over an
5-
Adafruit IO feed to a Map Dashboard
6-
block
4+
Example of sending metadata
5+
associated with a data point.
76
87
Author(s): Brent Rubell
98
"""
@@ -12,9 +11,14 @@
1211
from Adafruit_IO import Client, Feed, RequestError
1312

1413
# Set to your Adafruit IO key.
15-
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
14+
# Remember, your key is a secret,
15+
# so make sure not to publish it when you publish this code!
1616
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
1717

18+
# Set to your Adafruit IO username.
19+
# (go to https://accounts.adafruit.com to find your username)
20+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
21+
1822
# Create an instance of the REST client.
1923
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
2024

@@ -25,12 +29,12 @@
2529
feed = Feed(name="location")
2630
location = aio.create_feed(feed)
2731

28-
29-
# Top Secret Adafruit HQ Location
30-
value = 1
31-
lat = 40.726190
32-
lon = -74.005334
33-
ele = 6 # elevation above sea level (meters)
32+
value = 42
33+
# Set metadata associated with value
34+
metadata = {'lat': 40.726190,
35+
'lon': -74.005334,
36+
'ele': -6,
37+
'created_at': None}
3438

3539
# Send location data to Adafruit IO
36-
aio.send_location_data(location.key, lat, lon, ele, value)
40+
aio.send_data(location.key, value, metadata)

0 commit comments

Comments
 (0)