|
1 | 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | | -import ssl |
5 | | -import socketpool |
6 | | -import wifi |
| 4 | +import os |
| 5 | +import board |
| 6 | +import busio |
| 7 | +from digitalio import DigitalInOut |
| 8 | +from adafruit_esp32spi import adafruit_esp32spi |
| 9 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
7 | 10 | import adafruit_minimqtt.adafruit_minimqtt as MQTT |
8 | 11 |
|
9 | | -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
10 | | -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
11 | | -# source control. |
12 | | -# pylint: disable=no-name-in-module,wrong-import-order |
13 | | -try: |
14 | | - from secrets import secrets |
15 | | -except ImportError: |
16 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
17 | | - raise |
18 | | - |
19 | | -# Set your Adafruit IO Username and Key in secrets.py |
20 | | -# (visit io.adafruit.com if you need to create an account, |
21 | | -# or if you need your Adafruit IO key.) |
22 | | -aio_username = secrets["aio_username"] |
23 | | -aio_key = secrets["aio_key"] |
24 | | - |
25 | | -print("Connecting to %s" % secrets["ssid"]) |
26 | | -wifi.radio.connect(secrets["ssid"], secrets["password"]) |
27 | | -print("Connected to %s!" % secrets["ssid"]) |
| 12 | +# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys |
| 13 | +# with your WiFi credentials. Add your Adafruit IO username and key as well. |
| 14 | +# DO NOT share that file or commit it into Git or other source control. |
| 15 | + |
| 16 | +aio_username = os.getenv("aio_username") |
| 17 | +aio_key = os.getenv("aio_key") |
| 18 | + |
| 19 | +# If you are using a board with pre-defined ESP32 Pins: |
| 20 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 21 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 22 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 23 | + |
| 24 | +# If you have an externally connected ESP32: |
| 25 | +# esp32_cs = DigitalInOut(board.D9) |
| 26 | +# esp32_ready = DigitalInOut(board.D10) |
| 27 | +# esp32_reset = DigitalInOut(board.D5) |
| 28 | + |
| 29 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 30 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 31 | + |
| 32 | +print("Connecting to AP...") |
| 33 | +while not esp.is_connected: |
| 34 | + try: |
| 35 | + esp.connect_AP( |
| 36 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 37 | + ) |
| 38 | + except RuntimeError as e: |
| 39 | + print("could not connect to AP, retrying: ", e) |
| 40 | + continue |
| 41 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
28 | 42 |
|
29 | 43 | ### Topic Setup ### |
30 | 44 |
|
31 | 45 | # MQTT Topic |
32 | 46 | # Use this topic if you'd like to connect to a standard MQTT broker |
33 | | -mqtt_topic = "test/topic" |
| 47 | +# mqtt_topic = "test/topic" |
34 | 48 |
|
35 | 49 | # Adafruit IO-style Topic |
36 | 50 | # Use this topic if you'd like to connect to io.adafruit.com |
37 | | -# mqtt_topic = secrets["aio_username"] + '/feeds/temperature' |
| 51 | +mqtt_topic = aio_username + "/feeds/temperature" |
38 | 52 |
|
39 | 53 |
|
40 | 54 | ### Code ### |
| 55 | + |
| 56 | + |
41 | 57 | # Define callback methods which are called when events occur |
42 | 58 | # pylint: disable=unused-argument, redefined-outer-name |
43 | 59 | def connect(mqtt_client, userdata, flags, rc): |
@@ -69,21 +85,17 @@ def publish(mqtt_client, userdata, topic, pid): |
69 | 85 |
|
70 | 86 |
|
71 | 87 | def message(client, topic, message): |
72 | | - # Method called when a client's subscribed feed has a new value. |
73 | 88 | print("New message on topic {0}: {1}".format(topic, message)) |
74 | 89 |
|
75 | 90 |
|
76 | | -# Create a socket pool |
77 | | -pool = socketpool.SocketPool(wifi.radio) |
| 91 | +socket.set_interface(esp) |
| 92 | +MQTT.set_socket(socket, esp) |
78 | 93 |
|
79 | 94 | # Set up a MiniMQTT Client |
80 | 95 | mqtt_client = MQTT.MQTT( |
81 | | - broker=secrets["broker"], |
82 | | - port=secrets["port"], |
83 | | - username=secrets["aio_username"], |
84 | | - password=secrets["aio_key"], |
85 | | - socket_pool=pool, |
86 | | - ssl_context=ssl.create_default_context(), |
| 96 | + broker="io.adafruit.com", |
| 97 | + username=aio_username, |
| 98 | + password=aio_key, |
87 | 99 | ) |
88 | 100 |
|
89 | 101 | # Connect callback handlers to mqtt_client |
|
0 commit comments