目录

ESP-01S通过AT指令上报数据到阿里云物模型

ESP-01S通过AT指令上报数据到阿里云物模型

ESP-01S使用AT指令上报数据到阿里云物模型

上篇文章介绍了如何用AT指令连接阿里云并进行通信:

但最终需要将传感器数据上报到云平台显示,所以需要建立阿里云物模型

阿里云平台建立物模型

在产品的功能定义中,点击编辑草稿,新建物模型

https://i-blog.csdnimg.cn/blog_migrate/b6c2a9c31306fd7dfe2d33b82e15ff40.png#pic_center

可以选择添加标准功能,也可以自己定义

https://i-blog.csdnimg.cn/blog_migrate/0ba0c9f0eee7b0468274c86a8b3f82e4.png#pic_center

选择添加标准功能,这些是云平台已经生成好的物模型,可以直接使用

https://i-blog.csdnimg.cn/blog_migrate/e24bc72b8b3c2bee674b812a200083bc.png#pic_center

选择自定义,可自己设置模型的参数,例如数据类型和取值范围等

https://i-blog.csdnimg.cn/blog_migrate/79e9b7416dca0b50cb3fce7073c582e0.png#pic_center

设置完成则生成以下物模型,最后记得点击发布上线,这里生成的只是草稿,要发布后才能在设备中使用

https://i-blog.csdnimg.cn/blog_migrate/f27204292782ade5e9ecad0c7dd0a598.png#pic_center

ESP-01S上报温湿度数据到物模型

首先获取物模型属性上报Topic

https://i-blog.csdnimg.cn/blog_migrate/9a1527d381b77c04bb3047475dc2ef5b.png#pic_center

在设备已经显示在线的前提下,发送下面AT指令,上报物模型数据

注意:ESP8266-1是设备号,要改为自己的设备,id可以随便写,params后面是标识符和对应值,改变值即可上传不同数据

继电器:

AT+MQTTPUB=0,"/sys/hiyfcAbCXmq/ESP8266-1/thing/event/property/post","{\"method\":\"thing.event.property.post\"\,\"id\":\"1231\"\,\"params\":{\"Relay\":0}\,\"version\":\"1.0.0\"}",1,0

温度:

AT+MQTTPUB=0,"/sys/hiyfcAbCXmq/ESP8266-1/thing/event/property/post","{\"method\":\"thing.event.property.post\"\,\"id\":\"1234\"\,\"params\":{\"temperature\":20}\,\"version\":\"1.0.0\"}",1,0

湿度:

AT+MQTTPUB=0,"/sys/hiyfcAbCXmq/ESP8266-1/thing/event/property/post","{\"method\":\"thing.event.property.post\"\,\"id\":\"1234\"\,\"params\":{\"Humidity\":88}\,\"version\":\"1.0.0\"}",1,0

https://i-blog.csdnimg.cn/blog_migrate/aeed840b09fed43409d0a9a9faf6c9db.png#pic_center

JSON格式物模型属性上报格式
{
  "method": "thing.event.property.post",
  "id": "162277852",
  "params": {
      "Relay": 1,
      "Humidity": 75.6,
      "temperature": 18
    },
    "version": "1.0.0"
}

JSON格式的可以在MQTTX软件中发送物模型数据

回到阿里云可以看到物模型数据已经上传成功,要打开实时刷新,数据便会自动更新,否则要刷新网页

https://i-blog.csdnimg.cn/blog_migrate/fd0a31e359e8847da2ea449270f81bc4.png#pic_center

阿里云下发物模型数据到ESP-01S

复制订阅权限的Topic

https://i-blog.csdnimg.cn/blog_migrate/233afdd562b27f2ca93c522e6eba3f2e.png#pic_center

ESP-01S发送AT指令,订阅该Topic

AT+MQTTSUB=0,"/sys/hiyfcAbCXmq/ESP8266-1/thing/service/property/set",1

https://i-blog.csdnimg.cn/blog_migrate/fe8430cbca6aae3bc7b966c5fb5b0e6c.png#pic_center

在阿里云平台的在线调试页面设置物模型参数,点击设置进行数据下发

https://i-blog.csdnimg.cn/blog_migrate/60c213fb59b737e6560a0ffadaa69cf9.png#pic_center

点击设置后,ESP-01S会收到数据,物模型数据被改变

https://i-blog.csdnimg.cn/blog_migrate/b00b85ded47ff7049f2b6cbb6374e605.png#pic_center