1717 print ("WiFi secrets are kept in secrets.py, please add them there!" )
1818 raise
1919
20- esp32_cs = DigitalInOut (board .D13 )
21- esp32_ready = DigitalInOut (board .D11 )
22- esp32_reset = DigitalInOut (board .D12 )
20+ # If you are using a board with pre-defined ESP32 Pins:
21+ esp32_cs = DigitalInOut (board .ESP_CS )
22+ esp32_ready = DigitalInOut (board .ESP_BUSY )
23+ esp32_reset = DigitalInOut (board .ESP_RESET )
24+
25+ # If you have an externally connected ESP32:
26+ # esp32_cs = DigitalInOut(board.D9)
27+ # esp32_ready = DigitalInOut(board.D10)
28+ # esp32_reset = DigitalInOut(board.D5)
2329
2430spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
2531esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
3844# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
3945wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
4046
41- ### Topic Setup ###
42-
43- # MQTT Topic
44- # Use this topic if you'd like to connect to a standard MQTT broker
45- mqtt_topic = "test/topic"
46-
47- # Adafruit IO-style Topic
48- # Use this topic if you'd like to connect to io.adafruit.com
49- # mqtt_topic = 'aio_user/feeds/temperature'
50-
5147### Code ###
5248
5349# Define callback methods which are called when events occur
5450# pylint: disable=unused-argument, redefined-outer-name
55- def connect (client , userdata , flags , rc ):
51+ def connected (client , userdata , flags , rc ):
5652 # This function will be called when the client is connected
5753 # successfully to the broker.
5854 print ("Connected to MQTT Broker!" )
59- print ("Flags: {0}\n RC: {1}" .format (flags , rc ))
6055
6156
62- def disconnect (client , userdata , rc ):
63- # This method is called when the client disconnects
64- # from the broker.
57+ def disconnected (client , userdata , rc ):
58+ # This method is called when the client is disconnected
6559 print ("Disconnected from MQTT Broker!" )
6660
6761
@@ -75,19 +69,15 @@ def unsubscribe(client, userdata, topic, pid):
7569 print ("Unsubscribed from {0} with PID {1}" .format (topic , pid ))
7670
7771
78- def publish (client , userdata , topic , pid ):
79- # This method is called when the client publishes data to a feed.
80- print ("Published to {0} with PID {1}" .format (topic , pid ))
81-
8272def on_battery_msg (client , topic , message ):
83- print ("Battery Level: {}v" .format (message ))
73+ # Method called when device/batteryLife has a new value
74+ print ("Battery level: {}v" .format (message ))
75+
76+ # client.remove_topic_callback("device/batteryLevel")
77+
8478
8579def on_message (client , topic , message ):
86- """Method callled when a client's subscribed feed has a new
87- value.
88- :param str topic: The topic of the feed with a new value.
89- :param str message: The new value
90- """
80+ # Method callled when a client's subscribed feed has a new value.
9181 print ("New message on topic {0}: {1}" .format (topic , message ))
9282
9383
@@ -96,32 +86,28 @@ def on_message(client, topic, message):
9686wifi .connect ()
9787print ("Connected!" )
9888
99- # Initialize MQTT interface with the esp interface
10089MQTT .set_socket (socket , esp )
10190
10291# Set up a MiniMQTT Client
103- client = MQTT .MQTT (broker = secrets ["broker" ], port = 1883 )
92+ client = MQTT .MQTT (broker = secrets ["broker" ], port = secrets [ "broker_port" ] )
10493
105- # Connect callback handlers to client
106- client .on_connect = connect
107- client .on_disconnect = disconnect
94+ # Setup the callback methods above
95+ client .on_connect = connected
96+ client .on_disconnect = disconnected
10897client .on_subscribe = subscribe
10998client .on_unsubscribe = unsubscribe
110- client .on_publish = publish
111-
11299client .on_message = on_message
113- client .add_topic_callback ("sensors /batteryLevel" , on_battery_msg )
100+ client .add_topic_callback ("device /batteryLevel" , on_battery_msg )
114101
115102# Connect the client to the MQTT broker.
116103print ("Connecting to MQTT broker..." )
117104client .connect ()
118105
119- # Subscribe to all notifications on the sensors/
120- client .subscribe ("sensors /#" , 1 )
106+ # Subscribe to all notifications on the device/ topic
107+ client .subscribe ("device /#" , 1 )
121108
122109# Start a blocking message loop...
123110# NOTE: NO code below this loop will execute
124- # NOTE: Network reconnection is handled within this loop
125111while True :
126112 try :
127113 client .loop ()
@@ -131,5 +117,3 @@ def on_message(client, topic, message):
131117 client .reconnect ()
132118 continue
133119 time .sleep (1 )
134-
135-
0 commit comments