1
1
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
2
2
# SPDX-License-Identifier: MIT
3
3
4
+ from os import getenv
4
5
import time
5
- import ssl
6
6
import json
7
- import socketpool
8
7
import wifi
8
+ import adafruit_connection_manager
9
9
import adafruit_minimqtt .adafruit_minimqtt as MQTT
10
10
from adafruit_aws_iot import MQTT_CLIENT
11
11
12
- # Add a secrets.py to your filesystem that has a dictionary called "secrets". DO NOT share that
13
- # file or commit it into Git or other source control. The "secrets" dictionary should have the
14
- # following keys:
15
- # "ssid" - Your WiFi ssid
16
- # "password" - Your WiFi password
17
- # "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem")
18
- # "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key")
19
- # "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com")
20
- # "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub")
21
- #
22
- # pylint: disable=no-name-in-module,wrong-import-order
23
- try :
24
- from secrets import secrets
25
- except ImportError :
26
- print ("WiFi secrets are kept in secrets.py, please add them there!" )
27
- raise
12
+ # Add a settings.toml to your filesystem. DO NOT share that file or commit it into
13
+ # Git or other source control. The file should have the following settings:
14
+ """
15
+ CIRCUITPY_WIFI_SSID="Your WiFi ssid"
16
+ CIRCUITPY_WIFI_PASSWORD="Your WiFi password"
17
+ device_cert_path="<THING_NAME>.cert.pem" # Path to the Device Certificate from AWS IoT
18
+ device_key_path="<THING_NAME>.private.key" # Path to the RSA Private Key from AWS IoT
19
+ broker="<PREFIX>.iot.<REGION>.amazonaws.com" # The endpoint for the AWS IoT broker
20
+ client_id="client_id" # The client id. Your device's Policy needs to allow this client
21
+ """
22
+
23
+ # Get WiFi details and AWS keys, ensure these are setup in settings.toml
24
+ ssid = getenv ("CIRCUITPY_WIFI_SSID" )
25
+ password = getenv ("CIRCUITPY_WIFI_PASSWORD" )
26
+ device_cert_path = getenv ("device_cert_path" )
27
+ device_key_path = getenv ("device_key_path" )
28
+ broker = getenv ("broker" )
29
+ client_id = getenv ("client_id" )
28
30
29
31
### Code ###
30
32
@@ -76,23 +78,21 @@ def message(client, topic, msg):
76
78
print ("Message from {}: {}" .format (topic , msg ))
77
79
78
80
79
- print ("Connecting to %s" % secrets [ " ssid" ] )
80
- wifi .radio .connect (secrets [ " ssid" ], secrets [ " password" ] )
81
- print ("Connected to %s!" % secrets [ "ssid" ] )
81
+ print (f "Connecting to { ssid } " )
82
+ wifi .radio .connect (ssid , password )
83
+ print (f "Connected to { ssid } !" )
82
84
83
85
# Create a socket pool
84
- pool = socketpool . SocketPool (wifi .radio )
85
- ssl_context = ssl . create_default_context ( )
86
+ pool = adafruit_connection_manager . get_radio_socketpool (wifi .radio )
87
+ ssl_context = adafruit_connection_manager . get_radio_ssl_context ( wifi . radio )
86
88
87
89
# Set AWS Device Certificate and AWS RSA Private Key
88
- ssl_context .load_cert_chain (
89
- certfile = secrets ["device_cert_path" ], keyfile = secrets ["device_key_path" ]
90
- )
90
+ ssl_context .load_cert_chain (certfile = device_cert_path , keyfile = device_key_path )
91
91
92
92
# Set up a MiniMQTT Client
93
93
mqtt_client = MQTT .MQTT (
94
- broker = secrets [ " broker" ] ,
95
- client_id = secrets [ " client_id" ] ,
94
+ broker = broker ,
95
+ client_id = client_id ,
96
96
is_ssl = True ,
97
97
socket_pool = pool ,
98
98
ssl_context = ssl_context ,
@@ -109,7 +109,7 @@ def message(client, topic, msg):
109
109
aws_iot .on_publish = publish
110
110
aws_iot .on_message = message
111
111
112
- print ("Attempting to connect to %s" % mqtt_client .broker )
112
+ print (f "Attempting to connect to { mqtt_client .broker } " )
113
113
aws_iot .connect ()
114
114
115
115
# Start a blocking message loop...
0 commit comments