Skip to content

Commit de591f0

Browse files
authored
Merge pull request #52 from adafruit/v2-examples
add examples for api-v2
2 parents 721bdc3 + 66ea1f4 commit de591f0

15 files changed

+444
-21
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ nosetests.xml
2929

3030
# Translations
3131
*.mo
32+
33+
# Local files
34+
release.md

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
system

examples/api/data.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
import datetime
88

99
# Set to your Adafruit IO key.
10-
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME'
11-
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
10+
# Remember, your key is a secret,
11+
# so make sure not to publish it when you publish this code!
12+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
13+
14+
# Set to your Adafruit IO username.
15+
# (go to https://accounts.adafruit.com to find your username)
16+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
1217

1318
# Create an instance of the REST client.
1419
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

examples/api/feeds.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
import json
88

99
# Set to your Adafruit IO key.
10-
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME'
11-
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
10+
# Remember, your key is a secret,
11+
# so make sure not to publish it when you publish this code!
12+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
13+
14+
# Set to your Adafruit IO username.
15+
# (go to https://accounts.adafruit.com to find your username)
16+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
1217

1318
# Create an instance of the REST client.
1419
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

examples/simple.py renamed to examples/api/simple.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
from Adafruit_IO import Client
77

88
# Set to your Adafruit IO key.
9-
ADAFRUIT_IO_USERNAME = 'YOUR ADAFRUIT IO USERNAME'
10-
ADAFRUIT_IO_KEY = 'YOUR ADAFRUIT IO KEY'
9+
# Remember, your key is a secret,
10+
# so make sure not to publish it when you publish this code!
11+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
12+
13+
# Set to your Adafruit IO username.
14+
# (go to https://accounts.adafruit.com to find your username)
15+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
1116

1217
# Create an instance of the REST client.
1318
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

examples/basics/digital_in.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
'adafruitio_06_digital_in.py'
3+
==================================
4+
Example of sending button values
5+
to an Adafruit IO feed.
6+
7+
Author(s): Brent Rubell, Todd Treece
8+
"""
9+
# Import standard python modules
10+
import time
11+
12+
# import Adafruit Blinka
13+
import board
14+
import digitalio
15+
16+
# import Adafruit IO REST client.
17+
from Adafruit_IO import Client, Feed, RequestError
18+
19+
# Set to your Adafruit IO key.
20+
# Remember, your key is a secret,
21+
# so make sure not to publish it when you publish this code!
22+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
23+
24+
# Set to your Adafruit IO username.
25+
# (go to https://accounts.adafruit.com to find your username)
26+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
27+
28+
# Create an instance of the REST client.
29+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
30+
31+
try: # if we have a 'digital' feed
32+
digital = aio.feeds('digital')
33+
except RequestError: # create a digital feed
34+
feed = Feed(name="digital")
35+
digital = aio.create_feed(feed)
36+
37+
# button set up
38+
button = digitalio.DigitalInOut(board.D12)
39+
button.direction = digitalio.Direction.INPUT
40+
button.pull = digitalio.Pull.UP
41+
button_current = 0
42+
43+
44+
while True:
45+
if not button.value:
46+
button_current = 1
47+
else:
48+
button_current = 0
49+
50+
print('Button -> ', button_current)
51+
aio.send(digital.key, button_current)
52+
53+
# avoid timeout from adafruit io
54+
time.sleep(1)

examples/basics/digital_out.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
'adafruitio_07_digital_out.py'
3+
===================================
4+
Example of turning on and off a LED
5+
from the Adafruit IO Python Client
6+
7+
Author(s): Brent Rubell, Todd Treece
8+
"""
9+
# Import standard python modules
10+
import time
11+
12+
# import Adafruit Blinka
13+
import digitalio
14+
import board
15+
16+
# import Adafruit IO REST client.
17+
from Adafruit_IO import Client, Feed, RequestError
18+
19+
# Set to your Adafruit IO key.
20+
# Remember, your key is a secret,
21+
# so make sure not to publish it when you publish this code!
22+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
23+
24+
# Set to your Adafruit IO username.
25+
# (go to https://accounts.adafruit.com to find your username)
26+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
27+
28+
# Create an instance of the REST client.
29+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
30+
31+
try: # if we have a 'digital' feed
32+
digital = aio.feeds('digital')
33+
except RequestError: # create a digital feed
34+
feed = Feed(name="digital")
35+
digital = aio.create_feed(feed)
36+
37+
# led set up
38+
led = digitalio.DigitalInOut(board.D5)
39+
led.direction = digitalio.Direction.OUTPUT
40+
41+
42+
while True:
43+
data = aio.receive(digital.key)
44+
if int(data.value) == 1:
45+
print('received <- ON\n')
46+
elif int(data.value) == 0:
47+
print('received <- OFF\n')
48+
49+
# set the LED to the feed value
50+
led.value = int(data.value)
51+
# timeout so we dont flood adafruit-io with requests
52+
time.sleep(0.5)

