Skip to content

Commit 2cccc6b

Browse files
authored
Merge pull request #2 from brentru/update-aio-code
Update Adafruit IO MQTT Example
2 parents fd26c93 + c88147b commit 2cccc6b

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

examples/minimqtt_adafruitio_wifi.py

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Adafruit MiniMQTT Pub/Sub Example
2+
# Written by Tony DiCola for Adafruit Industries
3+
# Modified by Brent Rubell for Adafruit Industries
14
import time
25
import board
36
import busio
@@ -30,7 +33,8 @@
3033
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3134
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3235
"""Use below for Most Boards"""
33-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
36+
status_light = neopixel.NeoPixel(
37+
board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
3438
"""Uncomment below for ItsyBitsy M4"""
3539
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
3640
# Uncomment below for an externally defined RGB LED
@@ -40,49 +44,67 @@
4044
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4145
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4246
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
43-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
47+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
48+
esp, secrets, status_light)
4449

45-
### Adafruit IO Setup ###
50+
### Feeds ###
4651

47-
# Setup a feed named `photocell` for publishing.
48-
aio_publish_feed = secrets['user']+'/feeds/photocell'
52+
# Setup a feed named 'photocell' for publishing to a feed
53+
photocell_feed = secrets['aio_username'] + '/feeds/photocell'
4954

50-
# Setup a feed named `onoffbutton` for subscribing to changes.
51-
aio_subscribe_feed = secrets['user']+'/feeds/onoffbutton'
55+
# Setup a feed named 'onoff' for subscribing to changes
56+
onoff_feed = secrets['aio_username'] + '/feeds/onoff'
5257

5358
### Code ###
5459

55-
# pylint: disable=unused-argument
56-
def on_message(client, topic, message):
57-
# This method is called whenever a new message is received
58-
# from the server.
60+
# Define callback methods which are called when events occur
61+
# pylint: disable=unused-argument, redefined-outer-name
62+
def connected(client, userdata, flags, rc):
63+
# This function will be called when the client is connected
64+
# successfully to the broker.
65+
print('Connected to Adafruit IO! Listening for topic changes on %s' % onoff_feed)
66+
# Subscribe to all changes on the onoff_feed.
67+
client.subscribe(onoff_feed)
68+
69+
70+
def disconnected(client, userdata, rc):
71+
# This method is called when the client is disconnected
72+
print('Disconnected from Adafruit IO!')
73+
74+
75+
def message(client, topic, message):
76+
# This method is called when a topic the client is subscribed to
77+
# has a new message.
5978
print('New message on topic {0}: {1}'.format(topic, message))
6079

80+
6181
# Connect to WiFi
6282
wifi.connect()
6383

6484
# Set up a MiniMQTT Client
6585
mqtt_client = MQTT(socket,
66-
broker = secrets['broker'],
67-
username = secrets['user'],
68-
password = secrets['pass'],
69-
network_manager = wifi)
70-
71-
# Attach on_message method to the MQTT Client
72-
mqtt_client.on_message = on_message
73-
74-
# Initialize the MQTT Client
86+
broker='io.adafruit.com',
87+
username=secrets['aio_username'],
88+
password=secrets['aio_key'],
89+
network_manager=wifi)
90+
91+
# Setup the callback methods above
92+
mqtt_client.on_connect = connected
93+
mqtt_client.on_disconnect = disconnected
94+
mqtt_client.on_message = message
95+
96+
# Connect the client to the MQTT broker.
97+
print('Connecting to Adafruit IO...')
7598
mqtt_client.connect()
7699

77-
# Subscribe the client to topic aio_subscribe_feed
78-
mqtt_client.subscribe(aio_subscribe_feed)
79-
80100
photocell_val = 0
81101
while True:
82102
# Poll the message queue
83103
mqtt_client.loop()
84104

85-
print('Sending photocell value: %d'%photocell_val)
86-
mqtt_client.publish(aio_publish_feed, photocell_val)
105+
# Send a new message
106+
print('Sending photocell value: %d...' % photocell_val)
107+
mqtt_client.publish(photocell_feed, photocell_val)
108+
print('Sent!')
87109
photocell_val += 1
88-
time.sleep(0.5)
110+
time.sleep(1)

0 commit comments

Comments
 (0)