Reputation: 1
I'm trying to connect my Raspberry Pi Pico W to AWS IoT Core for sending sensor data. I've installed the umqtt.simple library and followed some online guides, but I'm encountering errors when handling the certificates and establishing the connection.
I'm aiming for a secure connection and would appreciate any help with code snippets or best practices for achieving this.
Specifically, I'm unsure about:
How to properly read and use the downloaded certificate and private key files. The syntax for establishing a connection using umqtt.simple with secure communication enabled. Thanks in advance for any assistance!
This is the code I used establishing the connection.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# AWS IoT Core - RPi Pico W Demo
# Required imports
import time
import machine
import network
import ujson
from umqtt.simple import MQTTClient
[code][code][code]
###############################################################################
### START CODE MODIFICATION ###################################################
###############################################################################
# Wifi Name / SSID
SSID = b'<your wifi network name>'
# Wifi Password
PASS = b'<your wifi password>'
# AWS ThingName is used for the Client ID (Best Practice). Example: RaspberryPiPicoW
CLIENT_ID = b'RaspberryPiPicoW'
# AWS Endpoint (Refer to "Creating a Thing in AWS IoT Core" step 13)
AWS_ENDPOINT = b'<your IoT Endpoint value here>'
###############################################################################
### END CODE MODIFICATION #####################################################
###############################################################################
# AWS IoT Core publish topic
PUB_TOPIC = b'/' + CLIENT_ID + '/temperature'
# AWS IoT Core subscribe topic
SUB_TOPIC = b'/' + CLIENT_ID + '/light'
# Reading Thing Private Key and Certificate into variables for later use
with open('/certs/key.der', 'rb') as f:
DEV_KEY = f.read()
# Thing Certificate
with open('/certs/cert.der', 'rb') as f:
DEV_CRT = f.read()
# Define light (Onboard Green LED) and set its default state to off
light = machine.Pin("LED", machine.Pin.OUT)
light.off()
# Wifi Connection Setup
def wifi_connect():
print('Connecting to wifi...')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASS)
while wlan.isconnected() == False:
light.on()
print('Waiting for connection...')
time.sleep(0.5)
light.off()
time.sleep(0.5)
print('Connection details: %s' % str(wlan.ifconfig()))
# Callback function for all subscriptions
def mqtt_subscribe_callback(topic, msg):
print("Received topic: %s message: %s" % (topic, msg))
if topic == SUB_TOPIC:
mesg = ujson.loads(msg)
if 'state' in mesg.keys():
if mesg['state'] == 'on' or mesg['state'] == 'ON' or mesg['state'] == 'On':
light.on()
print('Light is ON')
else:
light.off()
print('Light is OFF')
# Read current temperature from RP2040 embeded sensor
def get_rpi_temperature():
sensor = machine.ADC(4)
voltage = sensor.read_u16() * (3.3 / 65535)
temperature = 27 - (voltage - 0.706) / 0.001721
return temperature
# Connect to wifi
wifi_connect()
# Set AWS IoT Core connection details
mqtt = MQTTClient(
client_id=CLIENT_ID,
server=AWS_ENDPOINT,
port=8883,
keepalive=5000,
ssl=True,
ssl_params={'key':DEV_KEY, 'cert':DEV_CRT, 'server_side':False})
# Establish connection to AWS IoT Core
mqtt.connect()
# Set callback for subscriptions
mqtt.set_callback(mqtt_subscribe_callback)
# Subscribe to topic
mqtt.subscribe(SUB_TOPIC)
# Main loop - with 5 sec delay
while True:
# Publisg the temperature
message = b'{"temperature":%s, "temperature_unit":"Degrees Celsius"}' % get_rpi_temperature()
print('Publishing topic %s message %s' % (PUB_TOPIC, message))
# QoS Note: 0=Sent zero or more times, 1=Sent at least one, wait for PUBACK
# See https://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html
mqtt.publish(topic=PUB_TOPIC, msg=message, qos=0)
# Check subscriptions for message
mqtt.check_msg()
time.sleep(5)
I used the code above to connect my raspberry pi Pico W to connect it to AWS IoT core. The code works till the part where it helps connect me to the WiFi. But it stops working once it reaches the code for establishing a connection with IoT core. It gives the following error.
Traceback (most recent call last): File "", line 99, in TypeError: unexpected keyword argument 'ssl_params'
Upvotes: 0
Views: 263
Reputation: 11
According to the Official Repository of Micropython Core libraries,
class MQTTClient:
def __init__(
self,
client_id,
server,
port=0,
user=None,
password=None,
keepalive=0,
ssl=None,
):
There no parameter for the 'ssl_params' I think you are referencing mpython documentation instead of Micropython documentation
Upvotes: 1