Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
2024-RSOC
星火1号_spark_星火一号_开发板
RW007 传输温湿度数据至阿里云平台
发布于 2024-08-02 14:08:18 浏览:336
订阅该版
[tocm] # RW007和星火一号上阿里云平台教程 将以图文进行分享 ## 首先 在studio创建好工程 然后添加以下四个软件包 ![微信图片_20240802133859.png](https://oss-club.rt-thread.org/uploads/20240802/923783c3eee3292ee6091c5986a59626.png) 版本号一定要正确 不然会存在版本不兼容的问题 导致一堆bug ------------ ## 配置RW007 按照图示进行配置 引脚号参考原理图即可 ![微信图片_20240802133958.png](https://oss-club.rt-thread.org/uploads/20240802/e3f509e109aecf506be8a2c14aac7076.png) ------------ ## 配置阿里云平台 ![微信图片_20240802134120.png](https://oss-club.rt-thread.org/uploads/20240802/ab839f22c122e42ca80bc4626d89178c.png) 阿里云平台 去登录阿里云 搜索物联网平台进入 1——创建产品 默认即可 2——添加设备到 刚刚创建产品的那个名称下 3——创建好后 点击产品——查看——TOPIC列表——自定义topic——编辑——设备操作权限选择发布和订阅——确认 保证 设备的信息能够发布,接收消息,发送消息 然后添加一下我们的模块 ![微信图片_20240802134844.png](https://oss-club.rt-thread.org/uploads/20240802/5707442a0da1b4492c726e10fe2a1153.png.webp) 然后在产品页面和设备页面 填写各个配置poduct_key,product_secret,device_name,device_secret 工程就创建好了 接下来是编写相关代码 以下是mqtt的相关代码示例 ```c /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-07-30 27644 the first version */ /* * 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" extern float humidity, temperature; 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; } } 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; } int example_publish(void *handle) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *payload = "{\"params\":{\"temp\":%d}}"; /*------------------处理温度数据----------------*/ char pay_load[70]; temperature=(int)temperature; rt_kprintf("send temp:%d",(int)temperature); HAL_Snprintf(pay_load,80,payload,(int)temperature); rt_kprintf("Final pay_load: %s\n", pay_load); /*-----------------------处理结束------------*/ 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, pay_load, strlen(pay_load)); 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 */ extern int mqtt_example_main() { 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"); /* 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; rt_thread_mdelay(200); } return 0; } void mqtt_init(){ 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 -1; } res = example_subscribe(pclient); return pclient; if (res < 0) { IOT_MQTT_Destroy(&pclient); return -1; } IOT_MQTT_Yield(pclient, 200); example_publish(pclient); return 0; } static int ali_app(void) { rt_thread_t thread; thread = rt_thread_create("ali",mqtt_example_main , RT_NULL, 4096, 11, 10); if (thread == RT_NULL) { return -RT_ERROR; } rt_thread_startup(thread); return RT_EOK; } MSH_CMD_EXPORT(ali_app,app ali ); #ifdef FINSH_USING_MSH MSH_CMD_EXPORT_ALIAS(mqtt_example_main, ali_mqtt_sample, ali coap sample); #endif ``` 通过获取到的温湿度 然后进行发布 如下代码 ```c //线程1开启温湿度 static void thread1_entry(void *para){ /*------------------温湿度---------------*/ aht10_device_t dev; /* 总线名称 */ const char *i2c_bus_name = "i2c3"; /* 等待传感器正常工作 */ rt_thread_mdelay(2000); /* 初始化 aht10 */ dev = aht10_init(i2c_bus_name); if (dev == RT_NULL) { rt_kprintf(" The sensor initializes failure"); return 0; } /*--------------------结束------------------*/ while (1) { /* 读取湿度 */ humidity = aht10_read_humidity(dev); rt_kprintf("humidity : %d.%d %%\r\n", (int)humidity, (int)(humidity * 10) % 10); /* 读取温度 */ temperature = aht10_read_temperature(dev); rt_kprintf("temperature: %d.%d\r\n", (int)temperature, (int)(temperature * 10) % 10); rt_thread_mdelay(5000); } } //开启阿里云 mqtt static void thread2_entry(void *para){ ali_app(); while(1){ rt_thread_mdelay(2000); } } ``` 打开串口即可看见发送的数据和相关信息了 ![微信图片_20240802131734.png](https://oss-club.rt-thread.org/uploads/20240802/f243eaaf86a60df7f03b5556857fd8af.png.webp) ![微信图片_20240802131805.png](https://oss-club.rt-thread.org/uploads/20240802/05661d3f8815df50fd7ad865af681e22.png) 要先链接wifi才能进行阿里云的发布 然后湿度数据 用相关的接口 模仿温度的进行编写 即可 关于 fmt需要去阿里云平台下的监控运维 在离线下使用——在线调试 设备模拟器 属性上报 然后 找到你创建好的物模型 去点击发送数据 找到fmt的格式 进行复制粘贴即可 然后payload temp就是你创建物模型的名称。 ```c int example_publish(void *handle) { int res = 0; const char *fmt = "/sys/%s/%s/thing/event/property/post"; char *topic = NULL; int topic_len = 0; char *payload = "{\"params\":{\"temp\":%d}}"; ``` ## 发布消息 通过 发送设备消息给rw007 提取payload的消息 通过判断消息类型 实现控制小灯翻转。 ## 后续会出一个小作品给大家学习和参考
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
CHNT_8350
这家伙很懒,什么也没写!
文章
9
回答
0
被采纳
0
关注TA
发私信
相关文章
1
[星火一号] 代码模板, 手动写启动代码, 开机后 snprintf 不能处理 %llu 了, 是有什么配置上的冲突吗?
2
使用MDK5.37开发星火一号,双击mklinks.bat 文件后,目录下没有 rt-thread 和 libraries 的文件夹图标。
3
studio文件构建丢失
4
rtt中星火一号stm-32怎么把两个示例工程合并成一个
5
星火一号串口发送问题
6
基于开发板建工程的疑问
7
使用星火一号开发板建工程的奇怪问题
8
星火一号板pwm功能,不报错,但也不输出,为什么?
9
星火一号怎么强制改变已占用的引脚的功能呢
10
火星一号的标准库在哪里?外设的数据手册在哪里看?
推荐文章
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
MicroPython
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
19
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
6
个答案
2
次被采纳
用户名由3_15位
13
个答案
1
次被采纳
本月文章贡献
程序员阿伟
9
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
RTT_逍遥
1
篇文章
5
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部