Compare commits
4 commits
master
...
mqtt-group
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad168618a7 | ||
|
|
48b406df0e | ||
|
|
f8c6b0c56a | ||
|
|
6be7255f5f |
24 changed files with 1304 additions and 1226 deletions
|
|
@ -157,7 +157,7 @@ class Client(object):
|
|||
Returns a Data instance with details about the newly appended row of data.
|
||||
Note that send_data now operates the same as append.
|
||||
|
||||
:param string feed: Name/Key/ID of Adafruit IO feed.
|
||||
:param string feed: Key/ID of Adafruit IO feed.
|
||||
:param string value: Value to send.
|
||||
:param dict metadata: Optional metadata associated with the value.
|
||||
:param int precision: Optional amount of precision points to send.
|
||||
|
|
@ -325,7 +325,7 @@ class Client(object):
|
|||
Returns a Data instance with details about the newly
|
||||
appended row of data.
|
||||
|
||||
:param string feed: Name/Key/ID of Adafruit IO feed.
|
||||
:param string feed: Key/ID of Adafruit IO feed.
|
||||
:param Data data: Instance of the Data class. Must have a value property set.
|
||||
"""
|
||||
path = "feeds/{0}/data".format(feed)
|
||||
|
|
@ -345,7 +345,7 @@ class Client(object):
|
|||
"""Retrieve a list of all feeds, or the specified feed. If feed is not
|
||||
specified a list of all feeds will be returned.
|
||||
|
||||
:param string feed: Name/Key/ID of Adafruit IO feed, defaults to None.
|
||||
:param string feed: Key of Adafruit IO feed, defaults to None.
|
||||
"""
|
||||
if feed is None:
|
||||
path = "feeds"
|
||||
|
|
@ -356,10 +356,11 @@ class Client(object):
|
|||
def create_feed(self, feed, group_key=None):
|
||||
"""Create the specified feed.
|
||||
|
||||
:param string feed: Key of Adafruit IO feed.
|
||||
:param string feed: Name/Key of Adafruit IO feed.
|
||||
:param group_key group: Group to place new feed in.
|
||||
"""
|
||||
f = feed._asdict()
|
||||
f = feed if not isinstance(feed, str) else Feed(name=feed)
|
||||
f = f._asdict()
|
||||
del f['id'] # Don't pass id on create call
|
||||
path = "feeds/"
|
||||
if group_key is not None: # create feed in a group
|
||||
|
|
@ -370,7 +371,7 @@ class Client(object):
|
|||
def delete_feed(self, feed):
|
||||
"""Delete the specified feed.
|
||||
|
||||
:param string feed: Name/Key/ID of Adafruit IO feed.
|
||||
:param string feed: Key of Adafruit IO feed.
|
||||
"""
|
||||
path = "feeds/{0}".format(feed)
|
||||
self._delete(path)
|
||||
|
|
@ -390,10 +391,19 @@ class Client(object):
|
|||
def create_group(self, group):
|
||||
"""Create the specified group.
|
||||
|
||||
:param string group: Name/Key/ID of Adafruit IO Group.
|
||||
:param Group/string group: Group object to create, or name/key of of Adafruit IO Group.
|
||||
"""
|
||||
path = "groups/"
|
||||
return Group.from_dict(self._post(path, group._asdict()))
|
||||
return Group.from_dict(
|
||||
self._post(
|
||||
path=path,
|
||||
data=(
|
||||
group._asdict()
|
||||
if isinstance(group, Group)
|
||||
else {"name": group} if isinstance(group, str) else group
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
def delete_group(self, group):
|
||||
"""Delete the specified group.
|
||||
|
|
|
|||
|
|
@ -116,8 +116,12 @@ class MQTTClient(object):
|
|||
topic = parsed_topic[0]
|
||||
payload = msg.payload.decode('utf-8')
|
||||
elif parsed_topic[1] == 'groups':
|
||||
topic = parsed_topic[3]
|
||||
payload = msg.payload.decode('utf-8')
|
||||
if len(parsed_topic) > 3:
|
||||
topic = parsed_topic[3]
|
||||
payload = msg.payload.decode('utf-8')
|
||||
else: # Group feed
|
||||
topic = parsed_topic[2]
|
||||
payload = msg.payload.decode('utf-8')
|
||||
elif parsed_topic[2] == 'weather':
|
||||
topic = parsed_topic[4]
|
||||
payload = '' if msg.payload is None else msg.payload.decode('utf-8')
|
||||
|
|
|
|||
|
|
@ -1,52 +1,52 @@
|
|||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony Dicola, Justin Cooper
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, Data, RequestError
|
||||
import datetime
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
try:
|
||||
temperature = aio.feeds('temperature')
|
||||
except RequestError:
|
||||
feed = Feed(name="temperature")
|
||||
temperature = aio.create_feed(feed)
|
||||
|
||||
#
|
||||
# Adding data
|
||||
#
|
||||
|
||||
aio.send_data(temperature.key, 42)
|
||||
# works the same as send now
|
||||
aio.append(temperature.key, 42)
|
||||
|
||||
# setup batch data with custom created_at values
|
||||
yesterday = (datetime.datetime.today() - datetime.timedelta(1)).isoformat()
|
||||
today = datetime.datetime.now().isoformat()
|
||||
data_list = [Data(value=50, created_at=today), Data(value=33, created_at=yesterday)]
|
||||
# send batch data
|
||||
aio.send_batch_data(temperature.key, data_list)
|
||||
|
||||
#
|
||||
# Retrieving data
|
||||
#
|
||||
|
||||
data = aio.receive_next(temperature.key)
|
||||
print(data)
|
||||
|
||||
data = aio.receive(temperature.key)
|
||||
print(data)
|
||||
|
||||
data = aio.receive_previous(temperature.key)
|
||||
print(data)
|
||||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony Dicola, Justin Cooper
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, Data, RequestError
|
||||
import datetime
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
try:
|
||||
temperature = aio.feeds('temperature')
|
||||
except RequestError:
|
||||
feed = Feed(name="temperature")
|
||||
temperature = aio.create_feed(feed)
|
||||
|
||||
#
|
||||
# Adding data
|
||||
#
|
||||
|
||||
aio.send_data(temperature.key, 42)
|
||||
# works the same as send now
|
||||
aio.append(temperature.key, 42)
|
||||
|
||||
# setup batch data with custom created_at values
|
||||
yesterday = (datetime.datetime.today() - datetime.timedelta(1)).isoformat()
|
||||
today = datetime.datetime.now().isoformat()
|
||||
data_list = [Data(value=50, created_at=today), Data(value=33, created_at=yesterday)]
|
||||
# send batch data
|
||||
aio.send_batch_data(temperature.key, data_list)
|
||||
|
||||
#
|
||||
# Retrieving data
|
||||
#
|
||||
|
||||
data = aio.receive_next(temperature.key)
|
||||
print(data)
|
||||
|
||||
data = aio.receive(temperature.key)
|
||||
print(data)
|
||||
|
||||
data = aio.receive_previous(temperature.key)
|
||||
print(data)
|
||||
|
|
|
|||
|
|
@ -1,44 +1,57 @@
|
|||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony Dicola, Justin Cooper, Brent Rubell
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed
|
||||
import json
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# List all of your feeds
|
||||
print("Obtaining user's feeds...")
|
||||
feeds = aio.feeds()
|
||||
print('Feeds: ', feeds)
|
||||
|
||||
# Create a new feed
|
||||
print("Creating new feed...")
|
||||
feed = Feed(name="PythonFeed")
|
||||
response = aio.create_feed(feed)
|
||||
print("New feed: ", response)
|
||||
|
||||
# Delete a feed
|
||||
aio.delete_feed(response.key)
|
||||
|
||||
# Create feed in a group
|
||||
feed = Feed(name="PythonGroupFeed")
|
||||
group_key = "example"
|
||||
print("Creating feed in group %s"%group_key)
|
||||
response = aio.create_feed(feed, group_key)
|
||||
print("New feed: ", response)
|
||||
|
||||
# Delete a feed within a group
|
||||
print("Deleting feed within group %s"%group_key)
|
||||
aio.delete_feed(response.key)
|
||||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony Dicola, Justin Cooper, Brent Rubell, Tyeth Gundry
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed
|
||||
import json
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# List all of your feeds
|
||||
print("Obtaining user's feeds (key, name)...")
|
||||
feeds = aio.feeds()
|
||||
print([(f.key, f.name) for f in feeds])
|
||||
print("End of feeds listing.\n")
|
||||
|
||||
# Create a new feed
|
||||
print("Creating new feed...")
|
||||
feed = Feed(name="PythonFeed")
|
||||
response = aio.create_feed(feed)
|
||||
print("New feed: ", response)
|
||||
|
||||
# Delete a feed
|
||||
print("Deleting feed...", end="")
|
||||
aio.delete_feed(response.key)
|
||||
print("done.\n")
|
||||
|
||||
# Get / Create group - use aio.groups(key) and catch the error or do this:
|
||||
GROUP_KEY = "example"
|
||||
groups = aio.groups()
|
||||
group_keys = [g.key for g in groups]
|
||||
group = (
|
||||
groups[group_keys.index(GROUP_KEY)]
|
||||
if GROUP_KEY in group_keys
|
||||
else aio.create_group(GROUP_KEY)
|
||||
)
|
||||
|
||||
# Create feed in a group
|
||||
feed = Feed(name="PythonGroupFeed")
|
||||
print("Creating feed in group %s" % GROUP_KEY)
|
||||
response = aio.create_feed(feed, GROUP_KEY)
|
||||
print("New feed: ", response)
|
||||
|
||||
# Delete a feed within a group
|
||||
print("Deleting feed within group %s..." % GROUP_KEY, end="")
|
||||
aio.delete_feed(response.key)
|
||||
print("done.")
|
||||
|
|
|
|||
|
|
@ -1,40 +1,40 @@
|
|||
"""
|
||||
'location.py'
|
||||
==================================
|
||||
Example of sending metadata
|
||||
associated with a data point.
|
||||
|
||||
Author(s): Brent Rubell
|
||||
"""
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Create a location feed
|
||||
try:
|
||||
location = aio.feeds('location')
|
||||
except RequestError:
|
||||
feed = Feed(name="location")
|
||||
location = aio.create_feed(feed)
|
||||
|
||||
value = 42
|
||||
# Set metadata associated with value
|
||||
metadata = {'lat': 40.726190,
|
||||
'lon': -74.005334,
|
||||
'ele': -6,
|
||||
'created_at': None}
|
||||
|
||||
# Send location data to Adafruit IO
|
||||
aio.send_data(location.key, value, metadata)
|
||||
"""
|
||||
'location.py'
|
||||
==================================
|
||||
Example of sending metadata
|
||||
associated with a data point.
|
||||
|
||||
Author(s): Brent Rubell
|
||||
"""
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Create a location feed
|
||||
try:
|
||||
location = aio.feeds('location')
|
||||
except RequestError:
|
||||
feed = Feed(name="location")
|
||||
location = aio.create_feed(feed)
|
||||
|
||||
value = 42
|
||||
# Set metadata associated with value
|
||||
metadata = {'lat': 40.726190,
|
||||
'lon': -74.005334,
|
||||
'ele': -6,
|
||||
'created_at': None}
|
||||
|
||||
# Send location data to Adafruit IO
|
||||
aio.send_data(location.key, value, metadata)
|
||||
|
|
|
|||
|
|
@ -1,50 +1,50 @@
|
|||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, RequestError, Feed
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Assign a foo feed, if one exists already
|
||||
try:
|
||||
foo = aio.feeds('foo')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="foo")
|
||||
foo = aio.create_feed(feed)
|
||||
|
||||
# Assign a test feed, if one exists already
|
||||
try:
|
||||
test = aio.feeds('test')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="test")
|
||||
test = aio.create_feed(feed)
|
||||
|
||||
# Send a value to the feed 'Test'.
|
||||
aio.send_data(test.key, 42)
|
||||
|
||||
# Send a string value 'bar' to the feed 'Foo'.
|
||||
aio.send_data(foo.key, 'bar')
|
||||
|
||||
# Now read the most recent value from the feed 'Test'. Notice that it comes
|
||||
# back as a string and should be converted to an int if performing calculations
|
||||
# on it.
|
||||
data = aio.receive(test.key)
|
||||
print('Retrieved value from Test has attributes: {0}'.format(data))
|
||||
print('Latest value from Test: {0}'.format(data.value))
|
||||
|
||||
# Finally read the most revent value from feed 'Foo'.
|
||||
data = aio.receive(foo.key)
|
||||
print('Retrieved value from Foo has attributes: {0}'.format(data))
|
||||
print('Latest value from Foo: {0}'.format(data.value))
|
||||
# Simple example of sending and receiving values from Adafruit IO with the REST
|
||||
# API client.
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, RequestError, Feed
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Assign a foo feed, if one exists already
|
||||
try:
|
||||
foo = aio.feeds('foo')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="foo")
|
||||
foo = aio.create_feed(feed)
|
||||
|
||||
# Assign a test feed, if one exists already
|
||||
try:
|
||||
test = aio.feeds('test')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="test")
|
||||
test = aio.create_feed(feed)
|
||||
|
||||
# Send a value to the feed 'Test'.
|
||||
aio.send_data(test.key, 42)
|
||||
|
||||
# Send a string value 'bar' to the feed 'Foo'.
|
||||
aio.send_data(foo.key, 'bar')
|
||||
|
||||
# Now read the most recent value from the feed 'Test'. Notice that it comes
|
||||
# back as a string and should be converted to an int if performing calculations
|
||||
# on it.
|
||||
data = aio.receive(test.key)
|
||||
print('Retrieved value from Test has attributes: {0}'.format(data))
|
||||
print('Latest value from Test: {0}'.format(data.value))
|
||||
|
||||
# Finally read the most revent value from feed 'Foo'.
|
||||
data = aio.receive(foo.key)
|
||||
print('Retrieved value from Foo has attributes: {0}'.format(data))
|
||||
print('Latest value from Foo: {0}'.format(data.value))
|
||||
|
|
|
|||
|
|
@ -1,66 +1,66 @@
|
|||
"""
|
||||
'analog_in.py'
|
||||
==================================
|
||||
Example of sending analog sensor
|
||||
values to an Adafruit IO feed.
|
||||
|
||||
Author(s): Brent Rubell
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_MCP3xxx
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_MCP3xxx)
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import board
|
||||
import digitalio
|
||||
import busio
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# import Adafruit CircuitPython MCP3xxx library
|
||||
from adafruit_mcp3xxx.mcp3008 import MCP3008
|
||||
from adafruit_mcp3xxx.analog_in import AnalogIn
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
# Create an instance of the REST client
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'analog' feed
|
||||
analog = aio.feeds('analog')
|
||||
except RequestError: # create a analog feed
|
||||
feed = Feed(name='analog')
|
||||
analog = aio.create_feed(feed)
|
||||
|
||||
# Create an instance of the `busio.spi` class
|
||||
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
|
||||
|
||||
# create the cs (chip select)
|
||||
cs = digitalio.DigitalInOut(board.D12)
|
||||
|
||||
# create a mcp3008 object
|
||||
mcp = MCP3008(spi, cs)
|
||||
|
||||
# create an an adc (single-ended) on pin 0
|
||||
chan = AnalogIn(mcp, MCP3008.pin_0)
|
||||
|
||||
while True:
|
||||
sensor_data = chan.value
|
||||
|
||||
print('Analog Data -> ', sensor_data)
|
||||
aio.send(analog.key, sensor_data)
|
||||
|
||||
# avoid timeout from adafruit io
|
||||
time.sleep(0.5)
|
||||
"""
|
||||
'analog_in.py'
|
||||
==================================
|
||||
Example of sending analog sensor
|
||||
values to an Adafruit IO feed.
|
||||
|
||||
Author(s): Brent Rubell
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_MCP3xxx
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_MCP3xxx)
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import board
|
||||
import digitalio
|
||||
import busio
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# import Adafruit CircuitPython MCP3xxx library
|
||||
from adafruit_mcp3xxx.mcp3008 import MCP3008
|
||||
from adafruit_mcp3xxx.analog_in import AnalogIn
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
# Create an instance of the REST client
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'analog' feed
|
||||
analog = aio.feeds('analog')
|
||||
except RequestError: # create a analog feed
|
||||
feed = Feed(name='analog')
|
||||
analog = aio.create_feed(feed)
|
||||
|
||||
# Create an instance of the `busio.spi` class
|
||||
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
|
||||
|
||||
# create the cs (chip select)
|
||||
cs = digitalio.DigitalInOut(board.D12)
|
||||
|
||||
# create a mcp3008 object
|
||||
mcp = MCP3008(spi, cs)
|
||||
|
||||
# create an an adc (single-ended) on pin 0
|
||||
chan = AnalogIn(mcp, MCP3008.pin_0)
|
||||
|
||||
while True:
|
||||
sensor_data = chan.value
|
||||
|
||||
print('Analog Data -> ', sensor_data)
|
||||
aio.send(analog.key, sensor_data)
|
||||
|
||||
# avoid timeout from adafruit io
|
||||
time.sleep(0.5)
|
||||
|
|
|
|||
|
|
@ -1,54 +1,54 @@
|
|||
"""
|
||||
'digital_in.py'
|
||||
==================================
|
||||
Example of sending button values
|
||||
to an Adafruit IO feed.
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'digital' feed
|
||||
digital = aio.feeds('digital')
|
||||
except RequestError: # create a digital feed
|
||||
feed = Feed(name="digital")
|
||||
digital = aio.create_feed(feed)
|
||||
|
||||
# button set up
|
||||
button = digitalio.DigitalInOut(board.D12)
|
||||
button.direction = digitalio.Direction.INPUT
|
||||
button.pull = digitalio.Pull.UP
|
||||
button_current = 0
|
||||
|
||||
|
||||
while True:
|
||||
if not button.value:
|
||||
button_current = 1
|
||||
else:
|
||||
button_current = 0
|
||||
|
||||
print('Button -> ', button_current)
|
||||
aio.send(digital.key, button_current)
|
||||
|
||||
# avoid timeout from adafruit io
|
||||
"""
|
||||
'digital_in.py'
|
||||
==================================
|
||||
Example of sending button values
|
||||
to an Adafruit IO feed.
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import board
|
||||
import digitalio
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'digital' feed
|
||||
digital = aio.feeds('digital')
|
||||
except RequestError: # create a digital feed
|
||||
feed = Feed(name="digital")
|
||||
digital = aio.create_feed(feed)
|
||||
|
||||
# button set up
|
||||
button = digitalio.DigitalInOut(board.D12)
|
||||
button.direction = digitalio.Direction.INPUT
|
||||
button.pull = digitalio.Pull.UP
|
||||
button_current = 0
|
||||
|
||||
|
||||
while True:
|
||||
if not button.value:
|
||||
button_current = 1
|
||||
else:
|
||||
button_current = 0
|
||||
|
||||
print('Button -> ', button_current)
|
||||
aio.send(digital.key, button_current)
|
||||
|
||||
# avoid timeout from adafruit io
|
||||
time.sleep(1)
|
||||
|
|
@ -1,52 +1,52 @@
|
|||
"""
|
||||
'digital_out.py'
|
||||
===================================
|
||||
Example of turning on and off a LED
|
||||
from the Adafruit IO Python Client
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import digitalio
|
||||
import board
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'digital' feed
|
||||
digital = aio.feeds('digital')
|
||||
except RequestError: # create a digital feed
|
||||
feed = Feed(name="digital")
|
||||
digital = aio.create_feed(feed)
|
||||
|
||||
# led set up
|
||||
led = digitalio.DigitalInOut(board.D5)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
|
||||
while True:
|
||||
data = aio.receive(digital.key)
|
||||
if int(data.value) == 1:
|
||||
print('received <- ON\n')
|
||||
elif int(data.value) == 0:
|
||||
print('received <- OFF\n')
|
||||
|
||||
# set the LED to the feed value
|
||||
led.value = int(data.value)
|
||||
# timeout so we dont flood adafruit-io with requests
|
||||
time.sleep(0.5)
|
||||
"""
|
||||
'digital_out.py'
|
||||
===================================
|
||||
Example of turning on and off a LED
|
||||
from the Adafruit IO Python Client
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
import digitalio
|
||||
import board
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'digital' feed
|
||||
digital = aio.feeds('digital')
|
||||
except RequestError: # create a digital feed
|
||||
feed = Feed(name="digital")
|
||||
digital = aio.create_feed(feed)
|
||||
|
||||
# led set up
|
||||
led = digitalio.DigitalInOut(board.D5)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
|
||||
while True:
|
||||
data = aio.receive(digital.key)
|
||||
if int(data.value) == 1:
|
||||
print('received <- ON\n')
|
||||
elif int(data.value) == 0:
|
||||
print('received <- OFF\n')
|
||||
|
||||
# set the LED to the feed value
|
||||
led.value = int(data.value)
|
||||
# timeout so we dont flood adafruit-io with requests
|
||||
time.sleep(0.5)
|
||||
|
|
|
|||
|
|
@ -1,66 +1,66 @@
|
|||
"""
|
||||
'location.py'
|
||||
====================================
|
||||
Example of sending GPS data points
|
||||
to an Adafruit IO Feed using the API
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Assign a location feed, if one exists already
|
||||
try:
|
||||
location = aio.feeds('location')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="location")
|
||||
location = aio.create_feed(feed)
|
||||
|
||||
# limit feed updates to every 3 seconds, avoid IO throttle
|
||||
loop_delay = 5
|
||||
|
||||
# We dont' have a GPS hooked up, but let's fake it for the example/test:
|
||||
# (replace this data with values from a GPS hardware module)
|
||||
value = 0
|
||||
lat = 40.726190
|
||||
lon = -74.005334
|
||||
ele = 6 # elevation above sea level (meters)
|
||||
|
||||
|
||||
while True:
|
||||
print('\nSending Values to location feed...\n')
|
||||
print('\tValue: ', value)
|
||||
print('\tLat: ', lat)
|
||||
print('\tLon: ', lon)
|
||||
print('\tEle: ', ele)
|
||||
# Send location data to Adafruit IO
|
||||
metadata = { 'lat':lat, 'lon':lon, 'ele':ele, 'created_at':time.asctime(time.gmtime()) }
|
||||
aio.send_data(location.key,value,metadata)
|
||||
# shift all values (for test/demo purposes)
|
||||
value += 1
|
||||
lat -= 0.01
|
||||
lon += -0.02
|
||||
ele += 1
|
||||
|
||||
# Read the location data back from IO
|
||||
print('\nData Received by Adafruit IO Feed:\n')
|
||||
data = aio.receive(location.key)
|
||||
print('\tValue: {0}\n\tLat: {1}\n\tLon: {2}\n\tEle: {3}'
|
||||
.format(data.value, data.lat, data.lon, data.ele))
|
||||
# wait loop_delay seconds to avoid api throttle
|
||||
time.sleep(loop_delay)
|
||||
"""
|
||||
'location.py'
|
||||
====================================
|
||||
Example of sending GPS data points
|
||||
to an Adafruit IO Feed using the API
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Assign a location feed, if one exists already
|
||||
try:
|
||||
location = aio.feeds('location')
|
||||
except RequestError: # Doesn't exist, create a new feed
|
||||
feed = Feed(name="location")
|
||||
location = aio.create_feed(feed)
|
||||
|
||||
# limit feed updates to every 3 seconds, avoid IO throttle
|
||||
loop_delay = 5
|
||||
|
||||
# We dont' have a GPS hooked up, but let's fake it for the example/test:
|
||||
# (replace this data with values from a GPS hardware module)
|
||||
value = 0
|
||||
lat = 40.726190
|
||||
lon = -74.005334
|
||||
ele = 6 # elevation above sea level (meters)
|
||||
|
||||
|
||||
while True:
|
||||
print('\nSending Values to location feed...\n')
|
||||
print('\tValue: ', value)
|
||||
print('\tLat: ', lat)
|
||||
print('\tLon: ', lon)
|
||||
print('\tEle: ', ele)
|
||||
# Send location data to Adafruit IO
|
||||
metadata = { 'lat':lat, 'lon':lon, 'ele':ele, 'created_at':time.asctime(time.gmtime()) }
|
||||
aio.send_data(location.key,value,metadata)
|
||||
# shift all values (for test/demo purposes)
|
||||
value += 1
|
||||
lat -= 0.01
|
||||
lon += -0.02
|
||||
ele += 1
|
||||
|
||||
# Read the location data back from IO
|
||||
print('\nData Received by Adafruit IO Feed:\n')
|
||||
data = aio.receive(location.key)
|
||||
print('\tValue: {0}\n\tLat: {1}\n\tLon: {2}\n\tEle: {3}'
|
||||
.format(data.value, data.lat, data.lon, data.ele))
|
||||
# wait loop_delay seconds to avoid api throttle
|
||||
time.sleep(loop_delay)
|
||||
|
|
|
|||
|
|
@ -1,69 +1,69 @@
|
|||
"""
|
||||
`rgb_led.py`
|
||||
=======================================================================
|
||||
Control a NeoPixel RGB LED using Adafruit IO and Python
|
||||
|
||||
Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color
|
||||
|
||||
Adafruit invests time and resources providing this open source code.
|
||||
Please support Adafruit and open source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Author(s): Brent Rubell for Adafruit Industries
|
||||
Copyright (c) 2023 Adafruit Industries
|
||||
Licensed under the MIT license.
|
||||
All text above must be included in any redistribution.
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_NeoPixel
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel)
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import neopixel
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
|
||||
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
|
||||
pixel_pin = board.D18
|
||||
|
||||
# The number of NeoPixels
|
||||
num_pixels = 1
|
||||
|
||||
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
|
||||
ORDER = neopixel.GRB
|
||||
|
||||
pixels = neopixel.NeoPixel(
|
||||
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
|
||||
)
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'color' feed
|
||||
color = aio.feeds('color')
|
||||
except RequestError: # create an `color` feed
|
||||
feed = Feed(name='color')
|
||||
color = aio.create_feed(feed)
|
||||
|
||||
while True:
|
||||
# get the value of the Adafruit IO `color` feed
|
||||
color_val = aio.receive(color.key)
|
||||
# Print hex value
|
||||
print('Received Color HEX: ', color_val)
|
||||
pixels.fill(color_val.value)
|
||||
pixels.show()
|
||||
|
||||
# let's sleep/wait so we don't flood adafruit io's servers with requests
|
||||
time.sleep(3)
|
||||
"""
|
||||
`rgb_led.py`
|
||||
=======================================================================
|
||||
Control a NeoPixel RGB LED using Adafruit IO and Python
|
||||
|
||||
Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color
|
||||
|
||||
Adafruit invests time and resources providing this open source code.
|
||||
Please support Adafruit and open source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Author(s): Brent Rubell for Adafruit Industries
|
||||
Copyright (c) 2023 Adafruit Industries
|
||||
Licensed under the MIT license.
|
||||
All text above must be included in any redistribution.
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_NeoPixel
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel)
|
||||
"""
|
||||
import time
|
||||
import board
|
||||
import neopixel
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
|
||||
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
|
||||
pixel_pin = board.D18
|
||||
|
||||
# The number of NeoPixels
|
||||
num_pixels = 1
|
||||
|
||||
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
|
||||
ORDER = neopixel.GRB
|
||||
|
||||
pixels = neopixel.NeoPixel(
|
||||
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
|
||||
)
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'color' feed
|
||||
color = aio.feeds('color')
|
||||
except RequestError: # create an `color` feed
|
||||
feed = Feed(name='color')
|
||||
color = aio.create_feed(feed)
|
||||
|
||||
while True:
|
||||
# get the value of the Adafruit IO `color` feed
|
||||
color_val = aio.receive(color.key)
|
||||
# Print hex value
|
||||
print('Received Color HEX: ', color_val)
|
||||
pixels.fill(color_val.value)
|
||||
pixels.show()
|
||||
|
||||
# let's sleep/wait so we don't flood adafruit io's servers with requests
|
||||
time.sleep(3)
|
||||
|
|
|
|||
|
|
@ -1,62 +1,62 @@
|
|||
"""
|
||||
'pi_camera.py'
|
||||
=======================================
|
||||
Example for sending pictures taken by
|
||||
a Raspberry Pi camera to an
|
||||
Adafruit IO feed.
|
||||
"""
|
||||
# import standard python modules
|
||||
import time
|
||||
import base64
|
||||
import os
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# import raspberry pi camera module
|
||||
import picamera
|
||||
|
||||
# camera capture interval, in seconds
|
||||
CAMERA_INTERVAL = 3
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Adafruit IO Pi Camera Feed
|
||||
# note: this feed requires its history to be
|
||||
# turned off from the adafruit io feed page
|
||||
picam_feed = aio.feeds('picam')
|
||||
|
||||
# set up picamera
|
||||
camera = picamera.PiCamera()
|
||||
|
||||
# set the resolution of the camera's captures
|
||||
# note: Adafruit IO feeds with history turned
|
||||
# OFF only support images < 1kb
|
||||
camera.resolution = (200, 200)
|
||||
|
||||
print('Adafruit IO: Raspberry Pi Camera Example')
|
||||
|
||||
while True:
|
||||
camera.capture('image.jpg')
|
||||
print('Camera: SNAP!')
|
||||
with open("image.jpg", "rb") as imageFile:
|
||||
image = base64.b64encode(imageFile.read())
|
||||
# encode the b64 bytearray as a string for adafruit-io
|
||||
image_string = image.decode("utf-8")
|
||||
try:
|
||||
aio.send(picam_feed.key, image_string)
|
||||
print('Picture sent to Adafruit IO')
|
||||
except:
|
||||
print('Sending to Adafruit IO Failed...')
|
||||
|
||||
time.sleep(CAMERA_INTERVAL)
|
||||
"""
|
||||
'pi_camera.py'
|
||||
=======================================
|
||||
Example for sending pictures taken by
|
||||
a Raspberry Pi camera to an
|
||||
Adafruit IO feed.
|
||||
"""
|
||||
# import standard python modules
|
||||
import time
|
||||
import base64
|
||||
import os
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# import raspberry pi camera module
|
||||
import picamera
|
||||
|
||||
# camera capture interval, in seconds
|
||||
CAMERA_INTERVAL = 3
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Adafruit IO Pi Camera Feed
|
||||
# note: this feed requires its history to be
|
||||
# turned off from the adafruit io feed page
|
||||
picam_feed = aio.feeds('picam')
|
||||
|
||||
# set up picamera
|
||||
camera = picamera.PiCamera()
|
||||
|
||||
# set the resolution of the camera's captures
|
||||
# note: Adafruit IO feeds with history turned
|
||||
# OFF only support images < 1kb
|
||||
camera.resolution = (200, 200)
|
||||
|
||||
print('Adafruit IO: Raspberry Pi Camera Example')
|
||||
|
||||
while True:
|
||||
camera.capture('image.jpg')
|
||||
print('Camera: SNAP!')
|
||||
with open("image.jpg", "rb") as imageFile:
|
||||
image = base64.b64encode(imageFile.read())
|
||||
# encode the b64 bytearray as a string for adafruit-io
|
||||
image_string = image.decode("utf-8")
|
||||
try:
|
||||
aio.send(picam_feed.key, image_string)
|
||||
print('Picture sent to Adafruit IO')
|
||||
except:
|
||||
print('Sending to Adafruit IO Failed...')
|
||||
|
||||
time.sleep(CAMERA_INTERVAL)
|
||||
|
|
|
|||
|
|
@ -1,40 +1,40 @@
|
|||
"""
|
||||
'publish.py'
|
||||
=========================================
|
||||
Publishes an incrementing value to a feed
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed
|
||||
|
||||
# holds the count for the feed
|
||||
run_count = 0
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Create a new feed named 'counter'
|
||||
feed = Feed(name="Counter")
|
||||
response = aio.create_feed(feed)
|
||||
|
||||
|
||||
while True:
|
||||
print('sending count: ', run_count)
|
||||
run_count += 1
|
||||
aio.send_data('counter', run_count)
|
||||
# Adafruit IO is rate-limited for publishing
|
||||
# so we'll need a delay for calls to aio.send_data()
|
||||
time.sleep(3)
|
||||
"""
|
||||
'publish.py'
|
||||
=========================================
|
||||
Publishes an incrementing value to a feed
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
# Import standard python modules
|
||||
import time
|
||||
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed
|
||||
|
||||
# holds the count for the feed
|
||||
run_count = 0
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Create a new feed named 'counter'
|
||||
feed = Feed(name="Counter")
|
||||
response = aio.create_feed(feed)
|
||||
|
||||
|
||||
while True:
|
||||
print('sending count: ', run_count)
|
||||
run_count += 1
|
||||
aio.send_data('counter', run_count)
|
||||
# Adafruit IO is rate-limited for publishing
|
||||
# so we'll need a delay for calls to aio.send_data()
|
||||
time.sleep(3)
|
||||
|
|
|
|||
|
|
@ -1,98 +1,98 @@
|
|||
"""
|
||||
`rgb_led.py`
|
||||
=======================================================================
|
||||
Control a RGB LED using
|
||||
Adafruit IO and Python
|
||||
|
||||
Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color
|
||||
|
||||
Adafruit invests time and resources providing this open source code.
|
||||
Please support Adafruit and open source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Author(s): Brent Rubell for Adafruit Industries
|
||||
Copyright (c) 2018 Adafruit Industries
|
||||
Licensed under the MIT license.
|
||||
All text above must be included in any redistribution.
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_PCA9685
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_PCA9685)
|
||||
"""
|
||||
# import system libraries
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
from board import SCL, SDA
|
||||
from busio import I2C
|
||||
|
||||
# import the PCA9685 module.
|
||||
from adafruit_pca9685 import PCA9685
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# PWM Pins
|
||||
RED_PIN = 6
|
||||
GREEN_PIN = 5
|
||||
BLUE_PIN = 4
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'color' feed
|
||||
color = aio.feeds('color')
|
||||
except RequestError: # create an `color` feed
|
||||
feed = Feed(name='color')
|
||||
color = aio.create_feed(feed)
|
||||
|
||||
# Create the I2C bus interface.
|
||||
i2c_bus = I2C(SCL, SDA)
|
||||
|
||||
# Create a simple PCA9685 class instance.
|
||||
pca = PCA9685(i2c_bus)
|
||||
pca.frequency = 60
|
||||
prev_color = '#000000'
|
||||
|
||||
def map_range(x, in_min, in_max, out_min, out_max):
|
||||
"""re-maps a number from one range to another."""
|
||||
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
|
||||
if out_min <= out_max:
|
||||
return max(min(mapped, out_max), out_min)
|
||||
return min(max(mapped, out_max), out_min)
|
||||
|
||||
while True:
|
||||
# grab the `color` feed
|
||||
color_val = aio.receive(color.key)
|
||||
if color_val != prev_color:
|
||||
# print rgb values and hex value
|
||||
print('Received Color: ')
|
||||
red = aio.to_red(color_val.value)
|
||||
print('\t - R: ', red)
|
||||
green = aio.to_green(color_val.value)
|
||||
print('\t - G: ', green)
|
||||
blue = aio.to_blue(color_val.value)
|
||||
print('\t - B: ', blue)
|
||||
print('\t - HEX: ', color_val.value)
|
||||
# map color values (0-255) to 16-bit values for the pca
|
||||
red = map_range(int(red), 0, 255, 0, 65535)
|
||||
green = map_range(int(green), 0, 255, 0, 65535)
|
||||
blue = map_range(int(blue), 0, 255, 0, 65535)
|
||||
# invert RGB values for common anode LEDs.
|
||||
pca.channels[RED_PIN].duty_cycle = 65535 - int(red)
|
||||
pca.channels[GREEN_PIN].duty_cycle = 65535 - int(green)
|
||||
pca.channels[BLUE_PIN].duty_cycle = 65535 - int(blue)
|
||||
prev_color = color_val
|
||||
# let's wait a bit so we don't flood adafruit io's servers...
|
||||
time.sleep(1)
|
||||
"""
|
||||
`rgb_led.py`
|
||||
=======================================================================
|
||||
Control a RGB LED using
|
||||
Adafruit IO and Python
|
||||
|
||||
Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-color
|
||||
|
||||
Adafruit invests time and resources providing this open source code.
|
||||
Please support Adafruit and open source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Author(s): Brent Rubell for Adafruit Industries
|
||||
Copyright (c) 2018 Adafruit Industries
|
||||
Licensed under the MIT license.
|
||||
All text above must be included in any redistribution.
|
||||
|
||||
Dependencies:
|
||||
- Adafruit_Blinka
|
||||
(https://github.com/adafruit/Adafruit_Blinka)
|
||||
- Adafruit_CircuitPython_PCA9685
|
||||
(https://github.com/adafruit/Adafruit_CircuitPython_PCA9685)
|
||||
"""
|
||||
# import system libraries
|
||||
import time
|
||||
|
||||
# import Adafruit Blinka
|
||||
from board import SCL, SDA
|
||||
from busio import I2C
|
||||
|
||||
# import the PCA9685 module.
|
||||
from adafruit_pca9685 import PCA9685
|
||||
|
||||
# import Adafruit IO REST client
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# PWM Pins
|
||||
RED_PIN = 6
|
||||
GREEN_PIN = 5
|
||||
BLUE_PIN = 4
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'color' feed
|
||||
color = aio.feeds('color')
|
||||
except RequestError: # create an `color` feed
|
||||
feed = Feed(name='color')
|
||||
color = aio.create_feed(feed)
|
||||
|
||||
# Create the I2C bus interface.
|
||||
i2c_bus = I2C(SCL, SDA)
|
||||
|
||||
# Create a simple PCA9685 class instance.
|
||||
pca = PCA9685(i2c_bus)
|
||||
pca.frequency = 60
|
||||
prev_color = '#000000'
|
||||
|
||||
def map_range(x, in_min, in_max, out_min, out_max):
|
||||
"""re-maps a number from one range to another."""
|
||||
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
|
||||
if out_min <= out_max:
|
||||
return max(min(mapped, out_max), out_min)
|
||||
return min(max(mapped, out_max), out_min)
|
||||
|
||||
while True:
|
||||
# grab the `color` feed
|
||||
color_val = aio.receive(color.key)
|
||||
if color_val != prev_color:
|
||||
# print rgb values and hex value
|
||||
print('Received Color: ')
|
||||
red = aio.to_red(color_val.value)
|
||||
print('\t - R: ', red)
|
||||
green = aio.to_green(color_val.value)
|
||||
print('\t - G: ', green)
|
||||
blue = aio.to_blue(color_val.value)
|
||||
print('\t - B: ', blue)
|
||||
print('\t - HEX: ', color_val.value)
|
||||
# map color values (0-255) to 16-bit values for the pca
|
||||
red = map_range(int(red), 0, 255, 0, 65535)
|
||||
green = map_range(int(green), 0, 255, 0, 65535)
|
||||
blue = map_range(int(blue), 0, 255, 0, 65535)
|
||||
# invert RGB values for common anode LEDs.
|
||||
pca.channels[RED_PIN].duty_cycle = 65535 - int(red)
|
||||
pca.channels[GREEN_PIN].duty_cycle = 65535 - int(green)
|
||||
pca.channels[BLUE_PIN].duty_cycle = 65535 - int(blue)
|
||||
prev_color = color_val
|
||||
# let's wait a bit so we don't flood adafruit io's servers...
|
||||
time.sleep(1)
|
||||
|
|
|
|||
|
|
@ -1,63 +1,63 @@
|
|||
"""
|
||||
'subscribe.py'
|
||||
==========================
|
||||
Subscribes to an Adafruit IO Feed
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
|
||||
# This example uses the MQTTClient instead of the REST client
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to the ID of the feed to subscribe to for updates.
|
||||
FEED_ID = 'counter'
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
"""Connected function will be called when the client is connected to
|
||||
Adafruit IO.This is a good place to subscribe to feed changes. The client
|
||||
parameter passed to this function is the Adafruit IO MQTT client so you
|
||||
can make calls against it easily.
|
||||
"""
|
||||
# Subscribe to changes on a feed named Counter.
|
||||
print('Subscribing to Feed {0}'.format(FEED_ID))
|
||||
client.subscribe(FEED_ID)
|
||||
print('Waiting for feed data...')
|
||||
|
||||
def disconnected(client):
|
||||
"""Disconnected function will be called when the client disconnects."""
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
"""Message function will be called when a subscribed feed has a new value.
|
||||
The feed_id parameter identifies the feed, and the payload parameter has
|
||||
the new value.
|
||||
"""
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_blocking()
|
||||
"""
|
||||
'subscribe.py'
|
||||
==========================
|
||||
Subscribes to an Adafruit IO Feed
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
|
||||
# This example uses the MQTTClient instead of the REST client
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to the ID of the feed to subscribe to for updates.
|
||||
FEED_ID = 'counter'
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
"""Connected function will be called when the client is connected to
|
||||
Adafruit IO.This is a good place to subscribe to feed changes. The client
|
||||
parameter passed to this function is the Adafruit IO MQTT client so you
|
||||
can make calls against it easily.
|
||||
"""
|
||||
# Subscribe to changes on a feed named Counter.
|
||||
print('Subscribing to Feed {0}'.format(FEED_ID))
|
||||
client.subscribe(FEED_ID)
|
||||
print('Waiting for feed data...')
|
||||
|
||||
def disconnected(client):
|
||||
"""Disconnected function will be called when the client disconnects."""
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
"""Message function will be called when a subscribed feed has a new value.
|
||||
The feed_id parameter identifies the feed, and the payload parameter has
|
||||
the new value.
|
||||
"""
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_blocking()
|
||||
|
|
|
|||
|
|
@ -1,31 +1,31 @@
|
|||
"""
|
||||
`time.py`
|
||||
==========================================
|
||||
Don't have a RTC handy and need
|
||||
accurate time measurements?
|
||||
|
||||
Let Adafruit IO serve up real-time values
|
||||
based off your device's IP-address!
|
||||
|
||||
Author: Brent Rubell
|
||||
"""
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, Data, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Get the time from Adafruit IO
|
||||
time = aio.receive_time()
|
||||
# Time is returned as a `struct_time`
|
||||
# https://docs.python.org/3.7/library/time.html#time.struct_time
|
||||
"""
|
||||
`time.py`
|
||||
==========================================
|
||||
Don't have a RTC handy and need
|
||||
accurate time measurements?
|
||||
|
||||
Let Adafruit IO serve up real-time values
|
||||
based off your device's IP-address!
|
||||
|
||||
Author: Brent Rubell
|
||||
"""
|
||||
# Import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, Data, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Create an instance of the REST client.
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Get the time from Adafruit IO
|
||||
time = aio.receive_time()
|
||||
# Time is returned as a `struct_time`
|
||||
# https://docs.python.org/3.7/library/time.html#time.struct_time
|
||||
print(time)
|
||||
|
|
@ -1,87 +1,87 @@
|
|||
"""
|
||||
`type-conversion.py`
|
||||
=========================================
|
||||
Example of sending and receiving
|
||||
different data types to/from Adafruit
|
||||
IO using the Adafruit IO Python Client
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'type' feed
|
||||
type = aio.feeds('type')
|
||||
except RequestError: # create a type feed
|
||||
feed = Feed(name="type")
|
||||
type = aio.create_feed(feed)
|
||||
|
||||
# Values to send to Adafruit IO
|
||||
char_val = 'a'
|
||||
string_val = 'adafruit'
|
||||
bool_val = True
|
||||
int_val = 3
|
||||
long_val = 0x80000000
|
||||
double_val = 3.1415926535897932
|
||||
float_val = +1E6
|
||||
|
||||
|
||||
"""
|
||||
Let's send some values to our feed
|
||||
and properly receive them
|
||||
"""
|
||||
print("Sending to Adafruit IO...")
|
||||
|
||||
# Char
|
||||
print('\tSending Character: ', char_val)
|
||||
aio.send(type.key, char_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Character: ', str(data))
|
||||
|
||||
# String
|
||||
print('\n\tSending String: ', string_val)
|
||||
aio.send(type.key, string_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived String: ', str(data))
|
||||
|
||||
# Boolean
|
||||
print('\n\tSending Bool: ', bool_val)
|
||||
aio.send(type.key, bool_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Bool: ', bool(data))
|
||||
|
||||
# Integer
|
||||
print('\n\tSending Int: ', int_val)
|
||||
aio.send(type.key, int_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Int: ', int(data))
|
||||
|
||||
# Long
|
||||
print('\n\tSending Long: ', long_val)
|
||||
aio.send(type.key, long_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Long: ', int(data))
|
||||
|
||||
# Double
|
||||
print('\n\tSending Double: ', double_val)
|
||||
aio.send(type.key, double_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Double: ', float(data))
|
||||
|
||||
# Float
|
||||
print('\n\tSending Float: ', float_val)
|
||||
aio.send(type.key, float_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Float: ', float(data))
|
||||
"""
|
||||
`type-conversion.py`
|
||||
=========================================
|
||||
Example of sending and receiving
|
||||
different data types to/from Adafruit
|
||||
IO using the Adafruit IO Python Client
|
||||
|
||||
Author(s): Brent Rubell, Todd Treece for Adafruit Industries
|
||||
"""
|
||||
|
||||
# import Adafruit IO REST client.
|
||||
from Adafruit_IO import Client, Feed, RequestError
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
try: # if we have a 'type' feed
|
||||
type = aio.feeds('type')
|
||||
except RequestError: # create a type feed
|
||||
feed = Feed(name="type")
|
||||
type = aio.create_feed(feed)
|
||||
|
||||
# Values to send to Adafruit IO
|
||||
char_val = 'a'
|
||||
string_val = 'adafruit'
|
||||
bool_val = True
|
||||
int_val = 3
|
||||
long_val = 0x80000000
|
||||
double_val = 3.1415926535897932
|
||||
float_val = +1E6
|
||||
|
||||
|
||||
"""
|
||||
Let's send some values to our feed
|
||||
and properly receive them
|
||||
"""
|
||||
print("Sending to Adafruit IO...")
|
||||
|
||||
# Char
|
||||
print('\tSending Character: ', char_val)
|
||||
aio.send(type.key, char_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Character: ', str(data))
|
||||
|
||||
# String
|
||||
print('\n\tSending String: ', string_val)
|
||||
aio.send(type.key, string_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived String: ', str(data))
|
||||
|
||||
# Boolean
|
||||
print('\n\tSending Bool: ', bool_val)
|
||||
aio.send(type.key, bool_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Bool: ', bool(data))
|
||||
|
||||
# Integer
|
||||
print('\n\tSending Int: ', int_val)
|
||||
aio.send(type.key, int_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Int: ', int(data))
|
||||
|
||||
# Long
|
||||
print('\n\tSending Long: ', long_val)
|
||||
aio.send(type.key, long_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Long: ', int(data))
|
||||
|
||||
# Double
|
||||
print('\n\tSending Double: ', double_val)
|
||||
aio.send(type.key, double_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Double: ', float(data))
|
||||
|
||||
# Float
|
||||
print('\n\tSending Float: ', float_val)
|
||||
aio.send(type.key, float_val)
|
||||
data = aio.receive(type.key).value
|
||||
print('\t\tReceived Float: ', float(data))
|
||||
|
|
|
|||
|
|
@ -1,92 +1,92 @@
|
|||
# Example of using the MQTT client class to subscribe to and publish feed values.
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Import standard python modules.
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to feed changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Connected to Adafruit IO! Listening for DemoFeed changes...')
|
||||
# Subscribe to changes on a feed named DemoFeed.
|
||||
client.subscribe('DemoFeed')
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Now the program needs to use a client loop function to ensure messages are
|
||||
# sent and received. There are a few options for driving the message loop,
|
||||
# depending on what your program needs to do.
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_background()
|
||||
# Now send new values every 10 seconds.
|
||||
print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
|
||||
while True:
|
||||
value = random.randint(0, 100)
|
||||
print('Publishing {0} to DemoFeed.'.format(value))
|
||||
client.publish('DemoFeed', value)
|
||||
time.sleep(10)
|
||||
|
||||
# Another option is to pump the message loop yourself by periodically calling
|
||||
# the client loop function. Notice how the loop below changes to call loop
|
||||
# continuously while still sending a new message every 10 seconds. This is a
|
||||
# good option if you don't want to or can't have a thread pumping the message
|
||||
# loop in the background.
|
||||
#last = 0
|
||||
#print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
|
||||
#while True:
|
||||
# # Explicitly pump the message loop.
|
||||
# client.loop()
|
||||
# # Send a new message every 10 seconds.
|
||||
# if (time.time() - last) >= 10.0:
|
||||
# value = random.randint(0, 100)
|
||||
# print('Publishing {0} to DemoFeed.'.format(value))
|
||||
# client.publish('DemoFeed', value)
|
||||
# last = time.time()
|
||||
|
||||
# The last option is to just call loop_blocking. This will run a message loop
|
||||
# forever, so your program will not get past the loop_blocking call. This is
|
||||
# good for simple programs which only listen to events. For more complex programs
|
||||
# you probably need to have a background thread loop or explicit message loop like
|
||||
# the two previous examples above.
|
||||
#client.loop_blocking()
|
||||
# Example of using the MQTT client class to subscribe to and publish feed values.
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Import standard python modules.
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to feed changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Connected to Adafruit IO! Listening for DemoFeed changes...')
|
||||
# Subscribe to changes on a feed named DemoFeed.
|
||||
client.subscribe('DemoFeed')
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Now the program needs to use a client loop function to ensure messages are
|
||||
# sent and received. There are a few options for driving the message loop,
|
||||
# depending on what your program needs to do.
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_background()
|
||||
# Now send new values every 10 seconds.
|
||||
print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
|
||||
while True:
|
||||
value = random.randint(0, 100)
|
||||
print('Publishing {0} to DemoFeed.'.format(value))
|
||||
client.publish('DemoFeed', value)
|
||||
time.sleep(10)
|
||||
|
||||
# Another option is to pump the message loop yourself by periodically calling
|
||||
# the client loop function. Notice how the loop below changes to call loop
|
||||
# continuously while still sending a new message every 10 seconds. This is a
|
||||
# good option if you don't want to or can't have a thread pumping the message
|
||||
# loop in the background.
|
||||
#last = 0
|
||||
#print('Publishing a new message every 10 seconds (press Ctrl-C to quit)...')
|
||||
#while True:
|
||||
# # Explicitly pump the message loop.
|
||||
# client.loop()
|
||||
# # Send a new message every 10 seconds.
|
||||
# if (time.time() - last) >= 10.0:
|
||||
# value = random.randint(0, 100)
|
||||
# print('Publishing {0} to DemoFeed.'.format(value))
|
||||
# client.publish('DemoFeed', value)
|
||||
# last = time.time()
|
||||
|
||||
# The last option is to just call loop_blocking. This will run a message loop
|
||||
# forever, so your program will not get past the loop_blocking call. This is
|
||||
# good for simple programs which only listen to events. For more complex programs
|
||||
# you probably need to have a background thread loop or explicit message loop like
|
||||
# the two previous examples above.
|
||||
#client.loop_blocking()
|
||||
|
|
|
|||
|
|
@ -1,80 +1,80 @@
|
|||
# Example of subscribing to an Adafruit IO group
|
||||
# and publishing to the feeds within it
|
||||
|
||||
# Author: Brent Rubell for Adafruit Industries, 2018
|
||||
|
||||
# Import standard python modules.
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Group Name
|
||||
group_name = 'grouptest'
|
||||
|
||||
# Feeds within the group
|
||||
group_feed_one = 'one'
|
||||
group_feed_two = 'two'
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to topic changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Listening for changes on ', group_name)
|
||||
# Subscribe to changes on a group, `group_name`
|
||||
client.subscribe_group(group_name)
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, topic_id, payload):
|
||||
# Message function will be called when a subscribed topic has a new value.
|
||||
# The topic_id parameter identifies the topic, and the payload parameter has
|
||||
# the new value.
|
||||
print('Topic {0} received new value: {1}'.format(topic_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Now the program needs to use a client loop function to ensure messages are
|
||||
# sent and received. There are a few options for driving the message loop,
|
||||
# depending on what your program needs to do.
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_background()
|
||||
# Now send new values every 5 seconds.
|
||||
print('Publishing a new message every 5 seconds (press Ctrl-C to quit)...')
|
||||
while True:
|
||||
value = random.randint(0, 100)
|
||||
print('Publishing {0} to {1}.{2}.'.format(value, group_name, group_feed_one))
|
||||
client.publish('one', value, group_name)
|
||||
|
||||
value = random.randint(0,100)
|
||||
print('Publishing {0} to {1}.{2}.'.format(value, group_name, group_feed_two))
|
||||
client.publish('two', value, group_name)
|
||||
time.sleep(5)
|
||||
# Example of subscribing to an Adafruit IO group
|
||||
# and publishing to the feeds within it
|
||||
|
||||
# Author: Brent Rubell for Adafruit Industries, 2018
|
||||
|
||||
# Import standard python modules.
|
||||
import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
|
||||
# Group Name
|
||||
group_name = 'grouptest'
|
||||
|
||||
# Feeds within the group
|
||||
group_feed_one = 'one'
|
||||
group_feed_two = 'two'
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to topic changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Listening for changes on ', group_name)
|
||||
# Subscribe to changes on a group, `group_name`
|
||||
client.subscribe_group(group_name)
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, topic_id, payload):
|
||||
# Message function will be called when a subscribed topic has a new value.
|
||||
# The topic_id parameter identifies the topic, and the payload parameter has
|
||||
# the new value.
|
||||
print('Topic {0} received new value: {1}'.format(topic_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Now the program needs to use a client loop function to ensure messages are
|
||||
# sent and received. There are a few options for driving the message loop,
|
||||
# depending on what your program needs to do.
|
||||
|
||||
# The first option is to run a thread in the background so you can continue
|
||||
# doing things in your program.
|
||||
client.loop_background()
|
||||
# Now send new values every 5 seconds.
|
||||
print('Publishing a new message every 5 seconds (press Ctrl-C to quit)...')
|
||||
while True:
|
||||
value = random.randint(0, 100)
|
||||
print('Publishing {0} to {1}.{2}.'.format(value, group_name, group_feed_one))
|
||||
client.publish('one', value, group_name)
|
||||
|
||||
value = random.randint(0,100)
|
||||
print('Publishing {0} to {1}.{2}.'.format(value, group_name, group_feed_two))
|
||||
client.publish('two', value, group_name)
|
||||
time.sleep(5)
|
||||
|
|
|
|||
|
|
@ -16,15 +16,15 @@ import random
|
|||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Shared IO Feed
|
||||
# Make sure you have read AND write access to this feed to publish.
|
||||
IO_FEED = 'SHARED_AIO_FEED_NAME'
|
||||
|
|
|
|||
|
|
@ -1,65 +1,65 @@
|
|||
# Example of using the MQTT client class to subscribe to a feed and print out
|
||||
# any changes made to the feed. Edit the variables below to configure the key,
|
||||
# username, and feed to subscribe to for changes.
|
||||
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to the ID of the feed to subscribe to for updates.
|
||||
FEED_ID = 'DemoFeed'
|
||||
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to feed changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Connected to Adafruit IO! Listening for {0} changes...'.format(FEED_ID))
|
||||
# Subscribe to changes on a feed named DemoFeed.
|
||||
client.subscribe(FEED_ID)
|
||||
|
||||
def subscribe(client, userdata, mid, granted_qos):
|
||||
# This method is called when the client subscribes to a new feed.
|
||||
print('Subscribed to {0} with QoS {1}'.format(FEED_ID, granted_qos[0]))
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
client.on_subscribe = subscribe
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Start a message loop that blocks forever waiting for MQTT messages to be
|
||||
# received. Note there are other options for running the event loop like doing
|
||||
# so in a background thread--see the mqtt_client.py example to learn more.
|
||||
client.loop_blocking()
|
||||
# Example of using the MQTT client class to subscribe to a feed and print out
|
||||
# any changes made to the feed. Edit the variables below to configure the key,
|
||||
# username, and feed to subscribe to for changes.
|
||||
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to the ID of the feed to subscribe to for updates.
|
||||
FEED_ID = 'DemoFeed'
|
||||
|
||||
|
||||
# Define callback functions which will be called when certain events happen.
|
||||
def connected(client):
|
||||
# Connected function will be called when the client is connected to Adafruit IO.
|
||||
# This is a good place to subscribe to feed changes. The client parameter
|
||||
# passed to this function is the Adafruit IO MQTT client so you can make
|
||||
# calls against it easily.
|
||||
print('Connected to Adafruit IO! Listening for {0} changes...'.format(FEED_ID))
|
||||
# Subscribe to changes on a feed named DemoFeed.
|
||||
client.subscribe(FEED_ID)
|
||||
|
||||
def subscribe(client, userdata, mid, granted_qos):
|
||||
# This method is called when the client subscribes to a new feed.
|
||||
print('Subscribed to {0} with QoS {1}'.format(FEED_ID, granted_qos[0]))
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create an MQTT client instance.
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_connect = connected
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
client.on_subscribe = subscribe
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Start a message loop that blocks forever waiting for MQTT messages to be
|
||||
# received. Note there are other options for running the event loop like doing
|
||||
# so in a background thread--see the mqtt_client.py example to learn more.
|
||||
client.loop_blocking()
|
||||
|
|
|
|||
|
|
@ -1,61 +1,61 @@
|
|||
"""
|
||||
mqtt_time.py
|
||||
============================================
|
||||
example of utilizing MQTT time topics to grab
|
||||
the time from the Adafruit IO server.
|
||||
|
||||
Author: Brent Rubell
|
||||
"""
|
||||
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('\t Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create a SECURE MQTT client instance
|
||||
# Note: This client will default to secure, an optional parameter can be added
|
||||
# to make it insecure, comment out the below line
|
||||
# client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY, secure=False)
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Subscribe to the time feeds
|
||||
print('* Subscribing to time/seconds')
|
||||
client.subscribe_time('seconds')
|
||||
|
||||
print('* Subscribing to time/millis')
|
||||
client.subscribe_time('millis')
|
||||
|
||||
print('* Subscribing to time/ISO-8601')
|
||||
client.subscribe_time('iso')
|
||||
|
||||
client.loop_blocking()
|
||||
"""
|
||||
mqtt_time.py
|
||||
============================================
|
||||
example of utilizing MQTT time topics to grab
|
||||
the time from the Adafruit IO server.
|
||||
|
||||
Author: Brent Rubell
|
||||
"""
|
||||
|
||||
# Import standard python modules.
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Import Adafruit IO MQTT client.
|
||||
from Adafruit_IO import MQTTClient
|
||||
|
||||
# Set to your Adafruit IO username.
|
||||
# (go to https://accounts.adafruit.com to find your username)
|
||||
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
|
||||
|
||||
# Set to your Adafruit IO key.
|
||||
# Remember, your key is a secret,
|
||||
# so make sure not to publish it when you publish this code!
|
||||
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
|
||||
|
||||
def disconnected(client):
|
||||
# Disconnected function will be called when the client disconnects.
|
||||
print('Disconnected from Adafruit IO!')
|
||||
sys.exit(1)
|
||||
|
||||
def message(client, feed_id, payload):
|
||||
# Message function will be called when a subscribed feed has a new value.
|
||||
# The feed_id parameter identifies the feed, and the payload parameter has
|
||||
# the new value.
|
||||
print('\t Feed {0} received new value: {1}'.format(feed_id, payload))
|
||||
|
||||
|
||||
# Create a SECURE MQTT client instance
|
||||
# Note: This client will default to secure, an optional parameter can be added
|
||||
# to make it insecure, comment out the below line
|
||||
# client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY, secure=False)
|
||||
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
||||
|
||||
# Setup the callback functions defined above.
|
||||
client.on_disconnect = disconnected
|
||||
client.on_message = message
|
||||
|
||||
# Connect to the Adafruit IO server.
|
||||
client.connect()
|
||||
|
||||
# Subscribe to the time feeds
|
||||
print('* Subscribing to time/seconds')
|
||||
client.subscribe_time('seconds')
|
||||
|
||||
print('* Subscribing to time/millis')
|
||||
client.subscribe_time('millis')
|
||||
|
||||
print('* Subscribing to time/ISO-8601')
|
||||
client.subscribe_time('iso')
|
||||
|
||||
client.loop_blocking()
|
||||
|
|
|
|||
|
|
@ -155,6 +155,26 @@ class TestClient(base.IOTestCase):
|
|||
result = aio.create_data('testfeed', data)
|
||||
self.assertEqual(int(result.value), 42)
|
||||
|
||||
|
||||
def test_delete_data(self):
|
||||
"""create_data
|
||||
"""
|
||||
aio = self.get_client()
|
||||
self.ensure_feed_deleted(aio, 'testfeed')
|
||||
test_feed = aio.create_feed(Feed(name="TestFeed"))
|
||||
aio.send_data('testfeed', 1) # Make sure TestFeed exists.
|
||||
data = Data(value=42)
|
||||
result = aio.create_data('testfeed', data)
|
||||
self.assertEqual(int(result.value), 42)
|
||||
data = Data(value=43)
|
||||
second_result = aio.create_data(test_feed.id, data)
|
||||
self.assertEqual(int(second_result.value), 43)
|
||||
aio.delete('testfeed', result.id)
|
||||
aio.delete(test_feed.id, second_result.id)
|
||||
latest_data = aio.receive('testfeed')
|
||||
self.assertEqual(int(latest_data.value), 1)
|
||||
|
||||
|
||||
def test_location_data(self):
|
||||
"""receive_location
|
||||
"""
|
||||
|
|
@ -227,13 +247,19 @@ class TestClient(base.IOTestCase):
|
|||
result = io.append(feed.key, 42)
|
||||
self.assertEqual(int(result.value), 42)
|
||||
|
||||
def test_create_feed(self):
|
||||
def test_create_feed_as_obj(self):
|
||||
io = self.get_client()
|
||||
self.ensure_feed_deleted(io, 'testfeed')
|
||||
feed = Feed(name='testfeed')
|
||||
result = io.create_feed(feed)
|
||||
self.assertEqual(result.name, 'testfeed')
|
||||
|
||||
def test_create_feed_as_str(self):
|
||||
io = self.get_client()
|
||||
self.ensure_feed_deleted(io, 'testfeed')
|
||||
result = io.create_feed('testfeed')
|
||||
self.assertEqual(result.name, 'testfeed')
|
||||
|
||||
def test_create_feed_in_group(self):
|
||||
"""Tests creating a feed within a group.
|
||||
|
||||
|
|
@ -289,6 +315,15 @@ class TestClient(base.IOTestCase):
|
|||
self.assertTrue('grouptest' in names)
|
||||
|
||||
def test_groups_retrieves_requested_group(self):
|
||||
io = self.get_client()
|
||||
self.ensure_group_deleted(io, 'grouptest')
|
||||
expected_response = io.create_group(Group(name='grouptest'))
|
||||
response = io.groups('grouptest')
|
||||
self.assertEqual(response.name, expected_response.name)
|
||||
self.assertEqual(response.key, expected_response.key)
|
||||
self.assertEqual(response.id, expected_response.id)
|
||||
|
||||
def test_create_groups_retrieves_requested_group(self):
|
||||
io = self.get_client()
|
||||
self.ensure_group_deleted(io, 'grouptest')
|
||||
response = io.create_group(Group(name='grouptest'))
|
||||
|
|
@ -302,6 +337,13 @@ class TestClient(base.IOTestCase):
|
|||
io.delete_group('groupdeletetest')
|
||||
self.assertRaises(RequestError, io.groups, 'groupdeletetest')
|
||||
|
||||
def test_create_group_by_name(self):
|
||||
io = self.get_client()
|
||||
self.ensure_group_deleted(io, 'grouprx')
|
||||
group = io.create_group('grouprx')
|
||||
response = io.groups(group.name)
|
||||
self.assertEqual(response.name, 'grouprx')
|
||||
|
||||
def test_receive_group_by_name(self):
|
||||
io = self.get_client()
|
||||
self.ensure_group_deleted(io, 'grouprx')
|
||||
|
|
|
|||
|
|
@ -1,41 +1,50 @@
|
|||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from Adafruit_IO import Client, RequestError, ThrottlingError
|
||||
import base
|
||||
|
||||
|
||||
class TestErrors(base.IOTestCase):
|
||||
|
||||
def test_request_error_from_bad_key(self):
|
||||
io = Client("test_user", "this is a bad key from a test")
|
||||
with self.assertRaises(RequestError):
|
||||
io.send("TestStream", 42)
|
||||
|
||||
@unittest.skip("Throttling test must be run in isolation to prevent other tests from failing.")
|
||||
def test_throttling_error_after_6_requests_in_short_period(self):
|
||||
io = Client(self.get_test_key())
|
||||
with self.assertRaises(ThrottlingError):
|
||||
for i in range(6):
|
||||
io.send("TestStream", 42)
|
||||
time.sleep(0.1) # Small delay to keep from hammering network.
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from Adafruit_IO import Client, RequestError, ThrottlingError
|
||||
import base
|
||||
|
||||
|
||||
class TestErrors(base.IOTestCase):
|
||||
|
||||
def test_request_error_from_bad_key(self):
|
||||
# the test username is possibly blocked / deleted, so gives a different error
|
||||
io = Client("test_user", "this is a bad key from a test")
|
||||
with self.assertRaises(RequestError) as error_context:
|
||||
io.send("TestStream", 42)
|
||||
self.assertRegex(error_context.exception.args[0], ".*(that username|invalid API key provided).*")
|
||||
|
||||
@unittest.skip("Throttling test must be run in isolation to prevent other tests from failing.")
|
||||
def test_throttling_error_after_6_requests_in_short_period(self):
|
||||
io = Client(self.get_test_username(), self.get_test_key())
|
||||
try:
|
||||
feed_key = [f.key for f in io.feeds() if f.name == "TestStream"][0]
|
||||
io.delete_feed(feed_key)
|
||||
except ValueError:
|
||||
pass
|
||||
new_feed = io.create_feed("TestStream")
|
||||
with self.assertRaises(ThrottlingError):
|
||||
for i in range(60):
|
||||
io.send(new_feed.key, 42)
|
||||
time.sleep(0.1) # Small delay to keep from hammering network.
|
||||
io.delete_feed(new_feed.key)
|
||||
|
|
|
|||
Loading…
Reference in a new issue