腾讯云物联网平台使用报告

前言:本文关于mqtt部分和前面两篇文章(硬件快速连接到阿里云物联网平台、ESP32 MQTT连接到中移OneNET物联网平台(附源码))大体相同,区别主要是腾讯云部分产品和设备的创建。

腾讯云创建产品和设备

1、物联网通信

2、创建产品

3、创建设备

产品列表---》设备列表---》添加新设备

4、设备信息

上面的client id 、mqtt usernme、mqtt password都是我们需要用到的。

除了上面3个参数,还需要知道Broker Address和Broker Port,根据腾讯官方的资料Broker Address和Broker Port分别是如下格式的:

Broker Address:PRODUCT_ID.iotcloud.tencentdevices.com

Broker Port:1883

5、订阅和发布主题

产品列表---》设备列表---》权限列表,可以看到如下3个主题:

MQTT.fx模拟设备接入

1、配置客户端

2、建立连接

3、订阅和发布主题

订阅成功之后,如下面的显示:

上报数据:Publish(发布)

腾讯云日志查看:

下发数据:设备列表---》点进相应的设备里面---》在线调试

客户端查看:

ESP32设备连接到腾讯云

1、创建产品

创建产品和设备、客户端参数的生成和上面一样的方法;创建一个温湿度上报到腾讯云的产品和设备。

2、MQTT库

ESP32需要使用到MQTT库,使用网上开源的umqtt:https://github.com/micropython/micropython-lib/blob/master/umqtt.simple/umqtt/simple.py需要把这个文件导入到ESP32中。

3、温湿度

MicroPython本身内置了dht模块,支持DHT11、DHT22。

4、硬件连接

VCC <------>3V3

GND<------>GND

DAT <------>G4

5、脚本编写

程序设计:定义了4个函数,ConnectWifi(ssid,passwd)用于连接wifi,ReadTemHum()用于读取温湿度,sub_cb(topic,msg)订阅主题回调,apptimerevent(mytimer)定时器回调函数。

代码语言:javascript
复制
from umqtt.simple import MQTTClient
from machine import Pin
import network
import time
import machine
import dht
from machine import Timer

SSID=""
PASSWORD="
*"

SERVER ='LGSODS81VJ.iotcloud.tencentdevices.com' #MQTT Server: LGSODS81VJ.iotcloud.tencentdevices.com
CLIENT_ID = "LGSODS81VJESP32Devcice1" #设备ID
#PORT=1883
username='LGSODS81VJESP32Devcice1;12010126;C2PYL;1622167250'
password='ecd65158e4c9cdbfe48d116ca08ff069589f305936d8748fee269396207b01f8;hmacsha256'

publish_TOPIC = 'LGSODS81VJ/ESP32Devcice1/data'
subscribe_TOPIC ='LGSODS81VJ/ESP32Devcice1/data'

client=None
mydht=None

def sub_cb(topic, msg):
print((topic, msg))

def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #Activate the network interface
wlan.disconnect() #Disconnect the last connected WiFi
wlan.connect(ssid,passwd) #connect wifi
while(wlan.ifconfig()[0]=='0.0.0.0'):
time.sleep(1)
print(wlan.ifconfig())

def apptimerevent(mytimer):
try:
sensordata=ReadTemHum()
mymessage='{"CurrentTemperature": %d ,"CurrentHumidity": %d }'%(sensordata[0],sensordata[1])
client.publish(topic=publish_TOPIC,msg= mymessage, retain=False, qos=0)
except Exception as ex_results2:
print('exception',ex_results2)
mytimer.deinit()

finally:

machine.reset()

#Catch exceptions,stop program if interrupted accidentally in the 'try'

def ReadTemHum():
mydht.measure()
tem=mydht.temperature()
hum=mydht.humidity()
data=[tem,hum]
print(data)

return data

if name=='main':
try:
mydht=dht.DHT11(machine.Pin(4))
connectWifi(SSID,PASSWORD)
client = MQTTClient(CLIENT_ID, SERVER,0,username,password,60) #create a mqtt client
print(client)
client.set_callback(sub_cb) #set callback
client.connect() #connect mqtt
client.subscribe(subscribe_TOPIC) #client subscribes to a topic
mytimer=Timer(0)
mytimer.init(mode=Timer.PERIODIC, period=5000,callback=apptimerevent)
while True:
client.wait_msg() #wait message

except Exception  as ex_results:
    print(&#39;exception1&#39;,ex_results)
finally:
    if(client is not None):
        client.disconnect()
    wlan.disconnect()
    wlan.active(False)</code></pre></div></div><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">数据以JSON格式上传:</code></pre></div></div><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">mymessage=&#39;{&#34;CurrentTemperature&#34;: %d ,&#34;CurrentHumidity&#34;: %d }&#39;%(sensordata[0],sensordata[1])</code></pre></div></div><p>产品列表---》设备---》云日志---》内容日志---》可以查看上报的数据</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1723301795543516231.png" /></div></div></div></figure><p>接收数据:</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1723301795856075081.png" /></div></div></div></figure><p>在行为日志里面可以看到,设备发送到平台的,平台发送到设备的:</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1723301796114362752.png" /></div></div></div></figure><p>——————END——————</p>