|
1 | 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
|
2 | 2 | # SPDX-License-Identifier: MIT
|
3 | 3 |
|
| 4 | +import os |
4 | 5 | import ssl
|
5 | 6 | import socketpool
|
6 | 7 | import wifi
|
7 | 8 | import adafruit_minimqtt.adafruit_minimqtt as MQTT
|
8 | 9 |
|
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 |
| 10 | +# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys |
| 11 | +# with your WiFi credentials. DO NOT share that file or commit it into Git or other |
11 | 12 | # 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 |
| 13 | + |
| 14 | +# Set your Adafruit IO Username and Key in settings.toml |
20 | 15 | # (visit io.adafruit.com if you need to create an account,
|
21 | 16 | # 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"]) |
| 17 | +aio_username = os.getenv("aio_username") |
| 18 | +aio_key = os.getenv("aio_key") |
| 19 | +aio_broker = os.getenv("broker") |
| 20 | +aio_port = os.getenv("port") |
| 21 | + |
| 22 | +print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) |
| 23 | +wifi.radio.connect( |
| 24 | + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") |
| 25 | +) |
| 26 | +print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID")) |
28 | 27 |
|
29 | 28 | ### Topic Setup ###
|
30 | 29 |
|
|
36 | 35 | # Use this topic if you'd like to connect to io.adafruit.com
|
37 | 36 | # mqtt_topic = secrets["aio_username"] + '/feeds/temperature'
|
38 | 37 |
|
| 38 | + |
39 | 39 | ### Code ###
|
40 | 40 | # Define callback methods which are called when events occur
|
41 | 41 | # pylint: disable=unused-argument, redefined-outer-name
|
@@ -77,10 +77,10 @@ def message(client, topic, message):
|
77 | 77 |
|
78 | 78 | # Set up a MiniMQTT Client
|
79 | 79 | mqtt_client = MQTT.MQTT(
|
80 |
| - broker=secrets["broker"], |
81 |
| - port=secrets["port"], |
82 |
| - username=secrets["aio_username"], |
83 |
| - password=secrets["aio_key"], |
| 80 | + broker=aio_broker, |
| 81 | + port=aio_port, |
| 82 | + username=os.getenv("CIRCUITPY_WIFI_SSID"), |
| 83 | + password=os.getenv("CIRCUITPY_WIFI_PASSWORD"), |
84 | 84 | socket_pool=pool,
|
85 | 85 | ssl_context=ssl.create_default_context(),
|
86 | 86 | )
|
|
0 commit comments