Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
【2024-RSOS】 第五天的学习---温湿度数据上传到阿里云
发布于 2024-07-27 23:13:20 浏览:148
订阅该版
[tocm] # 第五天的学习 软件包与组件 ## 1、使用AHT10软件包采集温湿度数据上传到阿里云 mqtt配置过程 ![screenshot_MQTT配置1.png](https://oss-club.rt-thread.org/uploads/20240727/8e0e45afb4b990900ca27534c62de396.png) ![screenshot_MQTT配置2.png](https://oss-club.rt-thread.org/uploads/20240727/a5c109ca1998874a3b49b1b11b9cbc15.png) ![screenshot_MQTT配置3.png](https://oss-club.rt-thread.org/uploads/20240727/341397a5b165166639d63f401786d262.png) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240727/14d984c8817731e45142bfdf778283ef.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240727/65c47c0e4f70a77b74984e7b26006cce.png.webp) 文件系统种类 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20240727/b0c17172fd2db6924c7ba9cd4666bb37.png) ![screenshot_image-20240727190137856.png](https://oss-club.rt-thread.org/uploads/20240727/b8cde79247c0a1345bc60ed956e3cd21.png.webp) 对文件的操作参数 ![screenshot_image-20240727190236080.png](https://oss-club.rt-thread.org/uploads/20240727/9d8a4368808711a027ead68525e91ab9.png.webp) ![screenshot_image-20240727190311885.png](https://oss-club.rt-thread.org/uploads/20240727/8e1c181b4e7a2b783ff67c96650a6641.png) FinSH命令 ![screenshot_image-20240727190355063.png](https://oss-club.rt-thread.org/uploads/20240727/8ef36305933719db4bbc7683eb4b354a.png.webp) ### 文件系统开启 ![screenshot_文件系统开关1-17219035200901.png](https://oss-club.rt-thread.org/uploads/20240727/f512f1cf85ef8b55ab9225371eca6c71.png) ![screenshot_文件系统开关2-17219035761332.png](https://oss-club.rt-thread.org/uploads/20240727/6a0d8f4ee14f568fa8c032e761147221.png) ![screenshot_文件系统开关3-17219036284683.png](https://oss-club.rt-thread.org/uploads/20240727/bcfcd2bb0ab970c2db0a397ce2a34e27.png) ![screenshot_文件系统开关4-17219037926974.png](https://oss-club.rt-thread.org/uploads/20240727/d61c80b64cf507fcb380e3429ad9c832.png) ![screenshot_文件系统开关5.png](https://oss-club.rt-thread.org/uploads/20240727/625d90c6d47c025c320aa53b210bc801.png) 开发板把aht10采集到的温湿度数据上传到阿里云平台和储存到自身的文件系统中 代码: ```c /* * Copyright (C) 2015-2018 Alibaba Group Holding Limited * * Again edit by rt-thread group * Change Logs: * Date Author Notes * 2019-07-21 MurphyZhao first edit */ #include "rtthread.h" #include "dev_sign_api.h" #include "mqtt_api.h" #include
#include
#include "aht10.h" #include
//需要添加软件包进这里 // AHT挂载的总线名字 #define AHT10_I2C_BUS "i2c3" // 创建AHT线程时待用 #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 2048 #define THREAD_TIMESLICE 5 // // AHT线程指针 // rt_thread_t AHT10 = RT_NULL; char String[512] = {0}; //定义接受文件内容的缓冲区 char buffer[100] = {}; void FileSystem_Test(void) { //文件描述符 int fd; //用只写方式打开文件,如果没有该文件,则创建一个文件 fd = open("/fal/Data.txt", O_WRONLY | O_CREAT); //如果打开成功 if (fd >= 0) { //写入文件 write(fd, String, sizeof(String)); rt_kprintf("Write done.\n"); //关闭文件 close(fd); } else { rt_kprintf("File Open Fail.\n"); } //用只读方式打开文件 fd = open("/fal/Data.txt", O_RDONLY); if (fd>= 0) { //读取文件内容 rt_uint32_t size = read(fd, buffer, sizeof(buffer)); if (size < 0) { rt_kprintf("Read File Fail.\n"); return ; } //输出文件内容 rt_kprintf("Read from file test.txt : %s \n", buffer); //关闭文件 close(fd); } else { rt_kprintf("File Open Fail.\n"); } } 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设备指针 aht10_device_t Dev = RT_NULL; // Humi:湿度值,Temp:温度值 float Humi, Temp; int i; // 初始化设备 Dev = aht10_init(AHT10_I2C_BUS); if (Dev == RT_NULL) { rt_kprintf("AHT10_init Fail"); //return; } // 读取温湿度值 Humi = aht10_read_humidity(Dev); Temp = aht10_read_temperature(Dev); i++; rt_snprintf(String, sizeof(String),"Temp:%d ;Humi: %d ;Count: %d",(int)Temp,(int)Humi,i); FileSystem_Test(); // 没有下载rt_vsprintf_full软件包的时候 rt_kprintf("Humi: %d.%d\n", (int)Humi, (int)(Humi * 10) % 10); rt_kprintf("Temp: %d.%d\n", (int)Temp, (int)(Temp * 10) % 10); int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; //char *payload = "{\"params\":{\"temp\": 26}}"; char payload[500] ={0}; // 分配足够的空间来存储最终的字符串 // 使用snprintf来避免缓冲区溢出 // 注意:%f用于格式化浮点数,.2f表示保留两位小数 rt_snprintf(payload, sizeof(payload), "{\"id\":1722009943010,\"params\":{\"temp\":%d,\"Humidity\":%d},\"version\":\"1.0\",\"method\":\"thing.event.property.post\"}", (int)Temp,(int )Humi); //rt_kprintf("%s",payload); 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; } static void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg) { EXAMPLE_TRACE("msg->event_type : %d", msg->event_type); } /* * NOTE: About demo topic of /${productKey}/${deviceName}/user/get * * The demo device has been configured in IoT console (https://iot.console.aliyun.com) * so that its /${productKey}/${deviceName}/user/get can both be subscribed and published * * We design this to completely demonstrate publish & subscribe process, in this way * MQTT client can receive original packet sent by itself * * For new devices created by yourself, pub/sub privilege also requires being granted * to its /${productKey}/${deviceName}/user/get for successfully running whole example */ static int my_mqtt_example_main(int argc, char *argv[]) { void *pclient = NULL; int res = 0; int loop_cnt = 0; iotx_mqtt_param_t mqtt_params; // rt_err_t result; // /* 初始化消息队列 */ // result = rt_mq_init(&mq, // "mqt", // &msg_pool[0], /* 内存池指向 msg_pool */ // 1, /* 每个消息的大小是 1 字节 */ // sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */ // RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,优先级大小的方法分配消息 */ // if (result != RT_EOK) // { // rt_kprintf("init message queue failed.\n"); // return -1; // } // // 创建线程 // AHT10 = rt_thread_create("AHT10", AHT10_Test, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); // // 创建成功就启动 // if (AHT10 != RT_NULL) // { // rt_thread_startup(AHT10); // } // else // { // rt_kprintf("AHT10_Thread Create Fail"); // } HAL_GetProductKey(DEMO_PRODUCT_KEY); HAL_GetDeviceName(DEMO_DEVICE_NAME); HAL_GetDeviceSecret(DEMO_DEVICE_SECRET); EXAMPLE_TRACE("mqtt example"); /* Initialize MQTT parameter */ /* * Note: * * If you did NOT set value for members of mqtt_params, SDK will use their default values * If you wish to customize some parameter, just un-comment value assigning expressions below * **/ memset(&mqtt_params, 0x0, sizeof(mqtt_params)); /** * * MQTT connect hostname string * * MQTT server's hostname can be customized here * * default value is ${productKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com */ /* mqtt_params.host = "something.iot-as-mqtt.cn-shanghai.aliyuncs.com"; */ /** * * MQTT connect port number * * TCP/TLS port which can be 443 or 1883 or 80 or etc, you can customize it here * * default value is 1883 in TCP case, and 443 in TLS case */ /* mqtt_params.port = 1883; */ /** * * MQTT request timeout interval * * MQTT message request timeout for waiting ACK in MQTT Protocol * * default value is 2000ms. */ /* mqtt_params.request_timeout_ms = 2000; */ /** * * MQTT clean session flag * * If CleanSession is set to 0, the Server MUST resume communications with the Client based on state from * the current Session (as identified by the Client identifier). * * If CleanSession is set to 1, the Client and Server MUST discard any previous Session and Start a new one. * * default value is 0. */ /* mqtt_params.clean_session = 0; */ /** * * MQTT keepAlive interval * * KeepAlive is the maximum time interval that is permitted to elapse between the point at which * the Client finishes transmitting one Control Packet and the point it starts sending the next. * * default value is 60000. */ /* mqtt_params.keepalive_interval_ms = 60000; */ /** * * MQTT write buffer size * * Write buffer is allocated to place upstream MQTT messages, MQTT client will be limitted * to send packet no longer than this to Cloud * * default value is 1024. * */ /* mqtt_params.write_buf_size = 1024; */ /** * * MQTT read buffer size * * Write buffer is allocated to place downstream MQTT messages, MQTT client will be limitted * to recv packet no longer than this from Cloud * * default value is 1024. * */ /* mqtt_params.read_buf_size = 1024; */ /** * * MQTT event callback function * * Event callback function will be called by SDK when it want to notify user what is happening inside itself * * default value is NULL, which means PUB/SUB event won't be exposed. * */ mqtt_params.handle_event.h_fp = example_event_handle; pclient = IOT_MQTT_Construct(&mqtt_params); if (NULL == pclient) { EXAMPLE_TRACE("MQTT construct failed"); return -1; } res = example_subscribe(pclient); if (res < 0) { IOT_MQTT_Destroy(&pclient); return -1; } while (1) { if (0 == loop_cnt % 20) { example_publish(pclient); } IOT_MQTT_Yield(pclient, 200); loop_cnt += 1; } return 0; } #ifdef FINSH_USING_MSH MSH_CMD_EXPORT_ALIAS(my_mqtt_example_main, my_ali_mqtt_sample, ali coap sample); #endif ``` 实验结果 ![screenshot_image-20240727224158205.png](https://oss-club.rt-thread.org/uploads/20240727/b01cae6f42a09f1255fb484b4d8aebdb.png)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
XLUR_8603
这家伙很懒,什么也没写!
文章
7
回答
0
被采纳
0
关注TA
发私信
相关文章
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
ulog
C++_cpp
at_device
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
8
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部