Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
夏令营
【2024-RSOC】RT-Thread软件包和组件的应用
发布于 2024-07-27 01:33:41 浏览:106
订阅该版
[tocm] # RT-Thread 软件包和组件的学习 环境:rt-thread:master分支 板卡:星火一号 # 一、采集温湿度上传MQTT云平台 ## 1.软件包的优势 星火一号的板载了AHT10,如果我们没用软件包的形式想去读取温湿度的话,那么我们是不是得自己去查找AHT10的手册,自己用IIC去读取相对于的寄存器,但是如果有了软件包之后我们就可以用别人提取写好的API去读取温湿度,这样下来大大提高了开发的效率。 那么我们该如何去查找对应的软件包呢? 可以现在官网上查看是否又对应的软件包:[软件包官网](https://packages.rt-thread.org/) 如果有的话,我们就可以使用env将对应的包使能。 ## 2.AHT10读取温湿度 ### a.使能AHT10软件包 如何知道这个软件包在那个具体的位置呢? 可以使用env中menuconfig的搜索功能或者是在软件包的官网搜索知道他的分类。 * menuconfig中搜索 ![image-20240726205221876](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115720.png) 可以看到按下键盘中的`/`就可以跳转对应的位置 我们按下`/`搜索AHT10,就可以看到有关AHT10的相关配置 ![image-20240726205335171](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115721.png) ![image-20240726205421206](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115722.png) * 从官网中查看相对于的分类 ![image-20240726205820684](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407262059252.png) 点进去可以查看到该软件包的分类。 ![image-20240726205953390](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115723.png) ![image-20240726210010328](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115724.png) ![image-20240726210026204](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115725.png) ![image-20240726210049617](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115726.png) ![image-20240726210112384](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115727.png) 我们就可以通过对应的分类路径,找到AHT10的软件包 我们就可以使能相对于的软件包,保持并退出之后。使用`pkgs --update`更新软件包。 ![image-20240726205545140](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115728.png) 就可以看到相对于的软件包从github中拉取了下来。 如果因为网络问题拉取不下来软件包,可以切换国内的gitee源。 在`env`中输入 `menuconfig -s`,进入env的配置,设置下载的服务,选择gitee。 ![image-20240726210317968](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115729.png) ![image-20240726210339508](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115730.png) ![image-20240726210348788](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115731.png) ### b.如何使用AHT10的API 一般对应的软件包的作者都会写Readme文件来教使用者如何简单的使用。 ![image-20240726210650528](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115732.png) 我们就可以快速的通过Readme文件去使用API对AHT10进行读取温湿度。 以下是我通过readme文档自己编写的一个读取温湿度的demo ~~~c #include
#include
#include
#include "aht10.h" //星火一号AHT10使用的为i2c3 #define AHT10_I2C_BUS "i2c3" //定义线程的信息 #define THREAD_PRIORITY 25 //线程优先级 #define THREAD_STACK_SIZE 2048 //线程栈大小 #define THREAD_TIMSLICE 5 //线程时间片 //AHT线程指针 rt_thread_t AHT10 = RT_NULL; void aht10_demo(void * paramenter) { aht10_device_t aht10_dev = RT_NULL; aht10_dev = aht10_init(AHT10_I2C_BUS); if(aht10_dev == RT_NULL) { rt_kprintf("aht10 init fail\n"); return; } float humi = 0,temp = 0; //读取温湿度 while(1) { temp = aht10_read_temperature(aht10_dev); humi = aht10_read_humidity(aht10_dev); rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10); rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10); rt_thread_mdelay(1000); } } void aht10_task(void) { AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY,THREAD_TIMSLICE); if(AHT10 == RT_NULL) { rt_kprintf("aht10 thread create fail\n"); return; } rt_thread_startup(AHT10); return; } MSH_CMD_EXPORT(aht10_task,aht10 read thread); ~~~ ![image-20240726213224161](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115733.png) 通过控制台可以看到温湿度读取成功。 ps:rt_kprintf()本身是不支持浮点型输出的,可以使用`rt_kpintf_full`来使能浮点型输出。 ![image-20240726213606504](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115735.png) ## 3.MQTT上传阿里云 ### a.创建阿里云实例 阿里云物联网平台:[官网](https://www.aliyun.com/product/iot/iot_instc_public_cn) * 登陆阿里云物联网平台,选择管理控制台 ![image-20240726214206046](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115736.png) * 创建一个公共的实例 ![image-20240726214245498](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115737.png) * 创建一个产品 ![image-20240726214322773](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115738.png) ![image-20240726214422733](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115739.png) * 创建产品 ![image-20240726214541015](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115740.png) 此时我们的产品以及设备就以及创建好了,我们只需要一些关键的参数即可。 ### b.打开RW007 ![image-20240726215014950](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115741.png) 修改RW007的引脚与星火一号对应。 ![image-20240726215041279](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115742.png) ### c.使能阿里云软件包 打开云端 ![image-20240726215157510](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115743.png) 使能阿里云,并配置相应的参数。 ![image-20240726215226326](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115744.png) ![image-20240726215316495](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115745.png) 这几个参数可以在阿里云的产品以及设备中获取。 ![image-20240726221130496](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115746.png) ![image-20240726215616709](C:/Users/CYFSybW/AppData/Roaming/Typora/typora-user-images/image-20240726215616709.png![image-20240726221006794](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115747.png) 把相对于的参数复制到menuconfig中。 使能案例。 ![image-20240726215949862](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115748.png) 推出menuconfig并保存,更新包`pkgs --update`,编译`scons`,可以看到有报错,是因为新版RTT API的变得,把对应报错的文件中的RT_WEAK改为小写即可。 ![image-20240726220103091](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115749.png) 再次编译即可。下载程序。 连接wifi,wifi的频段必须在2.4Ghz。 ![image-20240726220457785](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115750.png) 按下TAB,可以看到有阿里云的mqtt示例。 ![image-20240726220532253](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115751.png) ![image-20240726221446335](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115752.png) ### d.上传物模型通信的主题 物模型通信的主题 ![image-20240726222123371](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115753.png) 设置相对应的物模型功能。 ![image-20240726222228848](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115754.png) 参考示例代码,修改发布温湿度的主题。 aht_publish.h ~~~c #ifndef __AHT_PUBLISH_H__ #define __AHT_PUBLISH_H__ #include
typedef struct aht_publish { /* data */ rt_int32_t temp_int; rt_int32_t temp_dec; rt_int32_t humi_int; rt_int32_t humi_dec; }aht_publish; #endif ~~~ aht_publish.c ~~~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 "dev_sign_api.h" #include "mqtt_api.h" #include "aht_publish.h" #include
#include
#include "aht10.h" 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, ...); #define EXAMPLE_TRACE(fmt, ...) \ do \ { \ HAL_Printf("%s|%03d :: ", __func__, __LINE__); \ HAL_Printf(fmt, ##__VA_ARGS__); \ HAL_Printf("%s", "\r\n"); \ } while (0) 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"); break; default: break; } } 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; } static int example_publish(void *handle,aht_publish * data) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *payload = "{\"params\":{\"CurrentTemperature\":%d.%d,\"CurrentHumidity\":%d.%d}}"; char message[256]; HAL_Snprintf(message,256,payload,data->temp_int,data->temp_dec,data->humi_int,data->humi_dec); 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, message, strlen(message)); if (res < 0) { EXAMPLE_TRACE("publish failed, res = %d", res); HAL_Free(topic); return -1; } HAL_Free(topic); 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); } /*-----------------------------------------------------------------------------------*/ //星火一号AHT10使用的为i2c3 #define AHT10_I2C_BUS "i2c3" //定义线程的信息 #define THREAD_PRIORITY 10 //线程优先级 #define THREAD_STACK_SIZE 2048 //线程栈大小 #define THREAD_TIMSLICE 5 //线程时间片 //发送数据邮箱 rt_mailbox_t aht10_mailbox = RT_NULL; //AHT线程指针 rt_thread_t AHT10 = RT_NULL; void aht10_demo(void * paramenter) { aht10_device_t aht10_dev = RT_NULL; aht10_dev = aht10_init(AHT10_I2C_BUS); if(aht10_dev == RT_NULL) { rt_kprintf("aht10 init fail\n"); return; } float humi = 0,temp = 0; aht_publish data = {0}; //读取温湿度 while(1) { temp = aht10_read_temperature(aht10_dev); humi = aht10_read_humidity(aht10_dev); data.temp_int = (int)temp; data.temp_dec = (int)(temp*10)%10; data.humi_int = (int)humi; data.humi_dec = (int)(humi*10)%10; rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10); rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10); rt_mb_send(aht10_mailbox,(rt_uint32_t)&data); rt_thread_mdelay(1000); } } void aht10_task(void) { AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,9,THREAD_TIMSLICE); if(AHT10 == RT_NULL) { rt_kprintf("aht10 thread create fail\n"); return; } rt_thread_startup(AHT10); return; } void mqtt_example_main(void *parameter) { void *pclient = NULL; int res = 0; int loop_cnt = 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.handle_event.h_fp = example_event_handle; pclient = IOT_MQTT_Construct(&mqtt_params); if (NULL == pclient) { EXAMPLE_TRACE("MQTT construct failed"); return; } res = example_subscribe(pclient); if (res < 0) { IOT_MQTT_Destroy(&pclient); return; } aht_publish * data; while (1) { if(rt_mb_recv(aht10_mailbox,(rt_uint32_t *)&data,RT_WAITING_FOREVER) == RT_EOK) { example_publish(pclient,data); } IOT_MQTT_Yield(pclient, 200); } } rt_thread_t MQTT_Thread; void MQTT_Creat_Thread(void) { // 创建线程 MQTT_Thread = rt_thread_create("MQTT_Thread", mqtt_example_main, RT_NULL, 4096, 10, 5); // 创建成功就启动 if (MQTT_Thread != RT_NULL) { rt_thread_startup(MQTT_Thread); } else { rt_kprintf("MQTT_Thread_Thread Create Fail"); } } void aht10_publish(void) { aht10_mailbox = rt_mb_create("aht10",10,RT_IPC_FLAG_PRIO); if(aht10_mailbox == RT_NULL) { rt_kprintf("create mailbox fail\n"); return; } aht10_task(); MQTT_Creat_Thread(); } MSH_CMD_EXPORT(aht10_publish, aht10 publish init); ~~~ 结果: ![image-20240726231439909](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115755.png) # 二、RT-thread 组件 ## 1.文件系统 使能文件系统 ![91c952fd55f8c8755d6276dd29a7a9a](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115756.png) ![6a14550b14f8e2797bdc114a0696f20](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115757.png) ![f4149f2ea6f00baeb9ec34661a39372](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115758.png) ![6a4f3bd7638cfff7dc74296a645c613](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115759.png) ![0d7f08f987eabe22576ff41a3f39384](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115760.png) 因为W25q64一次性只能擦除一个扇区也就是4K 重新编译,注意可能第一次使用的时候需要手动挂载 ![49fbf40b5a6df84f60a0f41f478f4e8](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115761.png) ## 2.API MSH 这部分内容官方文档写得很详细了,请参看官网文档(绝对不是因为我懒,不想写) [官方文档](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/fal/fal) ## 3.作业 ![image-20240727011436521](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270115762.png) 不想解释了,上代码 .h ~~~c #ifndef __AHT_PUBLISH_H__ #define __AHT_PUBLISH_H__ #include
typedef struct aht_publish { /* data */ rt_int32_t temp_int; rt_int32_t temp_dec; rt_int32_t humi_int; rt_int32_t humi_dec; }aht_publish; #endif ~~~ .c ~~~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 "dev_sign_api.h" #include "mqtt_api.h" #include "aht_publish.h" #include "cjson.h" #include
#include
#include "aht10.h" #include
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, ...); #define EXAMPLE_TRACE(fmt, ...) \ do \ { \ HAL_Printf("%s|%03d :: ", __func__, __LINE__); \ HAL_Printf(fmt, ##__VA_ARGS__); \ HAL_Printf("%s", "\r\n"); \ } while (0) 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 *root = cJSON_Parse(topic_info->payload); cJSON *params = cJSON_GetObjectItem(root, "params"); cJSON *LED = cJSON_GetObjectItem(params, "LEDSwitch"); if (LED != NULL) { rt_kprintf("LED Switch: %d", LED->valueint); if(LED->valueint == 0) { rt_kprintf("LED ON\n"); rt_pin_write(GET_PIN(F, 12), PIN_HIGH); } else { rt_kprintf("LED OFF\n"); rt_pin_write(GET_PIN(F, 12), PIN_LOW); } } cJSON_Delete(root); break; default: break; } } static int example_subscribe(void *handle) { int res = 0; const char *fmt = "/sys/%s/%s/thing/service/property/set"; 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; } static int example_publish(void *handle,aht_publish * data) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *payload = "{\"params\":{\"CurrentTemperature\":%d.%d,\"CurrentHumidity\":%d.%d}}"; char message[256]; HAL_Snprintf(message,256,payload,data->temp_int,data->temp_dec,data->humi_int,data->humi_dec); 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, message, strlen(message)); if (res < 0) { EXAMPLE_TRACE("publish failed, res = %d", res); HAL_Free(topic); return -1; } HAL_Free(topic); 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); } /*-----------------------------------------------------------------------------------*/ //星火一号AHT10使用的为i2c3 #define AHT10_I2C_BUS "i2c3" //定义线程的信息 #define THREAD_PRIORITY 10 //线程优先级 #define THREAD_STACK_SIZE 2048 //线程栈大小 #define THREAD_TIMSLICE 5 //线程时间片 //发送数据邮箱 rt_mailbox_t aht10_mailbox = RT_NULL; //AHT线程指针 rt_thread_t AHT10 = RT_NULL; void aht10_demo(void * paramenter) { aht10_device_t aht10_dev = RT_NULL; aht10_dev = aht10_init(AHT10_I2C_BUS); if(aht10_dev == RT_NULL) { rt_kprintf("aht10 init fail\n"); return; } int fd = 0; int count = 0; float humi = 0,temp = 0; aht_publish data = {0}; char buf[128]; fd = open("/fal/Data.txt",O_TRUNC|O_RDWR ); if(fd < 0) { rt_kprintf("open file fail\n"); return; } //读取温湿度 while(1) { temp = aht10_read_temperature(aht10_dev); humi = aht10_read_humidity(aht10_dev); data.temp_int = (int)temp; data.temp_dec = (int)(temp*10)%10; data.humi_int = (int)humi; data.humi_dec = (int)(humi*10)%10; rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10); rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10); rt_mb_send(aht10_mailbox,(rt_uint32_t)&data); rt_snprintf(buf,128,"Temp: %d.%d;Humi: %d.%d; Count: %d\n",(int)data.temp_int,(int)data.temp_dec,(int)data.humi_int,(int)data.humi_dec,++count); ssize_t write_len = 0; write_len = write(fd,buf,strlen(buf)); if(write_len <= 0) { rt_kprintf("write file fail\n"); close(fd); return; } fsync(fd); rt_thread_mdelay(5000); } } void aht10_task(void) { AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,9,THREAD_TIMSLICE); if(AHT10 == RT_NULL) { rt_kprintf("aht10 thread create fail\n"); return; } rt_thread_startup(AHT10); return; } void mqtt_example_main(void *parameter) { void *pclient = NULL; int res = 0; int loop_cnt = 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.handle_event.h_fp = example_event_handle; pclient = IOT_MQTT_Construct(&mqtt_params); if (NULL == pclient) { EXAMPLE_TRACE("MQTT construct failed"); return; } res = example_subscribe(pclient); if (res < 0) { IOT_MQTT_Destroy(&pclient); return; } aht_publish * data; while (1) { if(rt_mb_recv(aht10_mailbox,(rt_uint32_t *)&data,RT_WAITING_FOREVER) == RT_EOK) { example_publish(pclient,data); } IOT_MQTT_Yield(pclient, 200); } } rt_thread_t MQTT_Thread; void MQTT_Creat_Thread(void) { // 创建线程 MQTT_Thread = rt_thread_create("MQTT_Thread", mqtt_example_main, RT_NULL, 4096, 10, 5); // 创建成功就启动 if (MQTT_Thread != RT_NULL) { rt_thread_startup(MQTT_Thread); } else { rt_kprintf("MQTT_Thread_Thread Create Fail"); } } void aht10_publish(void) { aht10_mailbox = rt_mb_create("aht10",10,RT_IPC_FLAG_PRIO); if(aht10_mailbox == RT_NULL) { rt_kprintf("create mailbox fail\n"); return; } aht10_task(); MQTT_Creat_Thread(); } MSH_CMD_EXPORT(aht10_publish, aht10 publish init); /* 文件操作系统 */ #define WIFI_CS_PIN GET_PIN(F,10) int WIFI_CS_DWON(void) { rt_pin_mode(WIFI_CS_PIN,PIN_MODE_OUTPUT); rt_pin_write(WIFI_CS_PIN,PIN_LOW); return RT_EOK; } INIT_BOARD_EXPORT(WIFI_CS_DWON); ~~~ 运行结果 ![image-20240727011608757](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270116681.png) ![image-20240727011650009](https://cdn.jsdelivr.net/gh/CYFS3/Typroa/202407270116682.png) [github](https://github.com/CYFS3/rt-thread_demo)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
CYFS
这家伙很懒,什么也没写!
文章
6
回答
1
被采纳
0
关注TA
发私信
相关文章
1
2024RT-Thread操作系统
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
国产MCU移植系列教程汇总,欢迎查看!
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总线
ART-Pi
FinSH
USB
文件系统
DMA
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
ota在线升级
I2C_IIC
WIZnet_W5500
UART
flash
packages_软件包
cubemx
PWM
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
中断
flashDB
socket
Debug
GD32
编译报错
msh
keil_MDK
at_device
MicroPython
rt_mq_消息队列_msg_queue
ulog
C++_cpp
本月问答贡献
出出啊
1513
个答案
341
次被采纳
小小李sunny
1438
个答案
289
次被采纳
张世争
775
个答案
167
次被采纳
crystal266
546
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部