Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
2024-RSOC
【RSOS-2024】 温度传感器连接阿里云物联网平台
发布于 2024-07-29 17:45:32 浏览:404
订阅该版
[tocm] # [2024-RSOS] 温度传感器,并将数据上传至阿里云平台 ## 需要配置环境 - rw007 - aliyun sdk - beep - AHT21 ## 实现功能流程 ### 如果未启动 - 按下按钮以启动线程 - 蜂鸣器叫 - 收集数据并存储邮箱 - 读取邮箱并发送数据 ### 如果已经启动 - 按下按钮以启动线程 - 蜂鸣器叫 - 关闭线程并回收资源 ``` #include
#include
#include
#include
#include
#include
#include "dev_sign_api.h" #include "mqtt_api.h" #define LOG_TAG "my_work.app" #define LOG_LVL LOG_LVL_DBG #include
static char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0}; static char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0}; static 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: 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 = "/sys/%s/%s/thing/event/property/post_reply"; 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 void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) { EXAMPLE_TRACE("msg->event_type : %d", msg->event_type); } // 按钮计数器 static int num = 1; // 定义按钮引脚 #define KEY_LEFT GET_PIN(C, 0) // 定义蜂鸣器引脚 #define BUZZER GET_PIN(B, 0) // 定义 AHT10 I2C 设备名称 #define AHT10_I2C_BUS "i2c3" // 定义信号量和线程句柄 static rt_sem_t key_sem = RT_NULL; static aht10_device_t dev = RT_NULL; static rt_thread_t ger_temp_hum_thread = RT_NULL; static rt_thread_t send_aliyun_thread = RT_NULL; static rt_mailbox_t MbForTempAndHumidity = RT_NULL; static rt_sem_t key_ger_send = RT_NULL; static void *pclient = NULL; static int res = 0; static iotx_mqtt_param_t mqtt_params; // 定义数据结构体 typedef struct Data { float temperature; float humidity; } Data, *Data_t; void init_data(Data_t data, float _temperature, float _humidity) { data->temperature = _temperature; data->humidity = _humidity; } // static int example_publish(void *handle) // { // int res = 0; // const char *fmt = "/%s/%s/user/get"; // char *topic = NULL; // int topic_len = 0; // char *payload = "{\"message\":\"hello!\"}"; // topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1; // topic = HAL_Malloc(topic_len); // if (topic == NULL) { // EXAMPLE_TRACE("memory not enough"); // return -1; // } // memset(topic, 0, topic_len); // HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME); // res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload)); // if (res < 0) { // EXAMPLE_TRACE("publish failed, res = %d", res); // HAL_Free(topic); // return -1; // } // HAL_Free(topic); // return 0; // } // MQTT发布函数 static int example_publish(void *handle, Data_t data) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *payload = NULL; // 组装 负载 payload = HAL_Malloc(54); memset(payload,0,54); HAL_Snprintf(payload, 54, "{\"params\":{\"temperature\":\"%0.2f\",\"Humidity\":\"%0.2f\"}}", data->temperature, data->humidity); //组装主题 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(handle, 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; } // 发送数据到阿里云线程函数 void send_aliyun(void *param) { while (1) { rt_sem_take(key_ger_send, RT_WAITING_FOREVER); HAL_GetProductKey(DEMO_PRODUCT_KEY); HAL_GetDeviceName(DEMO_DEVICE_NAME); HAL_GetDeviceSecret(DEMO_DEVICE_SECRET); memset(&mqtt_params, 0x0, sizeof(mqtt_params)); mqtt_params.handle_event.h_fp = example_event_handle; if(pclient == NULL){ 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; } while (1) { Data_t data_aliyun = (Data_t)rt_malloc(sizeof(Data)); if ((rt_mb_recv(MbForTempAndHumidity, (rt_uint32_t *)&data_aliyun, RT_WAITING_FOREVER)) == RT_EOK) { LOG_I("接受的指针地址,%x",data_aliyun); LOG_E("temp:\t%.2f\thum:\t%.2f",data_aliyun->temperature,data_aliyun->humidity); // example_publish(pclient, data_aliyun); example_publish(pclient,data_aliyun); } rt_free(data_aliyun); } } } // 温湿度获取线程函数 static void ger_temp_hum(void *param) { float temperature = 0.0; float humidity = 0.0; int flag = (int)param; if (flag == 1) { while (1) { temperature = aht10_read_temperature(dev); humidity = aht10_read_humidity(dev); Data_t data = (Data_t)rt_malloc(sizeof(Data)); init_data(data, temperature, humidity); LOG_I("发送的指针地址,%x",data); LOG_I("温度: %0.2f 湿度: %0.2f", temperature, humidity); if ((rt_mb_urgent(MbForTempAndHumidity, (rt_uint32_t)data)) != RT_EOK) { //读取一个并释放一个 Data_t temp = (Data_t)rt_malloc(sizeof(Data)); rt_mb_recv(MbForTempAndHumidity, (rt_uint32_t *)&temp, RT_WAITING_FOREVER); rt_free(temp); rt_mb_urgent(MbForTempAndHumidity, (rt_uint32_t)data); } rt_thread_mdelay(500); } } else { LOG_I("线程挂起,等待下一次按钮激活"); } } // 按键回调函数 void key_left_callback(void *args) { rt_sem_release(key_sem); LOG_I("按键左被按下!"); } // 配置按键中断 void key_call_set() { rt_pin_mode(KEY_LEFT, PIN_MODE_INPUT_PULLUP); rt_pin_attach_irq(KEY_LEFT, PIN_IRQ_MODE_FALLING, key_left_callback, RT_NULL); rt_pin_irq_enable(KEY_LEFT, PIN_IRQ_ENABLE); } // 主线程入口函数 static void thread_entry(void *param) { while (1) { rt_sem_take(key_sem, RT_WAITING_FOREVER); //添加蜂鸣器 beep_init(BUZZER,PIN_LOW); beep(3,1000,70,2500); if (ger_temp_hum_thread == RT_NULL) { if (dev == RT_NULL) { dev = aht10_init(AHT10_I2C_BUS); } if (MbForTempAndHumidity == RT_NULL) { MbForTempAndHumidity = rt_mb_create("MbForTempAndHumidity",1024, RT_IPC_FLAG_FIFO); } if (key_ger_send == RT_NULL) { key_ger_send = rt_sem_create("ger_send", 0, RT_IPC_FLAG_FIFO); } ger_temp_hum_thread = rt_thread_create( "ger", ger_temp_hum, (void *)num, 1024, 25, 5); rt_thread_startup(ger_temp_hum_thread); send_aliyun_thread = rt_thread_create( "send", send_aliyun, RT_NULL,5120, 26, 5); rt_thread_startup(send_aliyun_thread); num = 0; rt_sem_release(key_ger_send); } else { if (dev != RT_NULL) { aht10_deinit(dev); dev = RT_NULL; } if (MbForTempAndHumidity != RT_NULL) { rt_mb_delete(MbForTempAndHumidity); MbForTempAndHumidity = RT_NULL; } rt_thread_delete(ger_temp_hum_thread); rt_thread_delete(send_aliyun_thread); if (key_ger_send != RT_NULL) { rt_sem_delete(key_ger_send); key_ger_send = RT_NULL; } if (num == 0) { ger_temp_hum_thread = rt_thread_create( "aliyun", ger_temp_hum, (void *)num, 1024, 25,5); rt_thread_startup(ger_temp_hum_thread); num = 1; } ger_temp_hum_thread = RT_NULL; send_aliyun_thread = RT_NULL; } } } // 初始化函数 void my_work() { key_sem = rt_sem_create("key_sem", 0, RT_IPC_FLAG_PRIO); if (key_sem == RT_NULL) { LOG_E("创建信号量失败"); return; } key_call_set(); rt_thread_t main_thread = rt_thread_create( "main_thread", thread_entry, RT_NULL, 1024, 25, 5); if (main_thread != RT_NULL) { rt_thread_startup(main_thread); } else { LOG_E("创建主线程失败"); } } // 导出 my_work 命令 MSH_CMD_EXPORT(my_work, my_work); ``` *** # 文件系统 ## FAL介绍 FAL(Flash Abstraction Layer)Flash 抽象层,是对Flash的分区进行管理,操作的抽象层,对上层统一了Flash 及其分区操作API,有以下特点 - 支持静态可配置的分区表,并可关联多个 Flash 设备; - 分区表支持 **自动装载** 。避免在多固件项目,分区表被多次定义的问题; - 代码精简,对操作系统 **无依赖** ,可运行于裸机平台,比如对资源有一定要求的 Bootloader; - 统一的操作接口。保证了文件系统、OTA、NVM(例如:[EasyFlash](https://github.com/armink-rtt-pkgs/EasyFlash)) 等对 Flash 有一定依赖的组件,底层 Flash 驱动的可重用性; - 自带基于 Finsh/MSH 的测试命令,可以通过 Shell 按字节寻址的方式操作(读写擦) Flash 或分区,方便开发者进行调试、测试; ![fal_framework.png](https://oss-club.rt-thread.org/uploads/20240729/892ae251344dcf0a526b57fb1f97d488.png) ## FAL API ![fal-api.png](https://oss-club.rt-thread.org/uploads/20240729/b33ff3f582a415a5dbbec741dcde66ca.png) ## 命令测试 * fal ![fal.png](https://oss-club.rt-thread.org/uploads/20240729/ab58d24d53cc369cf05e49c5915cd088.png) * fal probe ![fal probe.png](https://oss-club.rt-thread.org/uploads/20240729/29f586451f99ca70267688c32277c99b.png) * fal msh ![fal-msh.png](https://oss-club.rt-thread.org/uploads/20240729/76292cecccf9d80c47bf9634c53355be.png)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
你好鸭
这家伙很懒,什么也没写!
文章
6
回答
1
被采纳
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
ota在线升级
UART
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
张世争
8
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部