我是靠谱客的博主 俏皮大碗,这篇文章主要介绍RT-Thread Studio使用笔记(七):通过4G模块连接到阿里云概述阿里云创建产品关于属性和事件RT-Thread配置,现在分享给大家,希望可以做个参考。

概述

    本文带大家一起,基于上篇文章中的的4G模块连接到阿里云平台

传送门:

RT-Thread Studio使用笔记(六):4G模块SIM7600的使用

https://blog.csdn.net/tigerots/article/details/108132814

参考资料:

https://help.aliyun.com/document_detail/89301.html?spm=a2c4g.11186623.6.706.bec8429dZDvMvG

 

阿里云创建产品

阿里云物联网平台地址:https://iot.console.aliyun.com/studio?spm=a2c56.12526802.1304866.1.743b107bMaky1X

 

关于属性和事件

 

阿里关于设备属性,事件的描述有专门的章节,我们一般选用Alink json格式

使用Alink格式的数据如下:

上行(Alink JSON):

  • 设备请求用的Topic: /sys/{productKey}/{deviceName}/thing/event/property/post

本例程使用的请求Topic

"/sys/a1ZkpqGqexK/DTU_4G_002/thing/event/property/post"

  • 设备请求用的payload数据, 应包含方法, ID, 参数几个重要选项

 {
      "method":  "thing.event.property.post",
      "id":  "20",
      "params": {
             "RGBColor": {
                     "Red": 20,
                     "Blue": 21,
                     "Green": 22
               }
      },
      "version": "1.0.0"
 }

 

  • 云响应设备Topic: /sys/{productKey}/{deviceName}/thing/event/property/post_reply

/sys/a1ZkpqGqexK/DTU_4G_002//thing/event/property/post_reply

  • 云响应设备的数据内容, code为200表示成功, ID号与请求ID号一致

{
     "code": 200,
      "data": {
      },
      "id": "45",
      "message": "success",
      "method": "thing.event.property.post",
      "version": "1.0"
 }

一定要了解,你的设备要订阅或发布的主题,否则无法进行嵌入式程序的编写。

 

RT-Thread配置

1. 使能阿里云的mqtt软件包,()如需要深入了解MQTT协议,建议大家观看超威的视频,裸机分析,讲的非常透彻),并根据阿里云上你的产品进行配置。

以上信息来自于阿里云设备管理,如下图所示:

2. 配置完成后,打开例程,即可根据阿里的主题规则,配置订阅和要发布的主题

复制代码
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
uint32_t id_cnt = 0; // 上传修改属性值 static int example_publish(void *handle) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *fmt2 = "{%s,%s,%s,%s}"; char *method = ""method": "thing.event.property.post"";// 方法 char id[16] = "";// 唯一ID char params[128] = "";// 参数 char *version = ""version":"1.0.0"";// 版本,固定 char *payload = NULL; int payload_len = 0; // 生成payload所用的参数 id_cnt ++; HAL_Snprintf(id, 16, ""id": "%d"", id_cnt); HAL_Snprintf(params, 128, ""params":{"CurrentHumidity":%d,"VehSpeed":%d,"Atmosphere":%d,"CurrentTemperature":%d}", (id_cnt%100), (id_cnt%1150), (id_cnt%10+990), (id_cnt%10+20)); // 生成payload payload_len = strlen(fmt2) + strlen(method) + strlen(id) + strlen(params) + strlen(version) + 1; payload = HAL_Malloc(payload_len); if (payload == NULL) { EXAMPLE_TRACE("memory not enough"); return -1; } memset(payload, 0, payload_len); HAL_Snprintf(payload, payload_len, fmt2, method, id, params, version); // 生成topic topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1; topic = HAL_Malloc(topic_len); if (topic == NULL) { EXAMPLE_TRACE("memory not enough"); return -1; } memset(topic, 0, topic_len); HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); // 发布主题 res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload)); if (res < 0) { EXAMPLE_TRACE("publish failed, res = %d", res); HAL_Free(topic); return -1; } HAL_Free(topic); return 0; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
static int example_subscribe(void *handle) { int res = 0; const char *fmt = "/%s/%s/user/get"; char *topic = NULL; int topic_len = 0; topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1; topic = HAL_Malloc(topic_len); if (topic == NULL) { EXAMPLE_TRACE("memory not enough"); return -1; } memset(topic, 0, topic_len); HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL); if (res < 0) { EXAMPLE_TRACE("subscribe failed"); HAL_Free(topic); return -1; } HAL_Free(topic); return 0; }

 

编译运行后,中断一直提示保存,出现异常,是因为(基于上一篇博客《RT-Thread Studio使用笔记(六):4G模块SIM7600的使用》)中的主机地址数组长度不够,此处解析的阿里云地址数据比较长,默认为32字节,会导致内存溢出

 

修改完成后, 即可通过控制台,成功连接到阿里云,并且能够实现主题的发布与订阅。

 

各位,不好意思,这篇博文遗漏比较多,不够详细, 本来计划边写代码,边写博客,但是因为中间公司的开发任务比较多,耽误了时间,所以没有及时完成博客,现在想补,但是手头已经没有的板子,无法截取相关操作的图片了,如果有需要交流该问题的小伙伴,请私信我。

最后

以上就是俏皮大碗最近收集整理的关于RT-Thread Studio使用笔记(七):通过4G模块连接到阿里云概述阿里云创建产品关于属性和事件RT-Thread配置的全部内容,更多相关RT-Thread内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部