10
10
import neopixel
11
11
import busio
12
12
from digitalio import DigitalInOut
13
-
14
- # Import WiFi configuration
15
13
from adafruit_esp32spi import adafruit_esp32spi
16
14
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
17
15
import adafruit_esp32spi .adafruit_esp32spi_socket as socket
18
16
19
- # Import the Adafruit IO MQTT Class
20
- from adafruit_io .adafruit_io import MQTT
17
+ from adafruit_minimqtt import MQTT
18
+ from adafruit_io .adafruit_io import IO_MQTT
21
19
22
20
### WiFi ###
23
21
41
39
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
42
40
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
43
41
"""Use below for Most Boards"""
44
- status_light = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
42
+ status_light = neopixel .NeoPixel (
43
+ board .NEOPIXEL , 1 , brightness = 0.2
44
+ ) # Uncomment for Most Boards
45
45
"""Uncomment below for ItsyBitsy M4"""
46
46
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
47
47
# Uncomment below for an externally defined RGB LED
@@ -59,61 +59,59 @@ def connected(client):
59
59
# This is a good place to subscribe to feed changes. The client parameter
60
60
# passed to this function is the Adafruit IO MQTT client so you can make
61
61
# calls against it easily.
62
- print (' Connected to Adafruit IO! Listening for DemoFeed changes...' )
62
+ print (" Connected to Adafruit IO! Listening for DemoFeed changes..." )
63
63
# Subscribe to changes on a feed named DemoFeed.
64
- client .subscribe ('DemoFeed' )
64
+ client .subscribe ("DemoFeed" )
65
+
65
66
66
67
def disconnected (client ):
67
68
# Disconnected function will be called when the client disconnects.
68
- print (' Disconnected from Adafruit IO!' )
69
- sys . exit ( 1 )
69
+ print (" Disconnected from Adafruit IO!" )
70
+
70
71
71
72
def message (client , feed_id , payload ):
72
73
# Message function will be called when a subscribed feed has a new value.
73
74
# The feed_id parameter identifies the feed, and the payload parameter has
74
75
# the new value.
75
- print ('Feed {0} received new value: {1}' .format (feed_id , payload ))
76
+ print ("Feed {0} received new value: {1}" .format (feed_id , payload ))
77
+
76
78
77
79
# Connect to WiFi
78
80
wifi .connect ()
79
81
80
- # Create an Adafruit IO MQTT client.
81
- client = MQTT (secrets ['aio_user' ],
82
- secrets ['aio_password' ],
83
- wifi ,
84
- socket )
82
+ # Initialize a new MQTT Client
83
+ client = MQTT (
84
+ socket = socket ,
85
+ broker = "io.adafruit.com" ,
86
+ username = secrets ["aio_user" ],
87
+ password = secrets ["aio_key" ],
88
+ network_manager = wifi ,
89
+ )
85
90
86
91
# Setup the callback functions defined above.
87
- client .on_connect = connected
92
+ client .on_connect = connected
88
93
client .on_disconnect = disconnected
89
- client .on_message = message
94
+ client .on_message = message
90
95
91
- # Connect to the Adafruit IO server.
92
- client . connect ( )
96
+ # Initialize an Adafruit IO MQTT Client
97
+ io = IO_MQTT ( client )
93
98
94
- # Now the program needs to use a client loop function to ensure messages are
95
- # sent and received. There are a two options for driving the message loop:
99
+ # Connect to Adafruit IO
100
+ io . connect ()
96
101
97
- # You can pump the message loop yourself by periodically calling
98
- # the client loop function. Notice how the loop below changes to call loop
99
- # continuously while still sending a new message every 10 seconds. This is a
100
- # good option if you don't want to or can't have a thread pumping the message
101
- # loop in the background.
102
+ # You can call the message loop every X seconds
102
103
last = 0
103
- print (' Publishing a new message every 10 seconds (press Ctrl-C to quit) ...' )
104
+ print (" Publishing a new message every 10 seconds..." )
104
105
while True :
105
106
# Explicitly pump the message loop.
106
- client .loop ()
107
+ io .loop ()
107
108
# Send a new message every 10 seconds.
108
109
if (time .monotonic () - last ) >= 5 :
109
110
value = randint (0 , 100 )
110
- print (' Publishing {0} to DemoFeed.' .format (value ))
111
- client .publish (' DemoFeed' , value )
111
+ print (" Publishing {0} to DemoFeed." .format (value ))
112
+ io .publish (" DemoFeed" , value )
112
113
last = time .monotonic ()
113
114
114
- # Or you can just call loop_blocking. This will run a message loop
115
- # forever, so your program will not get past the loop_blocking call. This is
116
- # good for simple programs which only listen to events. For more complex programs
117
- # you probably need to have a background thread loop or explicit message loop like
118
- # the two previous examples above.
119
- # client.loop_blocking()
115
+ # You can also call loop_blocking if you only want to receive values.
116
+ # NOTE: If uncommented, no code below this line will run.
117
+ # io.loop_blocking()
0 commit comments