Skip to content

Commit bf18c6b

Browse files
author
brentru
committed
add group example
1 parent 7cc98d2 commit bf18c6b

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

examples/mqtt/adafruit_io_groups.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Subscribing to an Adafruit IO Group
2+
# and Publishing to the feeds in the group
3+
import time
4+
from random import randint
5+
6+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
7+
import board
8+
import busio
9+
import neopixel
10+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
11+
from adafruit_io.adafruit_io import IO_MQTT
12+
from adafruit_minimqtt import MQTT
13+
from digitalio import DigitalInOut
14+
15+
### WiFi ###
16+
17+
# Get wifi details and more from a secrets.py file
18+
try:
19+
from secrets import secrets
20+
except ImportError:
21+
print("WiFi secrets are kept in secrets.py, please add them there!")
22+
raise
23+
24+
# If you are using a board with pre-defined ESP32 Pins:
25+
esp32_cs = DigitalInOut(board.ESP_CS)
26+
esp32_ready = DigitalInOut(board.ESP_BUSY)
27+
esp32_reset = DigitalInOut(board.ESP_RESET)
28+
29+
# If you have an externally connected ESP32:
30+
# esp32_cs = DigitalInOut(board.D9)
31+
# esp32_ready = DigitalInOut(board.D10)
32+
# esp32_reset = DigitalInOut(board.D5)
33+
34+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
35+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
36+
"""Use below for Most Boards"""
37+
status_light = neopixel.NeoPixel(
38+
board.NEOPIXEL, 1, brightness=0.2
39+
) # Uncomment for Most Boards
40+
"""Uncomment below for ItsyBitsy M4"""
41+
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
42+
# Uncomment below for an externally defined RGB LED
43+
# import adafruit_rgbled
44+
# from adafruit_esp32spi import PWMOut
45+
# RED_LED = PWMOut.PWMOut(esp, 26)
46+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
47+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
48+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
50+
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 Group
61+
io.subscribe(group_key=group_name)
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+
)
87+
88+
# Initialize an Adafruit IO MQTT Client
89+
io = IO_MQTT(client)
90+
91+
# Connect the callback methods defined above to Adafruit IO
92+
io.on_connect = connected
93+
io.on_disconnect = disconnected
94+
io.on_message = message
95+
96+
# Group name
97+
group_name = "weatherstation"
98+
99+
# Feeds within the group
100+
temp_feed = "brubell/feeds/weatherstation.temperature"
101+
humid_feed = "brubell/feeds/weatherstation.humidity"
102+
103+
# Connect to Adafruit IO
104+
io.connect()
105+
106+
print("Publishing new messages to group feeds every 5 seconds...")
107+
108+
while True:
109+
io.loop()
110+
temp_reading = randint(0, 100)
111+
print("Publishing value {0} to feed: {1}".format(temp_reading, temp_feed))
112+
client.publish(temp_feed, temp_reading)
113+
114+
humid_reading = randint(0, 100)
115+
print("Publishing value {0} to feed: {1}".format(humid_reading, humid_feed))
116+
client.publish(humid_feed, humid_reading)
117+
time.sleep(5)

0 commit comments

Comments
 (0)