- # MQTT 服务器信息
- broker = "ip" # MQTT 服务器地址
- port = 1883 # 默认 MQTT 端口
- clientid = "ClientId" # 替换为你的 clientid
- username = "UserName" # 替换为你的 username
- password = "05b61b97bb8fb1bb9d5433e223fea16223f9b78b;hmacsha1" # 替换为你的 password
- # 上行请求 Topic
- up_topic = "$thing/up/property/00x/vibration" # 替换为你的上行请求 Topic
- # 下行响应 Topic
- down_topic = "$thing/down/property/00x/vibration" # 替换为你的下行响应 Topic
- # 构造 payload
- def generate_payload():
- return {
- "method": "report",
- "msgToken": "123",
- "timestamp": int(time.time() * 1000), # 当前时间戳(毫秒)
- "params": {
- "tem": transform_sensor_data(sensor_vibration.read_register(registeraddress=0, functioncode=3)),
- "accx": transform_sensor_data(sensor_vibration.read_register(registeraddress=10, functioncode=3)),
- "accy": transform_sensor_data(sensor_vibration.read_register(registeraddress=11, functioncode=3)),
- "accz": transform_sensor_data(sensor_vibration.read_register(registeraddress=12, functioncode=3)),
- "vx": transform_sensor_data(sensor_vibration.read_register(registeraddress=1, functioncode=3)),
- "vy": transform_sensor_data(sensor_vibration.read_register(registeraddress=2, functioncode=3)),
- "vz": transform_sensor_data(sensor_vibration.read_register(registeraddress=3, functioncode=3))
- }
- }
在物联网平台创建产品和设备,添加对应的物模型,具体为:
- "{\n "properties": [\n {\n "identifier": "tem",\n "tag": 1,\n "name": "温度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 2,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 0,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "float",\n "min": "0.000",\n "max": "800.000",\n "start": "0.000",\n "step": "0.010",\n "unit": "℃"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "accx",\n "tag": 1,\n "name": "x加速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "accy",\n "tag": 1,\n "name": "y加速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "accz",\n "tag": 1,\n "name": "z加速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "vx",\n "tag": 1,\n "name": "x速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "vy",\n "tag": 1,\n "name": "y速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n },\n {\n "identifier": "vz",\n "tag": 1,\n "name": "z速度",\n "desc": "",\n "required": false,\n "extendConfig": "",\n "isCanSceneLinkage": 1,\n "isShareAuthPerm": 0,\n "funcGroup": 1,\n "controlMode": 1,\n "userPerm": 3,\n "isHistory": 1,\n "isPassword": 2,\n "order": 0,\n "mode": "rw",\n "define": {\n "type": "int",\n "min": "0",\n "max": "500",\n "start": "0",\n "step": "1"\n },\n "isUseShadow": false,\n "isNoRecord": false\n }\n ],\n "profile": {\n "productID": "00x"\n }\n}"
后续成功将数据上传到物联网平台,实现设备的振动监测,具体为: