11# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
22# SPDX-License-Identifier: MIT
33
4+ from os import getenv
45import time
5- import ssl
66import json
7- import socketpool
87import wifi
8+ import adafruit_connection_manager
99import adafruit_minimqtt .adafruit_minimqtt as MQTT
1010from adafruit_aws_iot import MQTT_CLIENT
1111
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")
12+ # Add a settings.toml to your filesystem. DO NOT share that
13+ # file or commit it into Git or other source control. The file dictionary should have the
14+ # following settings :
15+ # CIRCUITPY_WIFI_SSID - Your WiFi ssid
16+ # CIRCUITPY_WIFI_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")
2121#
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
22+ # Get WiFi details and AWS keys, ensure these are setup in settings.toml
23+ ssid = getenv ("CIRCUITPY_WIFI_SSID" )
24+ password = getenv ("CIRCUITPY_WIFI_PASSWORD" )
25+ device_cert_path = getenv ("device_cert_path" )
26+ device_key_path = getenv ("device_key_path" )
27+ broker = getenv ("broker" )
28+ client_id = getenv ("client_id" )
2829
2930### Code ###
3031
@@ -76,23 +77,21 @@ def message(client, topic, msg):
7677 print ("Message from {}: {}" .format (topic , msg ))
7778
7879
79- print ("Connecting to %s" % secrets [ " ssid" ] )
80- wifi .radio .connect (secrets [ " ssid" ], secrets [ " password" ] )
81- print ("Connected to %s!" % secrets [ "ssid" ] )
80+ print (f "Connecting to { ssid } " )
81+ wifi .radio .connect (ssid , password )
82+ print (f "Connected to { ssid } !" )
8283
8384# Create a socket pool
84- pool = socketpool . SocketPool (wifi .radio )
85- ssl_context = ssl . create_default_context ( )
85+ pool = adafruit_connection_manager . get_radio_socketpool (wifi .radio )
86+ ssl_context = adafruit_connection_manager . get_radio_ssl_context ( wifi . radio )
8687
8788# 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- )
89+ ssl_context .load_cert_chain (certfile = device_cert_path , keyfile = device_key_path )
9190
9291# Set up a MiniMQTT Client
9392mqtt_client = MQTT .MQTT (
94- broker = secrets [ " broker" ] ,
95- client_id = secrets [ " client_id" ] ,
93+ broker = broker ,
94+ client_id = client_id ,
9695 is_ssl = True ,
9796 socket_pool = pool ,
9897 ssl_context = ssl_context ,
@@ -109,7 +108,7 @@ def message(client, topic, msg):
109108aws_iot .on_publish = publish
110109aws_iot .on_message = message
111110
112- print ("Attempting to connect to %s" % mqtt_client .broker )
111+ print (f "Attempting to connect to { mqtt_client .broker } " )
113112aws_iot .connect ()
114113
115114# Start a blocking message loop...
0 commit comments