17
17
print ("WiFi secrets are kept in secrets.py, please add them there!" )
18
18
raise
19
19
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)
23
29
24
30
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
25
31
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
38
44
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
39
45
wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
40
46
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
-
51
47
### Code ###
52
48
53
49
# Define callback methods which are called when events occur
54
50
# pylint: disable=unused-argument, redefined-outer-name
55
- def connect (client , userdata , flags , rc ):
51
+ def connected (client , userdata , flags , rc ):
56
52
# This function will be called when the client is connected
57
53
# successfully to the broker.
58
54
print ("Connected to MQTT Broker!" )
59
- print ("Flags: {0}\n RC: {1}" .format (flags , rc ))
60
55
61
56
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
65
59
print ("Disconnected from MQTT Broker!" )
66
60
67
61
@@ -75,19 +69,15 @@ def unsubscribe(client, userdata, topic, pid):
75
69
print ("Unsubscribed from {0} with PID {1}" .format (topic , pid ))
76
70
77
71
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
-
82
72
def 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
+
84
78
85
79
def 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.
91
81
print ("New message on topic {0}: {1}" .format (topic , message ))
92
82
93
83
@@ -96,32 +86,28 @@ def on_message(client, topic, message):
96
86
wifi .connect ()
97
87
print ("Connected!" )
98
88
99
- # Initialize MQTT interface with the esp interface
100
89
MQTT .set_socket (socket , esp )
101
90
102
91
# 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" ] )
104
93
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
108
97
client .on_subscribe = subscribe
109
98
client .on_unsubscribe = unsubscribe
110
- client .on_publish = publish
111
-
112
99
client .on_message = on_message
113
- client .add_topic_callback ("sensors /batteryLevel" , on_battery_msg )
100
+ client .add_topic_callback ("device /batteryLevel" , on_battery_msg )
114
101
115
102
# Connect the client to the MQTT broker.
116
103
print ("Connecting to MQTT broker..." )
117
104
client .connect ()
118
105
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 )
121
108
122
109
# Start a blocking message loop...
123
110
# NOTE: NO code below this loop will execute
124
- # NOTE: Network reconnection is handled within this loop
125
111
while True :
126
112
try :
127
113
client .loop ()
@@ -131,5 +117,3 @@ def on_message(client, topic, message):
131
117
client .reconnect ()
132
118
continue
133
119
time .sleep (1 )
134
-
135
-
0 commit comments