|
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("WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, please add them there!") |
22 | 31 | raise
|
23 | 32 |
|
24 | 33 | # Set your Adafruit IO Username and Key in secrets.py
|
|
27 | 36 | aio_username = secrets["aio_username"]
|
28 | 37 | aio_key = secrets["aio_key"]
|
29 | 38 |
|
30 |
| -print("Connecting to %s" % secrets["ssid"]) |
31 |
| -wifi.radio.connect(secrets["ssid"], secrets["password"]) |
32 |
| -print("Connected to %s!" % secrets["ssid"]) |
| 39 | +if not wifi.radio.connected: |
| 40 | + print("Connecting to %s" % secrets["ssid"]) |
| 41 | + wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 42 | + print("Connected to %s!" % secrets["ssid"]) |
33 | 43 |
|
34 | 44 |
|
35 | 45 | # Define callback functions which will be called when certain events happen.
|
@@ -74,11 +84,12 @@ def message(client, feed_id, payload):
|
74 | 84 | # Initialize a new MQTT Client object
|
75 | 85 | mqtt_client = MQTT.MQTT(
|
76 | 86 | broker="io.adafruit.com",
|
77 |
| - port=1883, |
| 87 | + port=8883, |
78 | 88 | username=secrets["aio_username"],
|
79 | 89 | password=secrets["aio_key"],
|
80 | 90 | socket_pool=pool,
|
81 | 91 | ssl_context=ssl.create_default_context(),
|
| 92 | + is_ssl=True |
82 | 93 | )
|
83 | 94 |
|
84 | 95 | # Initialize an Adafruit IO MQTT Client
|
|
0 commit comments