1
1
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2
2
# SPDX-License-Identifier: MIT
3
3
4
+ import os
4
5
import time
5
6
import board
6
7
import busio
7
8
from digitalio import DigitalInOut
8
9
import neopixel
9
10
from adafruit_esp32spi import adafruit_esp32spi
10
- from adafruit_esp32spi import adafruit_esp32spi_wifimanager
11
11
import adafruit_esp32spi .adafruit_esp32spi_socket as socket
12
12
13
13
import adafruit_minimqtt .adafruit_minimqtt as MQTT
14
14
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.
16
18
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" )
23
21
24
22
# If you are using a board with pre-defined ESP32 Pins:
25
23
esp32_cs = DigitalInOut (board .ESP_CS )
46
44
# GREEN_LED = PWMOut.PWMOut(esp, 27)
47
45
# BLUE_LED = PWMOut.PWMOut(esp, 25)
48
46
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49
- wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (esp , secrets , status_light )
50
47
51
48
### Adafruit IO Setup ###
52
49
53
50
# Setup a feed named `testfeed` for publishing.
54
- default_topic = secrets [ "user" ] + "/feeds/testfeed"
51
+ default_topic = aio_username + "/feeds/testfeed"
55
52
56
53
### Code ###
57
54
55
+
58
56
# Define callback methods which are called when events occur
59
57
# pylint: disable=unused-argument, redefined-outer-name
60
58
def connected (client , userdata , flags , rc ):
@@ -81,15 +79,15 @@ def message(client, topic, message):
81
79
82
80
# Connect to WiFi
83
81
print ("Connecting to WiFi..." )
84
- wifi . connect ( )
82
+ esp . connect_AP ( os . getenv ( "CIRCUITPY_WIFI_SSID" ), os . getenv ( "CIRCUITPY_WIFI_PASSWORD" ) )
85
83
print ("Connected!" )
86
84
87
85
# Initialize MQTT interface with the esp interface
88
86
MQTT .set_socket (socket , esp )
89
87
90
88
# Set up a MiniMQTT Client
91
89
mqtt_client = MQTT .MQTT (
92
- broker = secrets [ "broker" ] , username = secrets [ "user" ] , password = secrets [ "pass" ]
90
+ broker = os . getenv ( "broker" ) , username = aio_username , password = aio_key
93
91
)
94
92
95
93
# Setup the callback methods above
@@ -109,7 +107,11 @@ def message(client, topic, message):
109
107
mqtt_client .loop ()
110
108
except (ValueError , RuntimeError ) as e :
111
109
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
+ )
113
115
mqtt_client .reconnect ()
114
116
continue
115
117
time .sleep (1 )
0 commit comments