11# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22# SPDX-License-Identifier: MIT
33
4+ import os
45import time
56import board
67import busio
78from digitalio import DigitalInOut
89import neopixel
910from adafruit_esp32spi import adafruit_esp32spi
10- from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111import adafruit_esp32spi .adafruit_esp32spi_socket as socket
1212
1313import adafruit_minimqtt .adafruit_minimqtt as MQTT
1414
15- ### WiFi ###
15+ # Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
16+ # with your WiFi credentials. Add your Adafruit IO username and key as well.
17+ # DO NOT share that file or commit it into Git or other source control.
1618
17- # Get wifi details and more from a secrets.py file
18- try :
19- from secrets import secrets
20- except ImportError :
21- print ("WiFi secrets are kept in secrets.py, please add them there!" )
22- raise
19+ aio_username = os .getenv ("aio_username" )
20+ aio_key = os .getenv ("aio_key" )
2321
2422# If you are using a board with pre-defined ESP32 Pins:
2523esp32_cs = DigitalInOut (board .ESP_CS )
4644# GREEN_LED = PWMOut.PWMOut(esp, 27)
4745# BLUE_LED = PWMOut.PWMOut(esp, 25)
4846# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
5047
5148### Adafruit IO Setup ###
5249
5350# Setup a feed named `testfeed` for publishing.
54- default_topic = secrets [ "user" ] + "/feeds/testfeed"
51+ default_topic = aio_username + "/feeds/testfeed"
5552
5653### Code ###
5754
55+
5856# Define callback methods which are called when events occur
5957# pylint: disable=unused-argument, redefined-outer-name
6058def connected (client , userdata , flags , rc ):
@@ -81,15 +79,15 @@ def message(client, topic, message):
8179
8280# Connect to WiFi
8381print ("Connecting to WiFi..." )
84- wifi . connect ( )
82+ esp . connect_AP ( os . getenv ( "CIRCUITPY_WIFI_SSID" ), os . getenv ( "CIRCUITPY_WIFI_PASSWORD" ) )
8583print ("Connected!" )
8684
8785# Initialize MQTT interface with the esp interface
8886MQTT .set_socket (socket , esp )
8987
9088# Set up a MiniMQTT Client
9189mqtt_client = MQTT .MQTT (
92- broker = secrets [ "broker" ] , username = secrets [ "user" ] , password = secrets [ "pass" ]
90+ broker = os . getenv ( "broker" ) , username = aio_username , password = aio_key
9391)
9492
9593# Setup the callback methods above
@@ -109,7 +107,11 @@ def message(client, topic, message):
109107 mqtt_client .loop ()
110108 except (ValueError , RuntimeError ) as e :
111109 print ("Failed to get data, retrying\n " , e )
112- wifi .reset ()
110+ esp .reset ()
111+ time .sleep (1 )
112+ esp .connect_AP (
113+ os .getenv ("CIRCUITPY_WIFI_SSID" ), os .getenv ("CIRCUITPY_WIFI_PASSWORD" )
114+ )
113115 mqtt_client .reconnect ()
114116 continue
115117 time .sleep (1 )
0 commit comments