|
3 | 3 | import time
|
4 | 4 | from random import randint
|
5 | 5 |
|
| 6 | +import os |
6 | 7 | import ssl
|
7 | 8 | import socketpool
|
8 | 9 | import wifi
|
|
16 | 17 | # source control.
|
17 | 18 | # pylint: disable=no-name-in-module,wrong-import-order
|
18 | 19 | try:
|
19 |
| - from secrets import secrets |
| 20 | + if os.getenv("AIO_USERNAME") and os.getenv("AIO_KEY"): |
| 21 | + secrets = { |
| 22 | + "aio_username": os.getenv("AIO_USERNAME"), |
| 23 | + "aio_key": os.getenv("AIO_KEY"), |
| 24 | + "ssid": os.getenv("CIRCUITPY_WIFI_SSID"), |
| 25 | + "password": os.getenv("CIRCUITPY_WIFI_PASSWORD"), |
| 26 | + } |
| 27 | + else: |
| 28 | + from secrets import secrets |
20 | 29 | except ImportError:
|
21 |
| - print("WiFi secrets are kept in secrets.py, please add them there!") |
| 30 | + print( |
| 31 | + "WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, please add them there!" |
| 32 | + ) |
22 | 33 | raise
|
23 | 34 |
|
24 | 35 | # Set your Adafruit IO Username and Key in secrets.py
|
|
27 | 38 | aio_username = secrets["aio_username"]
|
28 | 39 | aio_key = secrets["aio_key"]
|
29 | 40 |
|
30 |
| -print("Connecting to %s" % secrets["ssid"]) |
31 |
| -wifi.radio.connect(secrets["ssid"], secrets["password"]) |
32 |
| -print("Connected to %s!" % secrets["ssid"]) |
| 41 | +if not wifi.radio.connected: |
| 42 | + print("Connecting to %s" % secrets["ssid"]) |
| 43 | + wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 44 | + print("Connected to %s!" % secrets["ssid"]) |
33 | 45 |
|
34 | 46 |
|
35 | 47 | # Define callback functions which will be called when certain events happen.
|
@@ -74,11 +86,12 @@ def message(client, feed_id, payload):
|
74 | 86 | # Initialize a new MQTT Client object
|
75 | 87 | mqtt_client = MQTT.MQTT(
|
76 | 88 | broker="io.adafruit.com",
|
77 |
| - port=1883, |
| 89 | + port=8883, |
78 | 90 | username=secrets["aio_username"],
|
79 | 91 | password=secrets["aio_key"],
|
80 | 92 | socket_pool=pool,
|
81 | 93 | ssl_context=ssl.create_default_context(),
|
| 94 | + is_ssl=True, |
82 | 95 | )
|
83 | 96 |
|
84 | 97 | # Initialize an Adafruit IO MQTT Client
|
|
0 commit comments