一、设计思路:软件仿真是利用MQTT协议模拟前端设备与OneNet平台进行通信,OneNet云平台利用MQTT协议下发指令。
二、相关代码
初始化:
复制代码
1
2
3
4
5
6
7
8BrokerHost = '183.230.40.39' # OneNET使用TCP方式连接时的主机地址 BrokerPort = 6002 # OneNET使用TCP方式连接时的主机端口号 DeviceId = 'xxxxxxxxx' # 设备ID ProductId = 'xxxxxx' # 产品ID APIKey = "xxxxxxxxxxxxxxxxx" # MasterAPIKey headers = {'api-key': APIKey} url_post = "https://api.heclouds.com/devices/" + DeviceId + "/datapoints" # 数据点 url_get = "https://api.heclouds.com/devices/" + DeviceId + "/datastreams" # 数据流
MQTT连接结果:
复制代码
1
2
3
4
5def on_connect(client, userdata, flags, rc): if rc != 0: print("连接失败:" + mqtt.connack_string(rc)) return print("***连接成功***")
消息发送:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19def MQTT_send(): Chang = random.uniform(-5, 15) Temperature= 20+Chang Temperature=round(Temperature, 2) data = { 'datastreams': [ { 'id': 'Temperature', # id为数据流名称 'datapoints': [ { 'value': Temperature } ] } ] } jdata = json.dumps(data).encode("utf-8") r = requests.post(url=url_post, headers=headers, data=jdata) print("发送成功:", r.text)
消息发送回调:
复制代码
1
2def on_publish(client, userdata, mid): print("[on_publish] mid:" + str(mid))
复制代码
1broker响应订阅请求时被调用:
复制代码
1
2
3def on_subscribe(client, userdata, mid, granted_qos): print("***** Broker响应订阅请求*****") print(granted_qos)
复制代码
1当与代理断开连接时调用:
复制代码
1
2
3def on_disconnect(client, userdata,rc=0): logging.debug("DisConnected result code "+str(rc)) client.loop_stop()
复制代码
1从服务器接收发布消息时的回调:
复制代码
1
2
3
4def on_message(client, userdata, msg): print("----------- 接收到消息-----------") # print(msg.topic + ":" + msg.payload.decode("utf-8")) print('接收到发布消息:'+msg.payload.decode("utf-8"))
实现代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84# -*- coding: utf-8 -*- """ @author: 浅笑醉红楼.(3303295829@qq.com) """ import paho.mqtt.client as mqtt import json import random import time import requests import logging BrokerHost = '183.230.40.39' # OneNET使用TCP方式连接时的主机地址 BrokerPort = 6002 # OneNET使用TCP方式连接时的主机端口号 DeviceId = 'xxxxxxxxx' # 设备ID ProductId = 'xxxxxx' # 产品ID APIKey = "xxxxxxxxxxxxxxxxx" # MasterAPIKey header = {'api-key': APIKey} url_post = "https://api.heclouds.com/devices/" + DeviceId + "/datapoints" # 数据点 url_get = "https://api.heclouds.com/devices/" + DeviceId + "/datastreams" # 数据流 # 传输数据,随机数模拟 def MQTT_send(): Chang = random.uniform(-5, 15) Temperature= 20+Chang Temperature=round(Temperature, 2) data = { 'datastreams': [ { 'id': 'Temperature', # id为数据流名称 'datapoints': [ { 'value': Temperature } ] } ] } jdata = json.dumps(data).encode("utf-8") r = requests.post(url=url_post, headers=header, data=jdata) print("发送成功:", r.text) def MQTT_get(): r = requests.get(url=url_get, headers=header) print("返回成功:n", r.text) def on_connect(client, userdata, flags, rc): if rc != 0: print("连接失败:" + mqtt.connack_string(rc)) return print("***连接成功***") def on_message(client, userdata, msg): print("---------- 接收到消息 ----------") # print(msg.topic + ":" + msg.payload.decode("utf-8")) print('接收到发布消息:'+msg.payload.decode("utf-8")) def on_subscribe(client, userdata, mid, granted_qos): print("***** Broker响应订阅请求*****") print(granted_qos) def on_publish(client, userdata, mid): print("[on_publish] mid:" + str(mid)) def on_disconnect(client, userdata,rc=0): logging.debug("DisConnected result code "+str(rc)) client.loop_stop() if __name__ == "__main__": client = mqtt.Client(client_id=DeviceId, protocol=mqtt.MQTTv311) client.on_connect = on_connect client.on_publish = on_publish client.on_message = on_message client.on_subscribe = on_subscribe client.username_pw_set(username=ProductId, password=APIKey) client.connect(host=BrokerHost, port=BrokerPort, keepalive=60) # client.loop_forever() while (True): client.loop_start() on_disconnect(client, client.username_pw_set) MQTT_send() time.sleep(2) # 等待两秒 MQTT_get() t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) print('数据上传成功:',t)
最后
以上就是等待蜗牛最近收集整理的关于基于python的onenet物联网平台温度数据仿真的全部内容,更多相关基于python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复