RT-Thread 软件包和组件的学习
环境:rt-thread:master分支
板卡:星火一号
一、采集温湿度上传MQTT云平台
1.软件包的优势
星火一号的板载了AHT10,如果我们没用软件包的形式想去读取温湿度的话,那么我们是不是得自己去查找AHT10的手册,自己用IIC去读取相对于的寄存器,但是如果有了软件包之后我们就可以用别人提取写好的API去读取温湿度,这样下来大大提高了开发的效率。
那么我们该如何去查找对应的软件包呢?
可以现在官网上查看是否又对应的软件包:软件包官网
如果有的话,我们就可以使用env将对应的包使能。
2.AHT10读取温湿度
a.使能AHT10软件包
如何知道这个软件包在那个具体的位置呢?
可以使用env中menuconfig的搜索功能或者是在软件包的官网搜索知道他的分类。
- menuconfig中搜索
可以看到按下键盘中的/
就可以跳转对应的位置
我们按下/
搜索AHT10,就可以看到有关AHT10的相关配置
- 从官网中查看相对于的分类
点进去可以查看到该软件包的分类。
我们就可以通过对应的分类路径,找到AHT10的软件包
我们就可以使能相对于的软件包,保持并退出之后。使用pkgs --update
更新软件包。
就可以看到相对于的软件包从github中拉取了下来。
如果因为网络问题拉取不下来软件包,可以切换国内的gitee源。
在env
中输入 menuconfig -s
,进入env的配置,设置下载的服务,选择gitee。
b.如何使用AHT10的API
一般对应的软件包的作者都会写Readme文件来教使用者如何简单的使用。
我们就可以快速的通过Readme文件去使用API对AHT10进行读取温湿度。
以下是我通过readme文档自己编写的一个读取温湿度的demo
#include <rtthread.h>
#include <board.h>
#include <drv_gpio.h>
#include "aht10.h"
//星火一号AHT10使用的为i2c3
#define AHT10_I2C_BUS "i2c3"
//定义线程的信息
#define THREAD_PRIORITY 25 //线程优先级
#define THREAD_STACK_SIZE 2048 //线程栈大小
#define THREAD_TIMSLICE 5 //线程时间片
//AHT线程指针
rt_thread_t AHT10 = RT_NULL;
void aht10_demo(void * paramenter)
{
aht10_device_t aht10_dev = RT_NULL;
aht10_dev = aht10_init(AHT10_I2C_BUS);
if(aht10_dev == RT_NULL)
{
rt_kprintf("aht10 init fail\n");
return;
}
float humi = 0,temp = 0;
//读取温湿度
while(1)
{
temp = aht10_read_temperature(aht10_dev);
humi = aht10_read_humidity(aht10_dev);
rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10);
rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10);
rt_thread_mdelay(1000);
}
}
void aht10_task(void)
{
AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY,THREAD_TIMSLICE);
if(AHT10 == RT_NULL)
{
rt_kprintf("aht10 thread create fail\n");
return;
}
rt_thread_startup(AHT10);
return;
}
MSH_CMD_EXPORT(aht10_task,aht10 read thread);
通过控制台可以看到温湿度读取成功。
ps:rt_kprintf()本身是不支持浮点型输出的,可以使用rt_kpintf_full
来使能浮点型输出。
3.MQTT上传阿里云
a.创建阿里云实例
阿里云物联网平台:官网
- 登陆阿里云物联网平台,选择管理控制台
- 创建一个公共的实例
- 创建一个产品
- 创建产品
此时我们的产品以及设备就以及创建好了,我们只需要一些关键的参数即可。
b.打开RW007
修改RW007的引脚与星火一号对应。
c.使能阿里云软件包
打开云端
使能阿里云,并配置相应的参数。
这几个参数可以在阿里云的产品以及设备中获取。
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%2FUsers%2FCYFSybW%2FAppData%2FRoaming%2FTypora%2Ftypora-user-images%2Fimage-20240726215616709.png!%5Bimage-20240726221006794%5D(https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2FCYFS3%2FTyproa%2F202407270115747.png&pos_id=img-rkUvikR8-1723723401285)
把相对于的参数复制到menuconfig中。
使能案例。
推出menuconfig并保存,更新包pkgs --update
,编译scons
,可以看到有报错,是因为新版RTT API的变得,把对应报错的文件中的RT_WEAK改为小写即可。
再次编译即可。下载程序。
连接wifi,wifi的频段必须在2.4Ghz。
按下TAB,可以看到有阿里云的mqtt示例。
d.上传物模型通信的主题
物模型通信的主题
设置相对应的物模型功能。
参考示例代码,修改发布温湿度的主题。
aht_publish.h
#ifndef __AHT_PUBLISH_H__
#define __AHT_PUBLISH_H__
#include <rtthread.h>
typedef struct aht_publish
{
/* data */
rt_int32_t temp_int;
rt_int32_t temp_dec;
rt_int32_t humi_int;
rt_int32_t humi_dec;
}aht_publish;
#endif
aht_publish.c
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*
* Again edit by rt-thread group
* Change Logs:
* Date Author Notes
* 2019-07-21 MurphyZhao first edit
*/
#include "dev_sign_api.h"
#include "mqtt_api.h"
#include "aht_publish.h"
#include <board.h>
#include <drv_gpio.h>
#include "aht10.h"
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#define EXAMPLE_TRACE(fmt, ...) \
do \
{ \
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
HAL_Printf(fmt, ##__VA_ARGS__); \
HAL_Printf("%s", "\r\n"); \
} while (0)
static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
switch (msg->event_type)
{
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic);
EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload);
EXAMPLE_TRACE("\n");
break;
default:
break;
}
}
static int example_subscribe(void *handle)
{
int res = 0;
const char *fmt = "/%s/%s/user/get";
char *topic = NULL;
int topic_len = 0;
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0)
{
EXAMPLE_TRACE("subscribe failed");
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
static int example_publish(void *handle,aht_publish * data)
{
int res = 0;
const char *fmt = "/sys/%s/%s/thing/event/property/post";
char *topic = NULL;
int topic_len = 0;
char *payload = "{\"params\":{\"CurrentTemperature\":%d.%d,\"CurrentHumidity\":%d.%d}}";
char message[256];
HAL_Snprintf(message,256,payload,data->temp_int,data->temp_dec,data->humi_int,data->humi_dec);
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, message, strlen(message));
if (res < 0)
{
EXAMPLE_TRACE("publish failed, res = %d", res);
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
static void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
EXAMPLE_TRACE("msg->event_type : %d", msg->event_type);
}
/*-----------------------------------------------------------------------------------*/
//星火一号AHT10使用的为i2c3
#define AHT10_I2C_BUS "i2c3"
//定义线程的信息
#define THREAD_PRIORITY 10 //线程优先级
#define THREAD_STACK_SIZE 2048 //线程栈大小
#define THREAD_TIMSLICE 5 //线程时间片
//发送数据邮箱
rt_mailbox_t aht10_mailbox = RT_NULL;
//AHT线程指针
rt_thread_t AHT10 = RT_NULL;
void aht10_demo(void * paramenter)
{
aht10_device_t aht10_dev = RT_NULL;
aht10_dev = aht10_init(AHT10_I2C_BUS);
if(aht10_dev == RT_NULL)
{
rt_kprintf("aht10 init fail\n");
return;
}
float humi = 0,temp = 0;
aht_publish data = {0};
//读取温湿度
while(1)
{
temp = aht10_read_temperature(aht10_dev);
humi = aht10_read_humidity(aht10_dev);
data.temp_int = (int)temp;
data.temp_dec = (int)(temp*10)%10;
data.humi_int = (int)humi;
data.humi_dec = (int)(humi*10)%10;
rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10);
rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10);
rt_mb_send(aht10_mailbox,(rt_uint32_t)&data);
rt_thread_mdelay(1000);
}
}
void aht10_task(void)
{
AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,9,THREAD_TIMSLICE);
if(AHT10 == RT_NULL)
{
rt_kprintf("aht10 thread create fail\n");
return;
}
rt_thread_startup(AHT10);
return;
}
void mqtt_example_main(void *parameter)
{
void *pclient = NULL;
int res = 0;
int loop_cnt = 0;
iotx_mqtt_param_t mqtt_params;
HAL_GetProductKey(DEMO_PRODUCT_KEY);
HAL_GetDeviceName(DEMO_DEVICE_NAME);
HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);
EXAMPLE_TRACE("mqtt example");
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.handle_event.h_fp = example_event_handle;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient)
{
EXAMPLE_TRACE("MQTT construct failed");
return;
}
res = example_subscribe(pclient);
if (res < 0)
{
IOT_MQTT_Destroy(&pclient);
return;
}
aht_publish * data;
while (1)
{
if(rt_mb_recv(aht10_mailbox,(rt_uint32_t *)&data,RT_WAITING_FOREVER) == RT_EOK)
{
example_publish(pclient,data);
}
IOT_MQTT_Yield(pclient, 200);
}
}
rt_thread_t MQTT_Thread;
void MQTT_Creat_Thread(void)
{
// 创建线程
MQTT_Thread = rt_thread_create("MQTT_Thread", mqtt_example_main, RT_NULL, 4096, 10, 5);
// 创建成功就启动
if (MQTT_Thread != RT_NULL)
{
rt_thread_startup(MQTT_Thread);
}
else
{
rt_kprintf("MQTT_Thread_Thread Create Fail");
}
}
void aht10_publish(void)
{
aht10_mailbox = rt_mb_create("aht10",10,RT_IPC_FLAG_PRIO);
if(aht10_mailbox == RT_NULL)
{
rt_kprintf("create mailbox fail\n");
return;
}
aht10_task();
MQTT_Creat_Thread();
}
MSH_CMD_EXPORT(aht10_publish, aht10 publish init);
结果:
二、RT-thread 组件
1.文件系统
使能文件系统
因为W25q64一次性只能擦除一个扇区也就是4K
重新编译,注意可能第一次使用的时候需要手动挂载
2.API MSH
这部分内容官方文档写得很详细了,请参看官网文档(绝对不是因为我懒,不想写)
3.作业
不想解释了,上代码
.h
#ifndef __AHT_PUBLISH_H__
#define __AHT_PUBLISH_H__
#include <rtthread.h>
typedef struct aht_publish
{
/* data */
rt_int32_t temp_int;
rt_int32_t temp_dec;
rt_int32_t humi_int;
rt_int32_t humi_dec;
}aht_publish;
#endif
.c
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*
* Again edit by rt-thread group
* Change Logs:
* Date Author Notes
* 2019-07-21 MurphyZhao first edit
*/
#include "dev_sign_api.h"
#include "mqtt_api.h"
#include "aht_publish.h"
#include "cjson.h"
#include <board.h>
#include <drv_gpio.h>
#include "aht10.h"
#include <dfs_posix.h>
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#define EXAMPLE_TRACE(fmt, ...) \
do \
{ \
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
HAL_Printf(fmt, ##__VA_ARGS__); \
HAL_Printf("%s", "\r\n"); \
} while (0)
static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
switch (msg->event_type)
{
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic);
EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload);
EXAMPLE_TRACE("\n");
cJSON *root = cJSON_Parse(topic_info->payload);
cJSON *params = cJSON_GetObjectItem(root, "params");
cJSON *LED = cJSON_GetObjectItem(params, "LEDSwitch");
if (LED != NULL)
{
rt_kprintf("LED Switch: %d", LED->valueint);
if(LED->valueint == 0)
{
rt_kprintf("LED ON\n");
rt_pin_write(GET_PIN(F, 12), PIN_HIGH);
}
else
{
rt_kprintf("LED OFF\n");
rt_pin_write(GET_PIN(F, 12), PIN_LOW);
}
}
cJSON_Delete(root);
break;
default:
break;
}
}
static int example_subscribe(void *handle)
{
int res = 0;
const char *fmt = "/sys/%s/%s/thing/service/property/set";
char *topic = NULL;
int topic_len = 0;
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0)
{
EXAMPLE_TRACE("subscribe failed");
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
static int example_publish(void *handle,aht_publish * data)
{
int res = 0;
const char *fmt = "/sys/%s/%s/thing/event/property/post";
char *topic = NULL;
int topic_len = 0;
char *payload = "{\"params\":{\"CurrentTemperature\":%d.%d,\"CurrentHumidity\":%d.%d}}";
char message[256];
HAL_Snprintf(message,256,payload,data->temp_int,data->temp_dec,data->humi_int,data->humi_dec);
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, message, strlen(message));
if (res < 0)
{
EXAMPLE_TRACE("publish failed, res = %d", res);
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
static void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
EXAMPLE_TRACE("msg->event_type : %d", msg->event_type);
}
/*-----------------------------------------------------------------------------------*/
//星火一号AHT10使用的为i2c3
#define AHT10_I2C_BUS "i2c3"
//定义线程的信息
#define THREAD_PRIORITY 10 //线程优先级
#define THREAD_STACK_SIZE 2048 //线程栈大小
#define THREAD_TIMSLICE 5 //线程时间片
//发送数据邮箱
rt_mailbox_t aht10_mailbox = RT_NULL;
//AHT线程指针
rt_thread_t AHT10 = RT_NULL;
void aht10_demo(void * paramenter)
{
aht10_device_t aht10_dev = RT_NULL;
aht10_dev = aht10_init(AHT10_I2C_BUS);
if(aht10_dev == RT_NULL)
{
rt_kprintf("aht10 init fail\n");
return;
}
int fd = 0;
int count = 0;
float humi = 0,temp = 0;
aht_publish data = {0};
char buf[128];
fd = open("/fal/Data.txt",O_TRUNC|O_RDWR );
if(fd < 0)
{
rt_kprintf("open file fail\n");
return;
}
//读取温湿度
while(1)
{
temp = aht10_read_temperature(aht10_dev);
humi = aht10_read_humidity(aht10_dev);
data.temp_int = (int)temp;
data.temp_dec = (int)(temp*10)%10;
data.humi_int = (int)humi;
data.humi_dec = (int)(humi*10)%10;
rt_kprintf("aht10 temp %d.%d\n",(int)temp,(int)(temp*10)%10);
rt_kprintf("aht10 humi %d.%d\n",(int)humi,(int)(humi*10)%10);
rt_mb_send(aht10_mailbox,(rt_uint32_t)&data);
rt_snprintf(buf,128,"Temp: %d.%d;Humi: %d.%d; Count: %d\n",(int)data.temp_int,(int)data.temp_dec,(int)data.humi_int,(int)data.humi_dec,++count);
ssize_t write_len = 0;
write_len = write(fd,buf,strlen(buf));
if(write_len <= 0)
{
rt_kprintf("write file fail\n");
close(fd);
return;
}
fsync(fd);
rt_thread_mdelay(5000);
}
}
void aht10_task(void)
{
AHT10 = rt_thread_create("aht10",aht10_demo,RT_NULL,THREAD_STACK_SIZE,9,THREAD_TIMSLICE);
if(AHT10 == RT_NULL)
{
rt_kprintf("aht10 thread create fail\n");
return;
}
rt_thread_startup(AHT10);
return;
}
void mqtt_example_main(void *parameter)
{
void *pclient = NULL;
int res = 0;
int loop_cnt = 0;
iotx_mqtt_param_t mqtt_params;
HAL_GetProductKey(DEMO_PRODUCT_KEY);
HAL_GetDeviceName(DEMO_DEVICE_NAME);
HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);
EXAMPLE_TRACE("mqtt example");
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.handle_event.h_fp = example_event_handle;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient)
{
EXAMPLE_TRACE("MQTT construct failed");
return;
}
res = example_subscribe(pclient);
if (res < 0)
{
IOT_MQTT_Destroy(&pclient);
return;
}
aht_publish * data;
while (1)
{
if(rt_mb_recv(aht10_mailbox,(rt_uint32_t *)&data,RT_WAITING_FOREVER) == RT_EOK)
{
example_publish(pclient,data);
}
IOT_MQTT_Yield(pclient, 200);
}
}
rt_thread_t MQTT_Thread;
void MQTT_Creat_Thread(void)
{
// 创建线程
MQTT_Thread = rt_thread_create("MQTT_Thread", mqtt_example_main, RT_NULL, 4096, 10, 5);
// 创建成功就启动
if (MQTT_Thread != RT_NULL)
{
rt_thread_startup(MQTT_Thread);
}
else
{
rt_kprintf("MQTT_Thread_Thread Create Fail");
}
}
void aht10_publish(void)
{
aht10_mailbox = rt_mb_create("aht10",10,RT_IPC_FLAG_PRIO);
if(aht10_mailbox == RT_NULL)
{
rt_kprintf("create mailbox fail\n");
return;
}
aht10_task();
MQTT_Creat_Thread();
}
MSH_CMD_EXPORT(aht10_publish, aht10 publish init);
/* 文件操作系统 */
#define WIFI_CS_PIN GET_PIN(F,10)
int WIFI_CS_DWON(void)
{
rt_pin_mode(WIFI_CS_PIN,PIN_MODE_OUTPUT);
rt_pin_write(WIFI_CS_PIN,PIN_LOW);
return RT_EOK;
}
INIT_BOARD_EXPORT(WIFI_CS_DWON);
运行结果