diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/README.md" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..ccfd178d745d9fff28b2fc5aabf6dd63690918cf --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/README.md" @@ -0,0 +1,63 @@ +# 物联网数据采集与控制 + +让开发板测量环境温湿度,通过无线网络(WiFi + MQTT)上传到云端服务器,同时在本地存储一份历史记录(文件系统),并且能够接受云端下发的指令(点亮 LED)。 + +功能模块 +- ① 温湿度采集 每隔2秒从 AHT10 传感器读出一组温度和湿度数据。 +- ② MQTT 上传 通过 WiFi 连接 MQTT 服务器(如 broker.emqx.io),将数据以 JSON 格式发布到主题 rtt-pub2120,在 PC 端的 MQTTX 客户端能实时接收到。 +- ③ 文件系统存储 每次采集的数据同时以追加方式写入开发板 Flash(如 W25Q64)上的 /fal/Data.txt 文件,格式为: +Temp:26.5 ; Humi:45.2 ; Count: 1(Count 为启动后的采集序号)。 +- ④ 云端点灯 通过 MQTTX 向主题 rtt-sub2120 发送指令 led,开发板上的红色 LED 会翻转(亮/灭)。 + + +# 实现逻辑 +```plain +┌─────────────┐ +│ main() │ +└──────┬──────┘ + ▼ +┌─────────────────────────┐ +│ sensor_thread_entry │ +│ (每 2s 循环) │ +└─────────────────────────┘ + │ + ├──→ led_init() ──→ PF12 初始化 + │ + ├──→ 等待 rt_wlan_is_ready() ──→ WiFi 自动连接 + │ + ├──→ sensor_init() ──→ aht10_init("i2c3") + │ + ├──→ fs_module_init() ──→ 检查/创建 /fal/Data.txt + │ + ├──→ mqtt_module_init() ──→ mqtt_lease() → connect → subscribe + │ (使用 KAWAII_MQTT_* 宏) + │ + └──→ 循环 ──→ sensor_read() ──┬──→ fs_append_record() + └──→ mqtt_publish_data() + │ + ▼ + {"temperature":26.5,"humidity":45.2,"count":1} + +云端下发 "led" ──→ mqtt_sub_callback() ──→ led_cmd_handler() ──→ led_toggle() +``` + +# 实现 + +## 启用软件包 +启用sensor、文件系统和mqtt相关软件包 + +编译测试环境是否正确,文件系统初始化成功,wifi连接,成功与mqtt服务器通信 +![alt text](image.png) +![alt text](image-1.png) + +## 运行效果 +系统初始化 +![alt text](image-2.png) +连接wifi后开始发送消息 +![alt text](image-5.png) +通过mqtt服务器向pc端的mqtt客户机发送消息 +![alt text](image-3.png) +输入led让LED翻转 +![alt text](image-4.png) +解决了乱码问题,因为在fs_module.c中写了中文冒号,通过cat /fal/Data.txt查看记录的文件内容 +![alt text](image-6.png) \ No newline at end of file diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/SConscript" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/SConscript" new file mode 100644 index 0000000000000000000000000000000000000000..1eaad937dd676fbeb19b534e72b2b7b258f06662 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/SConscript" @@ -0,0 +1,15 @@ +from building import * +import os + +cwd = GetCurrentDir() +CPPPATH = [cwd] +src = Glob('*.c') + +group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) + +list = os.listdir(cwd) +for item in list: + if os.path.isfile(os.path.join(cwd, item, 'SConscript')): + group = group + SConscript(os.path.join(item, 'SConscript')) + +Return('group') diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_config.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_config.h" new file mode 100644 index 0000000000000000000000000000000000000000..1ab334e77ce701492d6b83798c255f9c3f788704 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_config.h" @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +/* + * 全局宏、引脚、主题名配置 + */ + + +#ifndef __APP_CONFIG_H__ +#define __APP_CONFIG_H__ + +#include /* 复用所有软件包生成的宏 */ +#include +#include +#include +#include +#include +/* ========== 硬件引脚(rtconfig.h 中无此信息) ========== */ +#define LED_R_PIN GET_PIN(F, 12) /* 红色 LED,低电平点亮 */ + +/* ========== 业务参数(rtconfig.h 中无此信息) ========== */ +#define DATA_FILE_PATH "/fal/Data.txt" /* Flash 文件系统存储路径 */ +#define SAMPLE_INTERVAL_MS 2000 /* 采集周期 2s */ +#define AHT10_I2C_BUS "i2c3" /* 板载 AHT10 挂在 I2C3 */ + +#endif diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_init.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_init.h" new file mode 100644 index 0000000000000000000000000000000000000000..2c3c6a8648c915d8821bc8170f931562913d3c60 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/app_init.h" @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#ifndef APPLICATIONS_APP_INIT_H_ +#define APPLICATIONS_APP_INIT_H_ + + + +#endif /* APPLICATIONS_APP_INIT_H_ */ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.c" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.c" new file mode 100644 index 0000000000000000000000000000000000000000..5bf07d6502a3eb67999a3604b580b7edd55de189 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.c" @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#include "fs_module.h" +#include "app_config.h" +#include +#include + +#define DBG_TAG "fs" +#define DBG_LVL DBG_LOG +#include + +int fs_module_init(void) +{ + /* BSP_USING_FLASH_FS_AUTO_MOUNT 已开启,文件系统自动挂载,无需手动 mount */ + int fd = open(DATA_FILE_PATH, O_RDONLY); + if (fd < 0) + { + fd = open(DATA_FILE_PATH, O_WRONLY | O_CREAT, 0); + if (fd >= 0) + { + close(fd); + LOG_I("Data file created: %s", DATA_FILE_PATH); + } + else + { + LOG_E("Failed to create data file"); + return -1; + } + } + else + { + close(fd); + LOG_I("Data file ready: %s", DATA_FILE_PATH); + } + return 0; +} + +int fs_append_record(float temp, float humi, unsigned int count) +{ + int fd = open(DATA_FILE_PATH, O_WRONLY | O_APPEND, 0); + if (fd < 0) + { + LOG_E("Failed to open %s", DATA_FILE_PATH); + return -1; + } + + char buf[64]; + rt_snprintf(buf, sizeof(buf), + "Temp: %.1f ; Humi: %.1f ; Count: %u\r\n", + temp, humi, count); + + write(fd, buf, rt_strlen(buf)); + close(fd); + + LOG_I("FS append: %s", buf); + return 0; +} diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.h" new file mode 100644 index 0000000000000000000000000000000000000000..db2aea3ee050118b1a67a6ff588b1b9e606595be --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/fs_module.h" @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#ifndef __FS_MODULE_H__ +#define __FS_MODULE_H__ + +int fs_module_init(void); +int fs_append_record(float temp, float humi, unsigned int count); + +#endif diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-1.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-1.png" new file mode 100644 index 0000000000000000000000000000000000000000..b6d8f429a67ab6c8422f006b679114bf0f298ef9 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-1.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-2.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-2.png" new file mode 100644 index 0000000000000000000000000000000000000000..a4c505101bb360ac8b42e2f8393281a292cc6774 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-2.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-3.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-3.png" new file mode 100644 index 0000000000000000000000000000000000000000..e619680bc3822a4b62f93d6877451ca3fa73762c Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-3.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-4.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-4.png" new file mode 100644 index 0000000000000000000000000000000000000000..8abcddab496dad9ca9d6ce8caeaac4c6b5edff35 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-4.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-5.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-5.png" new file mode 100644 index 0000000000000000000000000000000000000000..6bba791d98a1b3adb5f23a77692bdaebd2491a2d Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-5.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-6.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-6.png" new file mode 100644 index 0000000000000000000000000000000000000000..70c035480b7b214f0a7ba241566f1aa67338fa4d Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image-6.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image.png" new file mode 100644 index 0000000000000000000000000000000000000000..cc4e7dd723fc04ccf2edbfd10c27be59a82a85b3 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/image.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.c" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.c" new file mode 100644 index 0000000000000000000000000000000000000000..bc584a0b2c38e3372ee9ef1cdddcc008abe14fb7 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.c" @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#include "led_module.h" +#include "app_config.h" +#include + +void led_init(void) +{ + rt_pin_mode(LED_R_PIN, PIN_MODE_OUTPUT); + rt_pin_write(LED_R_PIN, PIN_LOW); /* 默认点亮(低电平点亮) */ +} + +void led_toggle(void) +{ + rt_pin_write(LED_R_PIN, !rt_pin_read(LED_R_PIN)); +} diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.h" new file mode 100644 index 0000000000000000000000000000000000000000..b50ffb2cb4b1e5cb9982404bb5a5f3ea2ccf1d07 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/led_module.h" @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#ifndef __LED_MODULE_H__ +#define __LED_MODULE_H__ + +void led_init(void); +void led_toggle(void); + +#endif diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/main.c" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/main.c" new file mode 100644 index 0000000000000000000000000000000000000000..fc8b98de48a068f60666e7a473fd555ca122343a --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/main.c" @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-5-10 ShiHao first version + */ + +#include +#include +#include "app_config.h" +#include "led_module.h" +#include "sensor_module.h" +#include "fs_module.h" +#include "mqtt_module.h" + +#define DBG_TAG "main" +#define DBG_LVL DBG_LOG +#include + +static unsigned int sample_count = 0; + +/* ---------- LED 指令处理 ---------- */ +static void led_cmd_handler(void) +{ + led_toggle(); + LOG_I("LED toggled by cloud command"); +} + +/* ---------- 采集线程 ---------- */ +static void sensor_thread_entry(void *parameter) +{ + (void)parameter; + sensor_data_t data; + + /* ① 初始化 LED */ + led_init(); + + /* 等待 WiFi 自动连接就绪(RT_WLAN_AUTO_CONNECT_ENABLE 已开启) */ + LOG_I("Waiting for WiFi connection..."); + while (rt_wlan_is_ready() == 0) + { + rt_thread_mdelay(1000); + } + LOG_I("WiFi connected, IP ready"); + + /* ② 初始化传感器 */ + if (sensor_init() != 0) + { + LOG_E("Sensor init failed"); + return; + } + + /* ③ 初始化文件系统 */ + if (fs_module_init() != 0) + { + LOG_E("FS init failed"); + return; + } + + /* ④ 初始化 MQTT 并注册 LED 控制回调 */ + mqtt_set_led_callback(led_cmd_handler); + if (mqtt_module_init() != 0) + { + LOG_E("MQTT init failed"); + return; + } + + /* ⑤ 主循环:每 2 秒采集一次 */ + while (1) + { + if (sensor_read(&data) == 0) + { + sample_count++; + + /* 文件系统存储 */ + fs_append_record(data.temperature, data.humidity, sample_count); + + /* MQTT 上传 */ + mqtt_publish_data(data.temperature, data.humidity, sample_count); + } + + rt_thread_mdelay(SAMPLE_INTERVAL_MS); + } +} + +int main(void) +{ + rt_thread_t tid = rt_thread_create("sensor", + sensor_thread_entry, + RT_NULL, + 4096, + 10, + 10); + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + + return RT_EOK; +} diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.c" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.c" new file mode 100644 index 0000000000000000000000000000000000000000..a18e64ac666c07e615f4eb627e5f77eecd02fb53 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.c" @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#include "mqtt_module.h" +#include "app_config.h" +#include +#include +#include + +#define DBG_TAG "mqtt" +#define DBG_LVL DBG_LOG +#include + +static mqtt_client_t *mqtt_client = RT_NULL; +static void (*led_cmd_callback)(void) = RT_NULL; + +/* ---------- 订阅回调:云端下发指令 ---------- */ +static void mqtt_sub_callback(void *client, message_data_t *msg) +{ + (void)client; + + LOG_I("MQTT recv: topic=%s, payload=%.*s", + msg->topic_name, + msg->message->payloadlen, + (char *)msg->message->payload); + + /* 判断 payload 是否为 "led"(严格匹配长度,防止越界) */ + if (msg->message->payloadlen == 3 && + rt_strncmp((char *)msg->message->payload, "led", 3) == 0) + { + if (led_cmd_callback != RT_NULL) + { + led_cmd_callback(); + } + } +} + +/* ---------- MQTT 初始化与连接 ---------- */ +int mqtt_module_init(void) +{ + mqtt_client = mqtt_lease(); + if (mqtt_client == RT_NULL) + { + LOG_E("MQTT client lease failed"); + return -1; + } + + /* 以下宏全部来自 rtconfig.h(Kawaii MQTT 软件包配置) */ + mqtt_set_host(mqtt_client, KAWAII_MQTT_HOST); + mqtt_set_port(mqtt_client, KAWAII_MQTT_PORT); + mqtt_set_client_id(mqtt_client, KAWAII_MQTT_CLIENTID); + mqtt_set_user_name(mqtt_client, KAWAII_MQTT_USERNAME); + mqtt_set_password(mqtt_client, KAWAII_MQTT_PASSWORD); + mqtt_set_clean_session(mqtt_client, 1); + + if (mqtt_connect(mqtt_client) != 0) + { + LOG_E("MQTT connect failed"); + return -1; + } + + if (mqtt_subscribe(mqtt_client, KAWAII_MQTT_SUBTOPIC, QOS1, mqtt_sub_callback) != 0) + { + LOG_E("MQTT subscribe failed"); + return -1; + } + + LOG_I("MQTT connected"); + LOG_I(" pub topic: %s", KAWAII_MQTT_PUBTOPIC); + LOG_I(" sub topic: %s", KAWAII_MQTT_SUBTOPIC); + return 0; +} + +/* ---------- MQTT 发布 JSON 数据 ---------- */ +int mqtt_publish_data(float temp, float humi, unsigned int count) +{ + if (mqtt_client == RT_NULL) + { + LOG_W("MQTT client not ready"); + return -1; + } + + char json_buf[128]; + rt_snprintf(json_buf, sizeof(json_buf), + "{\"temperature\":%.1f,\"humidity\":%.1f,\"count\":%u}", + temp, humi, count); + + mqtt_message_t msg; + memset(&msg, 0, sizeof(msg)); + msg.qos = QOS1; + msg.payload = json_buf; + msg.payloadlen = rt_strlen(json_buf); + + if (mqtt_publish(mqtt_client, KAWAII_MQTT_PUBTOPIC, &msg) != 0) + { + LOG_W("MQTT publish failed"); + return -1; + } + + LOG_I("MQTT pub: %s", json_buf); + return 0; +} + +/* ---------- 注册 LED 控制回调 ---------- */ +void mqtt_set_led_callback(void (*cb)(void)) +{ + led_cmd_callback = cb; +} diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.h" new file mode 100644 index 0000000000000000000000000000000000000000..28e9e8693078177ce19f54f290e87a5d1fb599f5 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/mqtt_module.h" @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#ifndef __MQTT_MODULE_H__ +#define __MQTT_MODULE_H__ + +int mqtt_module_init(void); +int mqtt_publish_data(float temp, float humi, unsigned int count); +void mqtt_set_led_callback(void (*cb)(void)); + +#endif diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.c" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.c" new file mode 100644 index 0000000000000000000000000000000000000000..b49b0478b106f57dfd2a9ef13c7db26233521937 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.c" @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#include "sensor_module.h" +#include "app_config.h" +#include +#include + +#define DBG_TAG "sensor" +#define DBG_LVL DBG_LOG +#include + +static aht10_device_t aht10_dev = RT_NULL; + +int sensor_init(void) +{ + aht10_dev = aht10_init(AHT10_I2C_BUS); + if (aht10_dev == RT_NULL) + { + LOG_E("AHT10 init failed on %s", AHT10_I2C_BUS); + return -1; + } + LOG_I("AHT10 sensor initialized"); + return 0; +} + +int sensor_read(sensor_data_t *data) +{ + if (aht10_dev == RT_NULL || data == RT_NULL) + { + return -1; + } + + data->temperature = aht10_read_temperature(aht10_dev); + data->humidity = aht10_read_humidity(aht10_dev); + + /* AHT10 API 失败时返回 -50.0(温度)或 0.0(湿度) */ + if (data->temperature <= -49.0f || data->humidity <= 0.1f) + { + LOG_W("AHT10 read failed"); + return -1; + } + + return 0; +} diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.h" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.h" new file mode 100644 index 0000000000000000000000000000000000000000..38d5c2b258031917f4e07b9164469e12d2a17435 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\344\275\234\344\270\232/sensor_module.h" @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-28 d0ubleU0x00 the first version + */ +#ifndef __SENSOR_MODULE_H__ +#define __SENSOR_MODULE_H__ + +typedef struct { + float temperature; /* 单位:℃ */ + float humidity; /* 单位:%RH */ +} sensor_data_t; + +int sensor_init(void); +int sensor_read(sensor_data_t *data); + +#endif diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-1.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-1.png" new file mode 100644 index 0000000000000000000000000000000000000000..9463c73d7a965e070a5c705a5087a99d3b3c0c3d Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-1.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-10.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-10.png" new file mode 100644 index 0000000000000000000000000000000000000000..0937deb91e572faffa61c7582094c51a4db0da5f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-10.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-11.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-11.png" new file mode 100644 index 0000000000000000000000000000000000000000..55f9f13da34c7ca837d3e6a241e6f3c5310ce906 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-11.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-12.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-12.png" new file mode 100644 index 0000000000000000000000000000000000000000..be3c53351bf1186f1ff916f4431aecfe4428f1bd Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-12.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-13.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-13.png" new file mode 100644 index 0000000000000000000000000000000000000000..0d8815ba4c8c20e7e65c9cc530867941c787264b Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-13.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-14.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-14.png" new file mode 100644 index 0000000000000000000000000000000000000000..eaa42e233cd6002c25df06d7b06842385a2c4dc8 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-14.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-15.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-15.png" new file mode 100644 index 0000000000000000000000000000000000000000..21e74833a3a899b0b5c34cde31d616117a94a89c Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-15.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-16.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-16.png" new file mode 100644 index 0000000000000000000000000000000000000000..21e74833a3a899b0b5c34cde31d616117a94a89c Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-16.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-17.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-17.png" new file mode 100644 index 0000000000000000000000000000000000000000..9469d9d60e738a3aa6597bae74f946ef0b21ca84 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-17.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-18.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-18.png" new file mode 100644 index 0000000000000000000000000000000000000000..de97decbf2d529abc1d9e3cb4c1789f4ec5a432f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-18.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-19.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-19.png" new file mode 100644 index 0000000000000000000000000000000000000000..98f8e2da05b1357149a0daa2772fd710943fff92 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-19.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-2.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-2.png" new file mode 100644 index 0000000000000000000000000000000000000000..f4f1f9fb9f70639562a31236b72f652eba74d531 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-2.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-20.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-20.png" new file mode 100644 index 0000000000000000000000000000000000000000..5db7a8ce5be1fc1a97bdb5d1d4201daf11909531 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-20.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-21.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-21.png" new file mode 100644 index 0000000000000000000000000000000000000000..d7d60296504d64bfe431850db2e946cf6a0d5bd7 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-21.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-22.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-22.png" new file mode 100644 index 0000000000000000000000000000000000000000..ceb21de94467275e5d31a87a82feab12ea9feaf6 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-22.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-23.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-23.png" new file mode 100644 index 0000000000000000000000000000000000000000..c32adab07ce7261c6de8ab8388b661029655a807 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-23.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-24.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-24.png" new file mode 100644 index 0000000000000000000000000000000000000000..839b2262761a987cf7f66a7ea3fdc199aa0dd718 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-24.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-25.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-25.png" new file mode 100644 index 0000000000000000000000000000000000000000..4d4af8b75f59085f06376d3d33b78733578a0dfd Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-25.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-26.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-26.png" new file mode 100644 index 0000000000000000000000000000000000000000..d00c4f6489f6a9b36a02901e1b2dbd480dac680f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-26.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-27.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-27.png" new file mode 100644 index 0000000000000000000000000000000000000000..e20674eb036e18b51489977b22d464da3c8a1917 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-27.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-28.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-28.png" new file mode 100644 index 0000000000000000000000000000000000000000..99e098b649aec824bf42bca4b092d64d149c3805 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-28.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-29.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-29.png" new file mode 100644 index 0000000000000000000000000000000000000000..a460cab4ea1ad724138a402581802f6bc86fba5f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-29.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-3.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-3.png" new file mode 100644 index 0000000000000000000000000000000000000000..7a4e993a28ee3d922efa5751e68cb9353ca76b6f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-3.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-30.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-30.png" new file mode 100644 index 0000000000000000000000000000000000000000..25925d230d9f5d51ae345cd7312898bbe0444586 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-30.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-31.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-31.png" new file mode 100644 index 0000000000000000000000000000000000000000..43754fecc978310e9383e4c245adcb08b97746c1 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-31.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-32.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-32.png" new file mode 100644 index 0000000000000000000000000000000000000000..a06ce19b3238194148dee8c13dbc09c39c7c2018 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-32.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-33.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-33.png" new file mode 100644 index 0000000000000000000000000000000000000000..d2fd7f8cee58b33430875e7feb0da8112933b2cf Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-33.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-34.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-34.png" new file mode 100644 index 0000000000000000000000000000000000000000..527bb779301194451f13070197a9906eb5c728f2 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-34.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-35.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-35.png" new file mode 100644 index 0000000000000000000000000000000000000000..eb01999ef6f47b392656a24d7669326db6ec1358 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-35.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-36.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-36.png" new file mode 100644 index 0000000000000000000000000000000000000000..0200148bcc8cdf542a2af7461d50b7d98d8bb4e1 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-36.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-37.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-37.png" new file mode 100644 index 0000000000000000000000000000000000000000..e1d5f1082a49aa407ac69ae878230e4c9bfd9dc7 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-37.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-38.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-38.png" new file mode 100644 index 0000000000000000000000000000000000000000..78f09193b22c828b0d1fa5fefa29db9942e312f9 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-38.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-4.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-4.png" new file mode 100644 index 0000000000000000000000000000000000000000..44047e2f2370c781f40a7d72e017ad5300ba881d Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-4.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-5.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-5.png" new file mode 100644 index 0000000000000000000000000000000000000000..de817a4807dde8327fe03886a2dcffb327da2499 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-5.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-6.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-6.png" new file mode 100644 index 0000000000000000000000000000000000000000..6ff5094a1bf3c0cb837ce6764fa17d89558e15aa Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-6.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-7.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-7.png" new file mode 100644 index 0000000000000000000000000000000000000000..f223a569c6e9e1ade5952109515774c7f609606f Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-7.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-8.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-8.png" new file mode 100644 index 0000000000000000000000000000000000000000..6973b57893157a832573ead9b7764ffd42eda16a Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-8.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-9.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-9.png" new file mode 100644 index 0000000000000000000000000000000000000000..5e27c98cf1c1898191ba0fe4a2ec48cb0e6ae16d Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image-9.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image.png" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image.png" new file mode 100644 index 0000000000000000000000000000000000000000..3532194f1b2ef4945adbe95e70520bf11fef2e52 Binary files /dev/null and "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/image.png" differ diff --git "a/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/\350\275\257\344\273\266\345\214\205\344\270\216\346\226\207\344\273\266\347\263\273\347\273\237\347\254\224\350\256\260.md" "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/\350\275\257\344\273\266\345\214\205\344\270\216\346\226\207\344\273\266\347\263\273\347\273\237\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..d822e36d8f7ce62ea0848b53878712354ce42496 --- /dev/null +++ "b/2026/\347\254\2544\347\273\204/\347\216\213\347\220\263/\347\254\224\350\256\260/\344\274\240\346\204\237\345\231\250\346\226\207\344\273\266\347\263\273\347\273\237mqtt\351\200\232\344\277\241/\350\275\257\344\273\266\345\214\205\344\270\216\346\226\207\344\273\266\347\263\273\347\273\237\347\254\224\350\256\260.md" @@ -0,0 +1,446 @@ +# RT-Thread软件包与文件系统 + +- RTT的软件包索引机制 +- AHT10传感器与RW007 WiFi模块的软件包驱动配置 +- MQTT协议通信 +- DFS文件系统在外部Flash上的挂载与应用 +--- +### 一、软件包生态与索引机制 +针对RT-Thread的软件包生态系统,会议详细阐述了其索引原理与配置流程: +#### 1. 软件包索引与配置路径 +- **索引来源**:软件包的Kconfig配置路径位于env工具目录下,通过`packages`目录引入,具体路径为`studio安装目录/env/packages`。 +- >硬件的Kconfig在board/libraries/Kconfig +- **版本兼容性**:RT-Thread Studio针对不同内核版本(5.1.0以上/以下)维护了两份env配置,以确保软件包与内核版本的兼容匹配。 + +#### 2. 软件包集成流程 +- **配置使能**:在menuconfig中使能目标软件包(如AHT10、RW007)后,系统会自动拉取对应的软件包源码至工程目录。 +- **依赖管理**:部分软件包(如AHT10)依赖于底层框架(如Sensor框架),需在配置中手动开启相关依赖项,否则会导致编译错误。 +### 二、传感器驱动与WiFi网络配置 +通过AHT10温湿度传感器和RW007 WiFi模块,展示软件包驱动的具体应用: +#### 1. AHT10传感器驱动实现 +- **API调用流程**:通过调用`aht10_init`初始化设备,传入I2C总线名称获取设备句柄,随后调用`aht10_read_temperature/humidity`读取数据。 +- **浮点数打印支持**:由于默认libc库不支持浮点数打印,需额外使能`rt_vsnprintf_full`软件包,以解决温湿度数据无法正常显示的问题。 +- **线程封装**:建议将传感器读取逻辑封装在独立线程中,通过`msh_export`导出命令,便于在终端调试。 +#### 2. RW007 WiFi模块配置 +- **引脚配置修正**:RW007软件包默认引脚配置可能与开发板原理图不符,需根据原理图手动修正片选(CS)、Boot0/1及中断引脚的定义。 +- **网络功能验证**:配置完成后,通过`wifi scan`扫描热点,使用`wifi connect`连接网络,并通过`ping`命令验证网络连通性。 +### 三、MQTT协议通信实现 +利用cJSON与MQTT软件包,会议演示了设备与云端的轻量级通信方案: +#### 1. MQTT客户端配置 +- **服务器连接**:使用开源的EMQX服务器(端口1883),配置客户端ID、用户名密码(免费服务器无需校验)及订阅/发布主题。 +- **示例代码集成**:使能`cJSON`和`cai_mqtt`软件包,并开启示例代码,系统会自动生成MQTT客户端线程,实现连接与收发功能。 +- **数据收发验证**:通过MQTT.fx客户端订阅设备发布主题,验证设备端发送的数据能否被云端正确接收。 +#### 2. MSH命令封装 +- **自定义命令**:演示了如何封装MSH命令,通过解析`argc`和`argv`参数,实现通过命令行控制MQTT消息的发送。 +### 四、DFS文件系统与Flash管理 +利用DFS(Device File System)管理外部Flash存储空间 +#### 1. 文件系统挂载配置 +- **组件使能**:在menuconfig中依次使能DFS、elm文件系统(FATFS兼容层)及Flash自动挂载功能。 +- **分区表映射**:通过分区表将外部Flash(W25Q64)划分为固件区、下载区及文件系统区,并将文件系统挂载到根目录下的`/flash`路径。 +#### 2. Flash驱动与操作 +- **SFUD通用驱动**:利用SFUD(Serial Flash Universal Driver)组件屏蔽不同Flash芯片的底层差异,提供统一的读写接口。 +- **SPI总线冲突解决**:由于WiFi模块与Flash共用SPI总线,需在初始化时对WiFi片选引脚进行上拉处理,避免总线冲突导致Flash读写失败。 +- **POSIX接口应用**:演示了使用`open`、`write`、`close`等标准POSIX接口对Flash文件进行读写操作。 +### 五、疑难解答与版本规划 +#### 1. 常见问题排查 +- **按键中断无响应**:按键中断无响应问题,指出代码中延时过长可能导致响应迟钝,建议参考按键软件包实现消抖逻辑。 +- **环境版本冲突**:针对env版本报错问题,明确内核版本4.1.1只能使用env 1.5.x版本,新版本env不兼容旧内核。 +#### 2. 版本发布预告 +- **新版本发布**:预告即将发布RT-Thread 5.3.0版本,距离上次5.2.2版本发布已过去10个月,鼓励学员关注Issue并参与贡献。 +--- +## 开发环境配置 +开发板的包,基于开发板创建工程 +![alt text](image.png) +创建好后编译一下 + +使能板载硬件和软件包,保存后RT-Studio会自动拉取相应软件包 +![alt text](image-1.png) +![alt text](image-2.png) + +### aht10软件包 +本节课用到主要是温湿度传感器 +![alt text](image-3.png) +配置相应使能和参数 +![alt text](image-4.png) +下载成功,packages目录下出现相应软件包 +然后在package目录下就可以看到aht10这个软件包,里面有一个readme文件,告诉软件包是干什么的(温湿度传感器等)和初始化方法 +![alt text](image-9.png) +创建一个源文件实现通过aht10软件包读取温湿度 +```c +//aht10.c +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-08-24 d0ubleU0x00 the first version + */ +#include +#include + +#define DBG_TAG "aht10" +#define DBG_LVL BUG_LOG +#include + +int aht10_read_entry(void *parameter){ + aht10_device_t aht10= aht10_init("i2c3"); + if(aht10==RT_NULL){ + LOG_E("aht10 init falll"); + return -RT_ERROR; + } + while(1){ + float temp=aht10_read_temperature(aht10); + float humi=aht10_read_humidity(aht10); + + rt_kprintf("temp %.2f , humi %.2f \n",temp, humi); + rt_thread_mdelay(500); + } +} + +int aht10_thread_init(void){ + rt_thread_t AHT10_thread=rt_thread_create("aht10",aht10_read_entry, RT_NULL,1024,10,10); + if(AHT10_thread==RT_NULL){ + LOG_E("aht10 thread create fail"); + return -RT_ERROR; + } + rt_thread_startup(AHT10_thread); +} + +MSH_CMD_EXPORT(aht10_thread_init,aht10_thread_init); + +``` +未定义软件包依赖sensor框架,应该把sensor框架打开 + +要使能sensor的框架 +![alt text](image-5.png) +![alt text](image-10.png) +没有打开ulog,再打开 +![alt text](image-11.png) +支持浮点 +![alt text](image-6.png) +编译烧录,打印出当前温湿度 +![alt text](image-12.png) + +### 利用mqtt发送和接收订阅消息 + +然后利用mqtt发送订阅信息,通过mqtt把板端的信息发给设备端 +这里用mqttx做客户端,emqx做服务器 +添加mqtt软件包 +![alt text](image-13.png) + +利用板卡上的rw007 wifi模块进行网络的收发 +先使能RW007 + +![alt text](image-14.png) + +执行wifi scan命令接收现有空间中的wifi信号(配置好后在main函数编译加载就绪) + +![alt text](image-16.png) +适用wifi join命令连接热点 +![alt text](image-18.png) + +mqtt的发送和连接 + +配置软件包 +![alt text](image-19.png) +重新编译软件包的test.c文件,可以在mqttx客户端看到相应接入消息 +![alt text](image-20.png) +注意两个clint的id不能相同,否则其中一个会被挤掉,因为连的是同一个mqtt服务器,通过订阅进行通信。这里换了一下主题 +![alt text](image-21.png) +![alt text](image-22.png) + +### 文件系统 +使能文件系统 +![alt text](image-23.png) +配置组件FAL,将内部flash和板载W25Q64通过一个抽象层管理 +![alt text](image-24.png) +文件系统成功挂载 +![alt text](image-25.png) +片内的falsh为app和param分区表 +将W25Q64分为了多个分区,将最后一个分区filesystem作为文件系统挂载在下面 +分区表如下: +![alt text](image-26.png) + +![alt text](image-27.png) +通过SFUD软件包实现 +![alt text](image-28.png) + +过了一晚上出现了报错,因为未挂载任何文件系统 +![alt text](image-29.png) +重新编译以下main函数再试试 +![alt text](image-30.png) +还是初始化失败 +用list device查看设备 +![alt text](image-31.png) + +但是reboot一下就挂载成功了,让我非常疑惑 +![alt text](image-32.png) +这里有一个报错(在课程视频2:06:25解释),因为没有对pin做上拉(?)避免二者互相干扰 + +因为w25q64和wifi用的是同一总线,一开始没对wifi的片选信号做处理,会干扰到spi的flash读写操作,所以一开始报了w25q64没有找到的错误 + +assert failed问题仍未解决,终端所有输出如下: +```plaintext +msh />reboot +[0] I/I2C: I2C bus [i2c3] registered + + \ | / +- RT - Thread Operating System + / | \ 4.1.1 build Aug 26 2026 22:33:37 + 2006 - 2022 Copyright by RT-Thread team +lwIP-2.0.3 initialized! +[4] I/sal.skt: Socket Abstraction Layer initialize success. +[11] I/SFUD: Warning: Read SFDP parameter header information failed. The W25Q64 is not support JEDEC SFDP. +[23] I/SFUD: Warning: This flash device is not found or not support. +[30] I/SFUD: Error: W25Q64 flash device is initialize fail. +[37] E/SFUD: ERROR: SPI flash probe failed by SPI device spi20. +msh />[1010] E/[RW007]: The wifi Stage 1 status 0 0 0 1 +[1018] I/WLAN.dev: wlan init success +[1024] I/WLAN.lwip: eth device init ok name:w0 +[1030] I/WLAN.dev: wlan init success +[1037] I/WLAN.lwip: eth device init ok name:w1 + +rw007 sn: [rw007a8a355defc584afa9eeb] +rw007 ver: [RW007_2.1.0-c7747420-52] + +[D/FAL] (fal_flash_init:47) Flash device | onchip_flash_128k | addr: 0x08000000 | len: 0x00100000 | blk_size: 0x00020000 | +initialized finish. +[1565] E/SFUD: ERROR: Flash device W25Q64 not found! +[D/FAL] (fal_flash_init:47) Flash device | W25Q64 | addr: 0x00000000 | len: 0x00800000 | blk_size: 0x000010 +00 |initialized finish. +[I/FAL] ==================== FAL partition table ==================== +[I/FAL] | name | flash_dev | offset | length | +[I/FAL] ------------------------------------------------------------- +[I/FAL] | bootloader | onchip_flash_128k | 0x00000000 | 0x00020000 | +[I/FAL] | app | onchip_flash_128k | 0x00020000 | 0x00060000 | +[I/FAL] | easyflash | W25Q64 | 0x00000000 | 0x00080000 | +[I/FAL] | download | W25Q64 | 0x00080000 | 0x00100000 | +[I/FAL] | wifi_image | W25Q64 | 0x00180000 | 0x00080000 | +[I/FAL] | font | W25Q64 | 0x00200000 | 0x00300000 | +[I/FAL] | filesystem | W25Q64 | 0x00500000 | 0x00300000 | +[I/FAL] ============================================================= +[I/FAL] RT-Thread Flash Abstraction Layer initialize success. +[I/FAL] The FAL block device (filesystem) created successfully +[1687] A/SFUD: (sfud_dev) has assert failed at read:51. + +``` +然后在配置中看到了一个相似的选项,选中试试 +![alt text](image-33.png) +然后编译都过不了了 +然后我又发现RT-Studio中有个依赖图,在文件系统的地方打开看看 +然后什么也没有发现 +#### 重新尝试 +于是我重新创建了一个工程,用于做文件系统的练习,这次只有文件系统没挂载上的问题 +![alt text](image-34.png) +先把main函数中烦人的输出注释掉,然后重新编译烧录,再用list_device查看DFS和FatFs是否已编译进内核 +![alt text](image-35.png) +从list_device输出可以看到,filesystem和W25Q64两个Block Device都已成功注册,底层驱动链(SPI → SFUD → FAL → Block Device)完全正常。问题在DFS文件系统层。 +然后输入help命令,得到如下输出 +```plaintext +msh />help +RT-Thread shell commands: +clear - clear the terminal screen +version - show RT-Thread version information +list_thread - list thread +list_sem - list semaphore in system +list_event - list event in system +list_mutex - list mutex in system +list_mailbox - list mail box in system +list_msgqueue - list message queue in system +list_mempool - list memory pool in system +list_timer - list timer in system +list_device - list device in system +list - list objects +help - RT-Thread shell help. +ps - List threads in the system. +free - Show the memory usage in the system. +ls - List information about the FILEs. +cp - Copy SOURCE to DEST. +mv - Rename SOURCE to DEST. +cat - Concatenate FILE(s) +rm - Remove(unlink) the FILE(s). +cd - Change the shell working directory. +pwd - Print the name of the current working directory. +mkdir - Create the DIRECTORY. +mkfs - format disk with file system +mount - mount +umount - Unmount device from file system +df - disk free +echo - echo string to file +tail - print the last N - lines data of the given file +fal - FAL (Flash Abstraction Layer) operate. +sf - SPI Flash operate. +pin - pin [option] +list_fd - list file descriptor +reboot - Reboot System + +msh /> +``` + +从 help 输出可以看到 mkfs、mount、ls、mkdir、df 等命令都有,说明 DFS 和 FatFs 已经正确编译进内核了。问题不在配置缺失,而是运行时层面的问题。 + +根据日志 [E/app.filesystem] Failed to initialize filesystem!,说明你的应用层代码里调用了 dfs_mount 但失败了。 + +然后添加了格式化的代码,将main.c的内容改为 + +```c +#include +#include +#include +#include // 用于 mkdir +#include + +static int filesystem_init(void) +{ + /* 1. 创建 FAL block device */ + fal_blk_device_create("filesystem"); + + /* 2. 创建挂载点目录 */ + mkdir("/mnt", 0); + + /* 3. 先尝试挂载 */ + if (dfs_mount("filesystem", "/mnt", "elm", 0, 0) == 0) + { + rt_kprintf("Filesystem mounted successfully!\n"); + return 0; + } + + /* 4. 挂载失败,格式化 */ + rt_kprintf("No filesystem found, formatting...\n"); + + if (dfs_mkfs("elm", "filesystem") != 0) + { + rt_kprintf("Format failed!\n"); + return -1; + } + + rt_kprintf("Format success, mounting...\n"); + + /* 5. 再次挂载 */ + if (dfs_mount("filesystem", "/mnt", "elm", 0, 0) != 0) + { + rt_kprintf("Mount after format failed!\n"); + return -1; + } + + rt_kprintf("Filesystem mounted successfully after format!\n"); + return 0; +} +INIT_APP_EXPORT(filesystem_init); + +/* ========== 必须保留 main 函数 ========== */ +int main(void) +{ + return 0; +} +``` +但是出现了新的问题 +![alt text](image-36.png) + +改成打印错误码的代码 +```c +#include +#include +#include +#include +#include + +static int filesystem_init(void) +{ + int ret; + struct rt_device *dev; + + /* 检查 block device 是否已存在 */ + dev = rt_device_find("filesystem"); + if (dev == RT_NULL) + { + fal_blk_device_create("filesystem"); + rt_kprintf("Created block device\n"); + } + else + { + rt_kprintf("Block device already exists\n"); + } + + /* 创建挂载点 */ + mkdir("/mnt", 0); + + /* 先尝试挂载 */ + ret = dfs_mount("filesystem", "/mnt", "elm", 0, 0); + rt_kprintf("First mount ret = %d\n", ret); + if (ret == 0) + { + rt_kprintf("Filesystem mounted successfully!\n"); + return 0; + } + + /* 格式化 */ + rt_kprintf("Formatting...\n"); + ret = dfs_mkfs("elm", "filesystem"); + rt_kprintf("dfs_mkfs ret = %d\n", ret); + if (ret != 0) + { + rt_kprintf("Format failed, ret = %d\n", ret); + return -1; + } + + /* 再次挂载 */ + ret = dfs_mount("filesystem", "/mnt", "elm", 0, 0); + rt_kprintf("Second mount ret = %d\n", ret); + if (ret != 0) + { + rt_kprintf("Mount after format failed, ret = %d\n", ret); + return -1; + } + + rt_kprintf("Filesystem mounted successfully after format!\n"); + return 0; +} +INIT_APP_EXPORT(filesystem_init); + +int main(void) +{ + return 0; +} + +``` + +输出以下内容 +```paintext +[I/FAL] The FAL block device (filesystem) created successfully +[I/app.filesystem] Filesystem initialized! +Block device already exists +First mount ret = -1 +Formatting... +[W/time] Cannot find a RTC device! +dfs_mkfs ret = 0 +Second mount ret = -1 +Mount after format failed, ret = -1 +``` +其实刚才已经挂载成功了,挂载点是fal +![alt text](image-38.png) +![alt text](image-37.png) +删除了自定义的函数,只留main函数的程序重新编译烧录也可以成功初始化文件系统,不知道为什么 + +现在通过API去调用,写一个示例 +```c +#include +void filesystem_test(void){ + int fd; + + fd=open("/fal/rsoc.txt",O_RDWR|O_CREAT); + if (fd>=0){ + write(fd,"hello RSOC",10); + close(fd); + } +} + +MSH_CMD_EXPORT(filesystem_test,filesystem_test); +``` + +在视频的最后也说了一开始不能正确挂载文件系统需要格式化的问题 +```shell +mkfs -t elm filesystem +``` \ No newline at end of file