环境
esp32 wroom-32
microPython v1.12
simple.py MQTTClient
控制台已经配置好了。
mqttClientId
mqttUsername
mqttPassword
三个值使用 MQTT.qx客户端可以连上(在控制台可以看见) ,但是用microPython 报
raceback (most recent call last):
File "boot.py", line 53, in
File "simple.py", line 86, in connect
IndexError: bytes index out of range
遇到同样的问题, 同esp32 mqtt.fx模拟接入没有问题,但是用micropyton接入的时候, File "simple.py", line 85, in connect TException: 5
网络上已经有使用python 模拟设备接入阿里云,手里刚好有块ESP32的小板子,在上面跑过Alios Things,然后接入阿里云。但是终究感觉麻烦,有的时候只是做一个很简单的应用,跑os确实不太方便。使用micropython直接操作ESP32很是方便,就想着如果能通过micropython 直接接入物联网,那就方便多了。可以非常简单地接入云端,而且操作简单。先放上主干部分,有时间再补充细节
from umqtt.simple import MQTTClient
import usocket as socket
import time
import wifi
wifi.connect()
#Demo_01
ProductKey = "a1Mf4HZ5k**"
ClientId = "1234|securemode=3,signmethod=hmacsha1|"
DeviceName = "Demo_01"
DeviceSecret = "****************************"
strBroker = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com"
Brokerport = 1883
user_name = "Demo_01&a1Mf4HZ5k**"
user_password = "***************************************"
print("clientid:",ClientId,"\n","Broker:",strBroker,"\n","User Name:",user_name,"\n","Password:",user_password,"\n")
def connect():
client = MQTTClient(client_id = ClientId,server= strBroker,port=Brokerport,user=user_name, password=user_password,keepalive=60)
#please make sure keepalive value is not 0
client.connect()
temperature =25.00
while temperature < 30:
temperature += 0.5
send_mseg = '{"params": {"IndoorTemperature": %s},"method": "thing.event.property.post"}' % (temperature)
client.publish(topic="/sys/a1Mf4HZ5kET/Demo_01/thing/event/property/post", msg=send_mseg,qos=1, retain=False)
time.sleep(3)
while True:
pass
#client.disconnect()
连接成功后和可以在设备运行状态下看到上传的温度数据
bad8af1f5f8ab29423b767426d8d5261b2490592
demo中做了一个温度递增的上传,以下是数据记录:
1efed9107b0c5b3a6e1c579d12a76fed099b9e84
做的过程中参考了一下链接,放在这里,读者可以阅读,以获得更多细节:
子设备接入
https://help.aliyun.com/document_detail/66641.html
ESP8266 and MicroPython - Part 2
https://www.home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2/
使用MQTT客户端连接阿里云MQTT服务器
https://yq.aliyun.com/articles/592279
使用Python模拟设备接入阿里云物联网的MQTT服务器
https://yq.aliyun.com/articles/162978
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。