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")
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" )
2830
2931### Code ###
3032
@@ -76,23 +78,21 @@ def message(client, topic, msg):
7678 print ("Message from {}: {}" .format (topic , msg ))
7779
7880
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 } !" )
8284
8385# 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 )
8688
8789# 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 )
9191
9292# Set up a MiniMQTT Client
9393mqtt_client = MQTT .MQTT (
94- broker = secrets [ " broker" ] ,
95- client_id = secrets [ " client_id" ] ,
94+ broker = broker ,
95+ client_id = client_id ,
9696 is_ssl = True ,
9797 socket_pool = pool ,
9898 ssl_context = ssl_context ,
@@ -109,7 +109,7 @@ def message(client, topic, msg):
109109aws_iot .on_publish = publish
110110aws_iot .on_message = message
111111
112- print ("Attempting to connect to %s" % mqtt_client .broker )
112+ print (f "Attempting to connect to { mqtt_client .broker } " )
113113aws_iot .connect ()
114114
115115# Start a blocking message loop...
0 commit comments