补充font data 小灯反转笔记

This commit is contained in:
james 2024-07-31 03:56:08 +08:00
parent 85fbfc67e4
commit 370bc14cf4
2 changed files with 149 additions and 4 deletions

View File

@ -274,8 +274,102 @@ void WIFI_CS_PULL_DOWM(void)
INIT_BOARD_EXPORT(WIFI_CS GET_PIN);
```
## 我的实践
### 读取传感器数据,上传到阿里云
### 1. 使用AHT10软件包采集温湿度并上传到阿里云
(合并头两个代码),拼接字符串时我用了`sprintf`,其实应该也可以样例原有的`HAL_Snprintf`的
### 2. 将font分区给挂载上。在温湿度上传上传云端的同时将数据同步放在文件系统处
文件名为Data.txt文件内容
> TempXX ; HumiXX ; Count 1自上电起所采集的数据次数
TempXX ; HumiXX ; Count 2自上电起所采集的数据次数
TempXX ; HumiXX ; Count 3自上电起所采集的数据次数
在`drv_filesystem.c`中实现挂载font
``` c
fal_init();
fal_blk_device_create("font");
/* 创建目录 */
ret = mkdir("/fal/test", 0x777);
if (ret < 0)
{
/* 创建目录失败 */
rt_kprintf("dir error!\n");
}
else
{
/* 创建目录成功 */
rt_kprintf("mkdir ok!\n");
}
/* 挂载块设备"font"到 DFS 目录/fal/test中 */
if (dfs_mount("font", "/fal/test", "elm", 0, 0) == 0)
{
LOG_I("font initialized!");
}
else
{
dfs_mkfs("elm", "font");
if (dfs_mount("font", "/fal/test", "elm", 0, 0) == 0)
{
LOG_I("font initialized!");
}
else
{
LOG_E("Failed to initialize font!");
LOG_D("You should create a filesystem(font) on the block device first!");
}
}
```
接着文件末尾添加数据(Data.txt)
![alt text](image-7.png)
``` c
void make_file()
{
//文件描述符
int fd;
//用只写方式打开文件,如果没有该文件,则创建一个文件
fd = open("/fal/test/Data.txt", O_WRONLY | O_CREAT | O_APPEND); //和原来相比只是把O_TRUNC参数更改为O_APPEND即更改为打开后如果再进行写入将从文件的末尾位置开始写。
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,String);
//如果打开成功
if (fd >= 0)
{
//写入文件
write(fd, tmp, sizeof(tmp));
// rt_kprintf("Write done.\n");
//关闭文件
close(fd);
}
else
{
rt_kprintf("File Open Fail.\n");
}
return;
}
```
### 3. 利用云端给开发板发送指令然后实现小灯翻转
```c
rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT);
//topic_info->payload 为发送的内容,可以据此设置命令
if(rt_pin_read(GPIO_LED_R) == PIN_HIGH)
{
// rt_kprintf("LED_R should be ON\n");
rt_pin_write(GPIO_LED_R, PIN_LOW);
}
else
{
// rt_kprintf("LED_R should be OFF\n");
rt_pin_write(GPIO_LED_R, PIN_HIGH);
}
```
### 完整代码
``` c
#include "rtthread.h"
#include "dev_sign_api.h"
@ -285,6 +379,7 @@ INIT_BOARD_EXPORT(WIFI_CS GET_PIN);
#include <stdio.h>
#include <string.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};
@ -299,6 +394,13 @@ 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, ...);
//定义接受文件内容的缓冲区
char buffer[1026] = {};
char tmp[1026];
#define GPIO_LED_B GET_PIN(F,11)
#define GPIO_LED_R GET_PIN(F,12)
// AHT挂载的总线名字
#define AHT10_I2C_BUS "i2c3"
@ -324,6 +426,18 @@ static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_even
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT);
//topic_info->payload 为发送的内容,可以据此设置命令
if(rt_pin_read(GPIO_LED_R) == PIN_HIGH)
{
// rt_kprintf("LED_R should be ON\n");
rt_pin_write(GPIO_LED_R, PIN_LOW);
}
else
{
// rt_kprintf("LED_R should be OFF\n");
rt_pin_write(GPIO_LED_R, PIN_HIGH);
}
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");
@ -360,14 +474,44 @@ static int example_subscribe(void *handle)
return 0;
}
char tmp[256];
void make_file()
{
//文件描述符
int fd;
//用只写方式打开文件,如果没有该文件,则创建一个文件
fd = open("/fal/test/Data.txt", O_WRONLY | O_CREAT | O_APPEND); //和原来相比只是把O_TRUNC参数更改为O_APPEND即更改为打开后如果再进行写入将从文件的末尾位置开始写。
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,String);
//如果打开成功
if (fd >= 0)
{
//写入文件
write(fd, tmp, sizeof(tmp));
// rt_kprintf("Write done.\n");
//关闭文件
close(fd);
}
else
{
rt_kprintf("File Open Fail.\n");
}
return;
}
int cnt = 0;
void tmp_payload(void)
{
// 读取温湿度值
Humi = aht10_read_humidity(Dev);
Temp = aht10_read_temperature(Dev);
memset(tmp, 0, sizeof(tmp));
sprintf(tmp, "Temp: %.1f;Humi: %.1f;Count: %d\n", Temp, Humi,++cnt);
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp);
make_file();
sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f}}", Temp, Humi);
rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp);
return;
}
static int example_publish(void *handle)
@ -382,7 +526,7 @@ static int example_publish(void *handle)
int topic_len = 0;
char *payload = tmp;
// strcpy(payload,tmp_payload());
rt_kprintf("payload:%s\n",payload);
// rt_kprintf("payload:%s\n",payload);
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
@ -421,6 +565,7 @@ static void mqtt_example_main(void *parameter)
EXAMPLE_TRACE("mqtt example");
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.handle_event.h_fp = example_event_handle;

BIN
Day5/image-7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB