zeg*_*las 5 mqtt micropython aws-iot esp32
我已将 ESP32 注册为 AWS IoT 上的一个事物,并下载了其各自的证书以及公钥和私钥。还通过我的终端中的以下命令验证了这些连接是否正确:
openssl s_client -connect host.iot.region.amazonaws.com:8443 -CAfile AmazonRootCA1.pem -cert certificate.pem.crt -key private.pem.key
Run Code Online (Sandbox Code Playgroud)
这是我的 (main.py) 使用 MicroPython 连接到 AWS IoT 的简单代码
import machine
from network import WLAN
import network
from umqtt.simple import MQTTClient
# AWS endpoint parameters.
HOST = b'HOST' # ex: b'abcdefg1234567'
REGION = b'REGION' # ex: b'us-east-1'
CLIENT_ID = "CLIENT_ID" # Should be unique for each device connected.
AWS_ENDPOINT = b'%s.iot.%s.amazonaws.com' % (HOST, REGION)
keyfile = '/certs/private.pem.key'
with open(keyfile, 'r') as f:
key = f.read()
certfile = "/certs/certificate.pem.crt"
with open(certfile, 'r') as f:
cert = f.read()
# SSL certificates.
SSL_PARAMS = {'key': key,'cert': cert, 'server_side': False}
# Setup WiFi connection.
wlan = network.WLAN( network.STA_IF )
wlan.active( True )
wlan.connect( "SSID", "PASSWORD" )
while not wlan.isconnected():
machine.idle()
# Connect to MQTT broker.
mqtt = MQTTClient( CLIENT_ID, AWS_ENDPOINT, port = 8883, keepalive = 10000, ssl = True, ssl_params = SSL_PARAMS )
mqtt.connect()
# Publish a test MQTT message.
mqtt.publish( topic = 'test', msg = 'hello world', qos = 0 )
Run Code Online (Sandbox Code Playgroud)
但当我尝试连接时出现此错误:
(-17168, 'MBEDTLS_ERR_RSA_PRIVATE_FAILED+MBEDTLS_ERR_MPI_ALLOC_FAILED')
Run Code Online (Sandbox Code Playgroud)
经过一番努力,我让它发挥作用。我必须使用 idf3 MicroPython 二进制文件,
esp32-idf3-20191220-v1.12.bin
Run Code Online (Sandbox Code Playgroud)
idf4 二进制文件和 v1.12 之后的 idf3 不起作用。存在堆不够和内存分配问题。
- - - - - - 编辑 - - - - - -
新闻更新!基于 idf4 的新 v1.15 版 MicroPython 可与 AWS MQTT for IoT 配合使用。
| 归档时间: |
|
| 查看次数: |
2094 次 |
| 最近记录: |