# V0.2 实现数据上传HA

This commit is contained in:
jxh
2025-03-20 21:38:21 +08:00
parent 55eb2bed54
commit 58efe255f0
4 changed files with 120 additions and 107 deletions

View File

@ -15,6 +15,10 @@
#include "oled12864_drv.h"
// #include "sensor_json.h"
char txBuffer[29]; // 固定长度发送缓冲区
void packData (uint16_t vol, uint16_t cur, uint16_t pwr, uint16_t tmp,
uint16_t hum); // 数据打包函数
void
SystemClock_Init (void)
{
@ -31,8 +35,8 @@ main ()
u16 ina226_voltage = 0;
u16 ina226_current = 0;
u16 ina226_power = 0;
// char *p_json_str = NULL;
char *p_json_str = NULL;
SystemClock_Init (); // 时钟配置
Timer_Init (); // 初始化定时器
@ -45,7 +49,7 @@ main ()
IIC_Init ();
OLED_Init ();
INA226_Init (0.005f, 2.0f);
Key_Init ();
// Key_Init ();
DelayMs (100); // 初始化延时
@ -62,38 +66,47 @@ main ()
OLED_ShowNum (0, 1, GetUpTime (), 10, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
OLED_SHOW);
ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 1000);
ina226_voltage = (int16_t)(INA226_ReadBusVoltage () * 100);
OLED_ShowNum (0, 2, ina226_voltage, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
OLED_SHOW);
ina226_current = (int16_t)(INA226_ReadCurrent () * 1000);
ina226_current = (int16_t)(INA226_ReadCurrent () * 100);
OLED_ShowNum (48, 2, ina226_current, 5, OLED_FONT_SIXTEEN,
OLED_LEFT_ROLL, OLED_SHOW);
ina226_power = (int16_t)(INA226_ReadPower () * 1000);
ina226_power = (int16_t)(INA226_ReadPower () * 100);
OLED_ShowNum (0, 4, ina226_power, 5, OLED_FONT_SIXTEEN, OLED_LEFT_ROLL,
OLED_SHOW);
// DelayMs (100); // 初始化延时
last_round_time = this_round_time;
this_round_time = GetUpTime_10Ms ();
OLED_ShowNum (4, 7, this_round_time - last_round_time, 3,
OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW);
// last_round_time = this_round_time;
// this_round_time = GetUpTime_10Ms ();
// OLED_ShowNum (4, 7, this_round_time - last_round_time, 3,
// OLED_FONT_EIGHT, OLED_LEFT_ROLL, OLED_SHOW);
// p_json_str = make_sensor_json (ina226_voltage, // 电压
// ina226_current, // 电流
// ina226_power, // 功率
// 0x0CA2, // 温度
// 0x0BF6 // 湿度
// );
// printf ("data:%s",p_json_str); // 打印数据
// OLED_ShowPrintf (0, 6, p_json_str, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
// OLED_SHOW);
packData (ina226_voltage, // 电压
ina226_current, // 电流
ina226_power, // 功率
0x0CA2, // 温度
0x0BF6 // 湿度
);
printf (txBuffer); // 输出到串口
Key_Scan ();
OLED_ShowPrintf (0, 6, p_json_str, OLED_FONT_EIGHT, OLED_LEFT_ROLL,
OLED_SHOW);
// Key_Scan ();
}
}
// 数据打包函数
void
packData (uint16_t vol, uint16_t cur, uint16_t pwr, uint16_t tmp, uint16_t hum)
{
sprintf (txBuffer, "{%04X,%04X,%04X,%04X,%04X}\r\n", vol, cur, pwr, tmp,
hum); // 固定4字符HEX格式
}
void
Uart1_Isr (void) interrupt 4
{