1010
1111import adafruit_minimqtt .adafruit_minimqtt as MQTT
1212
13- ### WiFi ###
13+ # Get WiFi details and MQTT keys, ensure these are setup in settings.toml
14+ ssid = getenv ("CIRCUITPY_WIFI_SSID" )
15+ password = getenv ("CIRCUITPY_WIFI_PASSWORD" )
16+ broker = getenv ("broker" )
17+ username = getenv ("username" )
18+ paswword = getenv ("paswword" )
1419
15- # Get wifi details and more from a secrets.py file
16- try :
17- from secrets import secrets
18- except ImportError :
19- print ("WiFi secrets are kept in secrets.py, please add them there!" )
20- raise
20+ ### WiFi ###
2121
2222# If you are using a board with pre-defined ESP32 Pins:
2323esp32_cs = DigitalInOut (board .ESP_CS )
3232spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
3333esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
3434"""Use below for Most Boards"""
35- status_light = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
35+ status_pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
3636"""Uncomment below for ItsyBitsy M4"""
37- # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
37+ # status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
3838# Uncomment below for an externally defined RGB LED
3939# import adafruit_rgbled
4040# from adafruit_esp32spi import PWMOut
4141# RED_LED = PWMOut.PWMOut(esp, 26)
4242# GREEN_LED = PWMOut.PWMOut(esp, 27)
4343# BLUE_LED = PWMOut.PWMOut(esp, 25)
44- # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
45- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
44+ # status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
45+ wifi = adafruit_esp32spi_wifimanager .WiFiManager (esp , ssid , password , status_pixel = status_pixel )
4646
4747### Topic Setup ###
4848
@@ -109,9 +109,9 @@ def publish(client, userdata, topic, pid):
109109
110110# Set up a MiniMQTT Client
111111client = MQTT .MQTT (
112- broker = secrets [ " broker" ] ,
113- username = secrets [ "user" ] ,
114- password = secrets [ "pass" ] ,
112+ broker = broker ,
113+ username = username ,
114+ password = password ,
115115 socket_pool = pool ,
116116 ssl_context = ssl_context ,
117117)
@@ -123,17 +123,17 @@ def publish(client, userdata, topic, pid):
123123client .on_unsubscribe = unsubscribe
124124client .on_publish = publish
125125
126- print ("Attempting to connect to %s" % client .broker )
126+ print (f "Attempting to connect to { client .broker } " )
127127client .connect ()
128128
129- print ("Subscribing to %s" % mqtt_topic )
129+ print (f "Subscribing to { mqtt_topic } " )
130130client .subscribe (mqtt_topic )
131131
132- print ("Publishing to %s" % mqtt_topic )
132+ print (f "Publishing to { mqtt_topic } " )
133133client .publish (mqtt_topic , "Hello Broker!" )
134134
135- print ("Unsubscribing from %s" % mqtt_topic )
135+ print (f "Unsubscribing from { mqtt_topic } " )
136136client .unsubscribe (mqtt_topic )
137137
138- print ("Disconnecting from %s" % client .broker )
138+ print (f "Disconnecting from { client .broker } " )
139139client .disconnect ()
0 commit comments