Skip to content

Commit 7cc98d2

Browse files
author
brentru
committed
Adding publishing location/metadata via csv topic, add example
1 parent 566b6cf commit 7cc98d2

File tree

2 files changed

+131
-4
lines changed

2 files changed

+131
-4
lines changed

adafruit_io/adafruit_io.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def _on_message_mqtt(self, client, topic, payload):
148148
if self.on_message is not None:
149149
# Parse the MQTT topic string
150150
topic_name = topic.split("/")
151-
print(topic_name)
152151
if topic_name[1] == "groups":
153152
# Adafruit IO Group Feed(s)
154153
feeds = []
@@ -256,7 +255,7 @@ def subscribe_to_time(self, time_type):
256255
Information about these topics can be found on the Adafruit IO MQTT API Docs.:
257256
https://io.adafruit.com/api/docs/mqtt.html#time-topics
258257
"""
259-
if "seconds" or "millis" in time_type:
258+
if "seconds" or "millis" or "hours" in time_type:
260259
self._client.subscribe("time/" + time_type)
261260
elif time_type == "iso":
262261
self._client.subscribe("time/ISO-8601")
@@ -319,12 +318,13 @@ def publish_multiple(self, feeds_and_data, timeout=3, is_group=False):
319318
self.publish(t, d)
320319
time.sleep(timeout)
321320

322-
def publish(self, feed_key, data, shared_user=None, is_group=False):
321+
def publish(self, feed_key, data, metadata = None, shared_user=None, is_group=False):
323322
"""Publishes to an An Adafruit IO Feed.
324323
:param str feed_key: Adafruit IO Feed key.
325324
:param str data: Data to publish to the feed or group.
326325
:param int data: Data to publish to the feed or group.
327326
:param float data: Data to publish to the feed or group.
327+
:param str metadata: Optional metadata associated with the data.
328328
:param str shared_user: Owner of the Adafruit IO feed, required for
329329
feed sharing.
330330
:param bool is_group: Set True if publishing to an Adafruit IO Group.
@@ -353,16 +353,29 @@ def publish(self, feed_key, data, shared_user=None, is_group=False):
353353
..code-block:: python
354354
355355
client.publish('temperature', shared_user='myfriend')
356+
357+
Example of publishing a value along with locational metadata to a feed.
358+
..code-block:: python
359+
360+
data = 42
361+
# format: "lat, lon, ele"
362+
metadata = "40.726190, -74.005334, -6"
363+
io.publish("location-feed", data, metadata)
356364
357365
"""
358366
if is_group:
359367
self._client.publish("{0}/groups/{1}".format(self._user, feed_key), data)
360-
return
361368
if shared_user is not None:
362369
self._client.publish("{0}/feeds/{1}".format(shared_user, feed_key), data)
370+
if metadata is not None:
371+
if isinstance(data, int or float):
372+
data = str(data)
373+
csv_string = data + "," + metadata
374+
self._client.publish("{0}/feeds/{1}/csv".format(self._user, feed_key), csv_string)
363375
else:
364376
self._client.publish("{0}/feeds/{1}".format(self._user, feed_key), data)
365377

378+
366379
def get(self, feed_key):
367380
"""Calling this method will make Adafruit IO publish the most recent
368381
value on feed_key.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Example of tagging data with location values
2+
# and sending it to an Adafruit IO feed.
3+
4+
from random import randint
5+
import board
6+
import neopixel
7+
import busio
8+
from digitalio import DigitalInOut
9+
from adafruit_esp32spi import adafruit_esp32spi
10+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
11+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
12+
13+
from adafruit_minimqtt import MQTT
14+
from adafruit_io.adafruit_io import IO_MQTT
15+
16+
### WiFi ###
17+
18+
# Get wifi details and more from a secrets.py file
19+
try:
20+
from secrets import secrets
21+
except ImportError:
22+
print("WiFi secrets are kept in secrets.py, please add them there!")
23+
raise
24+
25+
# If you are using a board with pre-defined ESP32 Pins:
26+
esp32_cs = DigitalInOut(board.ESP_CS)
27+
esp32_ready = DigitalInOut(board.ESP_BUSY)
28+
esp32_reset = DigitalInOut(board.ESP_RESET)
29+
30+
# If you have an externally connected ESP32:
31+
# esp32_cs = DigitalInOut(board.D9)
32+
# esp32_ready = DigitalInOut(board.D10)
33+
# esp32_reset = DigitalInOut(board.D5)
34+
35+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
36+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
37+
"""Use below for Most Boards"""
38+
status_light = neopixel.NeoPixel(
39+
board.NEOPIXEL, 1, brightness=0.2
40+
) # Uncomment for Most Boards
41+
"""Uncomment below for ItsyBitsy M4"""
42+
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
43+
# Uncomment below for an externally defined RGB LED
44+
# import adafruit_rgbled
45+
# from adafruit_esp32spi import PWMOut
46+
# RED_LED = PWMOut.PWMOut(esp, 26)
47+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
48+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
49+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
51+
52+
# Define callback functions which will be called when certain events happen.
53+
def connected(client):
54+
# Connected function will be called when the client is connected to Adafruit IO.
55+
# This is a good place to subscribe to feed changes. The client parameter
56+
# passed to this function is the Adafruit IO MQTT client so you can make
57+
# calls against it easily.
58+
print("Connected to Adafruit IO!")
59+
60+
# Subscribe to a location feed!
61+
io.subscribe("location")
62+
63+
64+
def disconnected(client):
65+
# Disconnected function will be called when the client disconnects.
66+
print("Disconnected from Adafruit IO!")
67+
68+
69+
def message(client, feed_id, payload):
70+
# Message function will be called when a subscribed feed has a new value.
71+
# The feed_id parameter identifies the feed, and the payload parameter has
72+
# the new value.
73+
print("Feed {0} received new value: {1}".format(feed_id, payload))
74+
75+
76+
# Connect to WiFi
77+
wifi.connect()
78+
79+
# Initialize a new MQTT Client object
80+
client = MQTT(
81+
socket=socket,
82+
broker="io.adafruit.com",
83+
username=secrets["aio_user"],
84+
password=secrets["aio_key"],
85+
network_manager=wifi,
86+
log=True,
87+
)
88+
89+
# Initialize an Adafruit IO MQTT Client
90+
io = IO_MQTT(client)
91+
92+
# Connect the callback methods defined above to Adafruit IO
93+
io.on_connect = connected
94+
io.on_disconnect = disconnected
95+
io.on_message = message
96+
97+
# Connect to Adafruit IO
98+
io.connect()
99+
100+
# Set data
101+
data_value = 42
102+
103+
# Set up metadata associated with data_value
104+
# lat, lon, ele
105+
metadata = "40.726190, -74.005334, -6"
106+
107+
# Send data and location metadata to the 'location' feed
108+
print("Sending data and location metadata to IO...")
109+
io.publish("location", data_value, metadata)
110+
print("Data sent!")
111+
112+
113+
# Listen forever...
114+
io.loop_blocking()

0 commit comments

Comments
 (0)