examples/basics/location.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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)

examples/basics/publish.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
'adafruitio_00_publish.py'
3+
==========================
4+
Publishes an incrementing
5+
value to a feed
6+
7+
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
8+
"""
9+
# Import standard python modules
10+
import time
11+
12+
# Import Adafruit IO REST client.
13+
from Adafruit_IO import Client, Feed
14+
15+
# holds the count for the feed
16+
run_count = 0
17+
18+
# Set to your Adafruit IO key.
19+
# Remember, your key is a secret,
20+
# so make sure not to publish it when you publish this code!
21+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
22+
23+
# Set to your Adafruit IO username.
24+
# (go to https://accounts.adafruit.com to find your username)
25+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
26+
27+
# Create an instance of the REST client.
28+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
29+
30+
# Create a new feed named 'counter'
31+
feed = Feed(name="Counter")
32+
response = aio.create_feed(feed)
33+
34+
35+
while True:
36+
print('sending count: ', run_count)
37+
run_count += 1
38+
aio.send_data('counter', run_count)
39+
# Adafruit IO is rate-limited for publishing
40+
# so we'll need a delay for calls to aio.send_data()
41+
time.sleep(3)

examples/basics/subscribe.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
'adafruitio_01_subscribe.py'
3+
==========================
4+
Subscribes to an Adafruit IO Feed
5+
6+
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
7+
"""
8+
# Import standard python modules.
9+
import sys
10+
11+
# This example uses the MQTTClient instead of the REST client
12+
from Adafruit_IO import MQTTClient
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+
# Set to the ID of the feed to subscribe to for updates.
24+
FEED_ID = 'counter'
25+
26+
# Define callback functions which will be called when certain events happen.
27+
def connected(client):
28+
"""Connected function will be called when the client is connected to
29+
Adafruit IO.This is a good place to subscribe to feed changes. The client
30+
parameter passed to this function is the Adafruit IO MQTT client so you
31+
can make calls against it easily.
32+
"""
33+
# Subscribe to changes on a feed named Counter.
34+
print('Subscribing to Feed {0}'.format(FEED_ID))
35+
client.subscribe(FEED_ID)
36+
print('Waiting for feed data...')
37+
38+
def disconnected(client):
39+
"""Disconnected function will be called when the client disconnects."""
40+
sys.exit(1)
41+
42+
def message(client, feed_id, payload):
43+
"""Message function will be called when a subscribed feed has a new value.
44+
The feed_id parameter identifies the feed, and the payload parameter has
45+
the new value.
46+
"""
47+
print('Feed {0} received new value: {1}'.format(feed_id, payload))
48+
49+
50+
# Create an MQTT client instance.
51+
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
52+
53+
# Setup the callback functions defined above.
54+
client.on_connect = connected
55+
client.on_disconnect = disconnected
56+
client.on_message = message
57+
58+
# Connect to the Adafruit IO server.
59+
client.connect()
60+
61+
# The first option is to run a thread in the background so you can continue
62+
# doing things in your program.
63+
client.loop_blocking()

0 commit comments

Comments
 (0)