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.
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.
Code:
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.# SPDX-License-Identifier: MIT-0# AWS IoT Core - RPi Pico W Demo# Required importsimport timeimport machineimport networkimport ujsonfrom umqtt.simple import MQTTClient[code][code][code]################################################################################## START CODE MODIFICATION ################################################################################################################################### Wifi Name / SSIDSSID = b'<your wifi network name>'# Wifi PasswordPASS = b'<your wifi password>'# AWS ThingName is used for the Client ID (Best Practice). Example: RaspberryPiPicoWCLIENT_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 topicPUB_TOPIC = b'/' + CLIENT_ID + '/temperature'# AWS IoT Core subscribe topicSUB_TOPIC = b'/' + CLIENT_ID + '/light'# Reading Thing Private Key and Certificate into variables for later usewith open('/certs/key.der', 'rb') as f: DEV_KEY = f.read()# Thing Certificatewith open('/certs/cert.der', 'rb') as f: DEV_CRT = f.read()# Define light (Onboard Green LED) and set its default state to offlight = machine.Pin("LED", machine.Pin.OUT)light.off()# Wifi Connection Setupdef 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 subscriptionsdef 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 sensordef 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 wifiwifi_connect()# Set AWS IoT Core connection detailsmqtt = 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 Coremqtt.connect()# Set callback for subscriptionsmqtt.set_callback(mqtt_subscribe_callback)# Subscribe to topicmqtt.subscribe(SUB_TOPIC)# Main loop - with 5 sec delaywhile 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) Statistics: Posted by bensamuel — Sat Apr 20, 2024 5:38 am