Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
2024-RSOC
【2024-RSOC】RT-Thread软件包的使用:阿里云监测温湿度
发布于 2024-07-29 17:29:02 浏览:313
订阅该版
[tocm] # DAY5 今天早上学习了软件包的使用,包括使用软件包来对片外flash进行分区管理与访问阿里云物联网平台。 ## 一、软件包与组件 ### 软件包(Software Package) #### 定义: 具有特定的功能,用来完成特定任务的一个程序或一组程序。可以理解为帮助我们完成了底层驱动的编写,只需使用里面提供的API即可。 #### 功能: 简化开发过程,封装了复杂的底层细节,使开发者能专注于应用逻辑。例如,使用AHT10软件包采集温湿度数据,无需编写复杂的I2C通信代码。 ### 组件(Component) #### 定义: 组件是一个可以独立开发、测试、部署和维护的软件单元,具有明确的功能边界,可在不同项目中重复使用。 #### 功能: 提高软件的模块化和可维护性,方便功能扩展、维护和测试。例如,文件系统和网络协议栈(如MQTT)都可作为独立组件。 ## 二、软件包的使用 ### AHT10软件包 #### 使能软件包 打开ENV,输入命令menuconfig.exe进入配置界面。然后按下"/"对AHT10软件包进行搜索。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/00fb6b84cd1346a0a3bcea6f30cdbdf8.png.webp) 搜索成功,对AHT10软件包进行使能。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/a8c5033bd0e05b32277e78211c946cf0.png) 保持并退出之后。使用`pkgs --update`更新软件包。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/406a5b12f4e690bb883e7138e775e129.png) ### Ali_iotkit软件包 #### 使能RW007 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/bd79751e461f988eac0a89d99965b29d.png) 修改RW007的引脚与星火一号的引脚一致 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/17dbb4f4fbc8e9db573f0137b2e4d49c.png.webp) #### 使能软件包 与AHT10软件包的使能同理。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/1eff6edce5a2a12b6dcd4a17652b9c23.png) 修改阿里云软件包三元组与product secret ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/3f7ee9e531826ce730b793e8dedf1bde.png) 使能案例 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/1d8b0a079416f3c6b32d230e8d172de4.png) 保存并退出,输入'pkg --update'进行更新 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240729/d237ab5b700132133d10eb6b94504df7.png) ### 作业代码 #### 上传温湿度 ```c /* * Copyright (C) 2015-2018 Alibaba Group Holding Limited * * Again edit by rt-thread group * Change Logs: * Date Author Notes * 2019-07-21 MurphyZhao first edit */ #include "rtthread.h" #include "dev_sign_api.h" #include "mqtt_api.h" #include "cJSON.h" #include "rtdbg.h" #include "main.h" #include "usart.h" #include "gpio.h" const char *ALI_TOPIC_GET = "/%s/%s/user/get"; const char *ALI_TOPIC_TLS_POST = "/sys/%s/%s/thing/event/property/post"; const char *ALI_TOPIC_TLS_POST_REPLY = "/sys/%s/%s/thing/event/property/post_reply"; const char *ALI_TOPIC_TLS_SET = "/%s/%s/user/set"; char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0}; char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0}; char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0}; void *HAL_Malloc(uint32_t size); void HAL_Free(void *ptr); void HAL_Printf(const char *fmt, ...); int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]); int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]); int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]); uint64_t HAL_UptimeMs(void); int HAL_Snprintf(char *str, const int len, const char *fmt, ...); double temp = 0; double humi = 0.1; uint8_t LightSwitch = 1; uint8_t led2 = 0; #define EXAMPLE_TRACE(fmt, ...) \ do { \ HAL_Printf("%s|%03d :: ", __func__, __LINE__); \ HAL_Printf(fmt, ##__VA_ARGS__); \ HAL_Printf("%s", "\r\n"); \ } while(0) rt_thread_t msg_receive = RT_NULL; void msg_receive_entrry(void *pclient) { while(1){ /* handle the MQTT packet received from TCP or SSL connection */ IOT_MQTT_Yield(pclient, 200); rt_thread_delay(1000); } } static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) { iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg; switch (msg->event_type) { case IOTX_MQTT_EVENT_PUBLISH_RECEIVED: /* print topic name and topic message */ EXAMPLE_TRACE("Message Arrived:"); EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic); EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload); EXAMPLE_TRACE("\n"); cJSON* cjson = cJSON_Parse(topic_info->payload);//将JSON字符串转换成JSON结构体 if(cjson == NULL) //判断转换是否成功 { } else { cJSON* params_ali = cJSON_GetObjectItem(cjson,"params"); //解析对象 //判断是否有相对应的内容,有才会进行数据提取 if(cJSON_GetObjectItem(params_ali, "temp") != RT_NULL){ rt_kprintf("temp: %d -> ",temp); temp = cJSON_GetObjectItem(params_ali, "temp")->valueint; rt_kprintf("%d\n",temp); } if(cJSON_GetObjectItem(params_ali, "humi") != RT_NULL){ rt_kprintf("humi: %.3lf -> ",humi); humi = cJSON_GetObjectItem(params_ali, "humi")->valuedouble; rt_kprintf("%.3lf\n",humi); } if(cJSON_GetObjectItem(params_ali, "LightSwitch") != RT_NULL){ rt_kprintf("LightSwitch: %d -> ",LightSwitch); LightSwitch = cJSON_GetObjectItem(params_ali, "LightSwitch")->valueint; rt_kprintf("%d\n",LightSwitch); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, !LightSwitch); } if(cJSON_GetObjectItem(params_ali, "led2") != RT_NULL){ rt_kprintf("led2状态: %d -> ",!led2); led2 = cJSON_GetObjectItem(params_ali, "led2")->valueint; rt_kprintf("%d\n",led2); // HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, !led2); } cJSON_Delete(cjson); } // } break; default: break; } } static int topic_subscribe(void *handle, const char *ALI_TOPIC, iotx_mqtt_qos_t IOTX_MQTT_QOS){ int res = 0; char *topic = NULL; int topic_len = 0; topic_len = strlen(ALI_TOPIC) + 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, ALI_TOPIC, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS, example_message_arrive, NULL); if (res < 0) { EXAMPLE_TRACE("%s subscribe failed",ALI_TOPIC); HAL_Free(topic); return -1; } HAL_Free(topic); return 0; } static int example_subscribe(void *handle) { topic_subscribe(handle, ALI_TOPIC_GET, IOTX_MQTT_QOS1); // topic_subscribe(handle, ALI_TOPIC_TLS_POST, IOTX_MQTT_QOS1); topic_subscribe(handle, ALI_TOPIC_TLS_SET, IOTX_MQTT_QOS1); // topic_subscribe(handle, ALI_TOPIC_TLS_POST_REPLY, IOTX_MQTT_QOS0); return 0; } static int example_publish(void *handle)//向Topic主题发布属性函数 { int res = 0; const char *ALI_TOPIC_TLS_POST_payload = "{\"params\" : { \"temperature\":%.3lf } }"; char *topic = NULL; int topic_len = 0; int payload_len = 0; char *payload = NULL; payload_len = strlen(ALI_TOPIC_TLS_POST_payload)+4; 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,ALI_TOPIC_TLS_POST_payload,temp); temp = temp +0.1; // humi = humi + 1.2; topic_len = strlen(ALI_TOPIC_TLS_POST) + 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, ALI_TOPIC_TLS_POST, 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); HAL_Free(payload); return -1; } HAL_Free(topic); HAL_Free(payload); return 0; } static void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) { EXAMPLE_TRACE("msg->event_type : %d", msg->event_type); } /* * NOTE: About demo topic of /${productKey}/${deviceName}/user/get * * The demo device has been configured in IoT console (https://iot.console.aliyun.com) * so that its /${productKey}/${deviceName}/user/get can both be subscribed and published * * We design this to completely demonstrate publish & subscribe process, in this way * MQTT client can receive original packet sent by itself * * For new devices created by yourself, pub/sub privilege also requires being granted * to its /${productKey}/${deviceName}/user/get for successfully running whole example */ static int mqtt_example_main(int argc, char *argv[]) { void *pclient = NULL; int res = 0; iotx_mqtt_param_t mqtt_params; HAL_GetProductKey(DEMO_PRODUCT_KEY); HAL_GetDeviceName(DEMO_DEVICE_NAME); HAL_GetDeviceSecret(DEMO_DEVICE_SECRET); EXAMPLE_TRACE("mqtt example"); memset(&mqtt_params, 0x0, sizeof(mqtt_params));//对较大的结构体或数组进行清零操作的一种最快方法 mqtt_params.read_buf_size = 1024; mqtt_params.handle_event.h_fp = example_event_handle; pclient = IOT_MQTT_Construct(&mqtt_params); if (NULL == pclient) { EXAMPLE_TRACE("MQTT construct failed"); return -1; } res = example_subscribe(pclient); if (res < 0) { IOT_MQTT_Destroy(&pclient); return -1; } msg_receive = rt_thread_create("msg_receive", msg_receive_entrry, pclient, 4096, 14, 10); if(msg_receive == RT_NULL){ rt_kprintf("rt_thread_create msg_receive failed...\n"); } rt_thread_startup(msg_receive); while (1) { example_publish(pclient); rt_thread_delay(15000); } return 0; } static rt_thread_t tid1 = RT_NULL; int mqtt_thread(void) //创建mqtt线程 { tid1 = rt_thread_create("mqtt_thd", mqtt_example_main, RT_NULL, 4096, 14, 10); if (tid1 != RT_NULL) rt_thread_startup(tid1); return 0; } #ifdef FINSH_USING_MSH MSH_CMD_EXPORT(mqtt_thread, ali mqtt sample); #endif ```
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
TanZHi_RT
这家伙很懒,什么也没写!
文章
4
回答
0
被采纳
0
关注TA
发私信
相关文章
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
ulog
C++_cpp
at_device
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
8
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部