9
9
from adafruit_esp32spi import adafruit_esp32spi
10
10
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
11
11
import adafruit_esp32spi .adafruit_esp32spi_socket as socket
12
- from adafruit_minimqtt import MQTT
13
12
13
+ import adafruit_minimqtt as MQTT
14
14
15
- # Get Adafruit IO details and more from a secrets.py file
15
+ ### WiFi ###
16
+
17
+ # Get wifi details and more from a secrets.py file
16
18
try :
17
19
from secrets import secrets
18
20
except ImportError :
19
- print ("Adafruit IO secrets are kept in secrets.py, please add them there!" )
21
+ print ("WiFi secrets are kept in secrets.py, please add them there!" )
20
22
raise
21
23
22
- cs = DigitalInOut (board .D10 )
23
- spi_bus = busio .SPI (board .SCK , MOSI = board .MOSI , MISO = board .MISO )
24
-
25
- # Initialize ethernet interface with DHCP
26
- eth = WIZNET5K (spi_bus , cs )
24
+ # If you are using a board with pre-defined ESP32 Pins:
25
+ esp32_cs = DigitalInOut (board .ESP_CS )
26
+ esp32_ready = DigitalInOut (board .ESP_BUSY )
27
+ esp32_reset = DigitalInOut (board .ESP_RESET )
28
+
29
+ # If you have an externally connected ESP32:
30
+ # esp32_cs = DigitalInOut(board.D9)
31
+ # esp32_ready = DigitalInOut(board.D10)
32
+ # esp32_reset = DigitalInOut(board.D5)
33
+
34
+ spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
35
+ esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset , debug = True )
36
+ """Use below for Most Boards"""
37
+ status_light = neopixel .NeoPixel (
38
+ board .NEOPIXEL , 1 , brightness = 0.2 ) # Uncomment for Most Boards
39
+ """Uncomment below for ItsyBitsy M4"""
40
+ # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
41
+ # Uncomment below for an externally defined RGB LED
42
+ # import adafruit_rgbled
43
+ # from adafruit_esp32spi import PWMOut
44
+ # RED_LED = PWMOut.PWMOut(esp, 26)
45
+ # GREEN_LED = PWMOut.PWMOut(esp, 27)
46
+ # BLUE_LED = PWMOut.PWMOut(esp, 25)
47
+ # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
48
+ wifi = adafruit_esp32spi_wifimanager .ESPSPI_WiFiManager (
49
+ esp , secrets , status_light )
27
50
28
51
### Feeds ###
29
52
@@ -56,15 +79,18 @@ def message(client, topic, message):
56
79
print ('New message on topic {0}: {1}' .format (topic , message ))
57
80
58
81
59
- # Initialize MQTT interface with the ethernet interface
60
- MQTT .set_socket
82
+ # Connect to WiFi
83
+ print ("Connecting to WiFi..." )
84
+ wifi .connect ()
85
+ print ("Connected!" )
86
+
87
+ # Initialize MQTT interface with the esp interface
88
+ MQTT .set_socket (socket , esp )
61
89
62
90
# Set up a MiniMQTT Client
63
- # NOTE: We'll need to connect insecurely for ethernet configurations.
64
- mqtt_client = MQTT .MQTT (broker = 'io.adafruit.com' ,
65
- username = secrets ['aio_username' ],
66
- password = secrets ['aio_key' ],
67
- is_ssl = False )
91
+ mqtt_client = MQTT .MQTT (broker = 'http://io.adafruit.com' ,
92
+ username = secrets ['aio_username' ],
93
+ password = secrets ['aio_key' ])
68
94
69
95
# Setup the callback methods above
70
96
mqtt_client .on_connect = connected
@@ -85,4 +111,4 @@ def message(client, topic, message):
85
111
mqtt_client .publish (photocell_feed , photocell_val )
86
112
print ('Sent!' )
87
113
photocell_val += 1
88
- time .sleep (1 )
114
+ time .sleep (5 )
0 commit comments