# 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

0
Lib/dht10.c Normal file
View File

0
Lib/dht10.h Normal file
View File

View File

@ -1,101 +1,101 @@
/**********************************************
* 文件名sensor_json.h
* 功能将5个uint16参数封装为紧凑JSON格式
* 特性HEX传输、静态内存、无库依赖
* 设计依据C51硬件架构优化
**********************************************/
// /**********************************************
// * 文件名sensor_json.h
// * 功能将5个uint16参数封装为紧凑JSON格式
// * 特性HEX传输、静态内存、无库依赖
// * 设计依据C51硬件架构优化
// **********************************************/
#ifndef __SENSOR_JSON_H__
#define __SENSOR_JSON_H__
// #ifndef __SENSOR_JSON_H__
// #define __SENSOR_JSON_H__
#include <stdint.h>
// #include <stdint.h>
/* 硬件相关定义 */
#define JSON_BUF_SIZE 57 // 内存预分配避免动态申请
#define HEX_DIGITS "0123456789ABCDEF"
// /* 硬件相关定义 */
// #define JSON_BUF_SIZE 57 // 内存预分配避免动态申请
// #define HEX_DIGITS "0123456789ABCDEF"
/* 静态缓冲区声明 */
static char json_buf[JSON_BUF_SIZE]
= "{\"vol\":----,\"cur\":----,\"pwr\":----,\"tmp\":----,\"hum\":----}";
// /* 静态缓冲区声明 */
// static char json_buf[JSON_BUF_SIZE]
// = "{\"vol\":----,\"cur\":----,\"pwr\":----,\"tmp\":----,\"hum\":----}";
/**********************************************
* 函数名hex4_encode
* 功能快速HEX转换无分支优化
* 输入16位数值、4字节输出缓冲区
* 注:避免浮点运算提升执行效率
**********************************************/
static void
hex4_encode (uint16_t num, char *out)
{
out[0] = HEX_DIGITS[(num >> 12) & 0x0F];
out[1] = HEX_DIGITS[(num >> 8) & 0x0F];
out[2] = HEX_DIGITS[(num >> 4) & 0x0F];
out[3] = HEX_DIGITS[num & 0x0F];
}
// /**********************************************
// * 函数名hex4_encode
// * 功能快速HEX转换无分支优化
// * 输入16位数值、4字节输出缓冲区
// * 注:避免浮点运算提升执行效率
// **********************************************/
// static void
// hex4_encode (uint16_t num, char *out)
// {
// out[0] = HEX_DIGITS[(num >> 12) & 0x0F];
// out[1] = HEX_DIGITS[(num >> 8) & 0x0F];
// out[2] = HEX_DIGITS[(num >> 4) & 0x0F];
// out[3] = HEX_DIGITS[num & 0x0F];
// }
/**********************************************
* 主函数make_sensor_json
* 参数顺序:电压、电流、功率、温度、湿度
* 返回值:静态缓冲区指针(注意不可重入)
* 内存布局优化:固定位置替换减少计算量
**********************************************/
char *
make_sensor_json (uint16_t v, uint16_t c, uint16_t p, uint16_t t, uint16_t h)
{
char tmp[4];
// /**********************************************
// * 主函数make_sensor_json
// * 参数顺序:电压、电流、功率、温度、湿度
// * 返回值:静态缓冲区指针(注意不可重入)
// * 内存布局优化:固定位置替换减少计算量
// **********************************************/
// char *
// make_sensor_json (uint16_t v, uint16_t c, uint16_t p, uint16_t t, uint16_t h)
// {
// char tmp[4];
/* 定点内存操作(避免字符串拼接) */
hex4_encode (v, tmp);
json_buf[7] = tmp[0];
json_buf[8] = tmp[1]; // vol
json_buf[9] = tmp[2];
json_buf[10] = tmp[3];
// /* 定点内存操作(避免字符串拼接) */
// hex4_encode (v, tmp);
// json_buf[7] = tmp[0];
// json_buf[8] = tmp[1]; // vol
// json_buf[9] = tmp[2];
// json_buf[10] = tmp[3];
hex4_encode (c, tmp);
json_buf[18] = tmp[0];
json_buf[19] = tmp[1]; // cur
json_buf[20] = tmp[2];
json_buf[21] = tmp[3];
// hex4_encode (c, tmp);
// json_buf[18] = tmp[0];
// json_buf[19] = tmp[1]; // cur
// json_buf[20] = tmp[2];
// json_buf[21] = tmp[3];
hex4_encode (p, tmp);
json_buf[29] = tmp[0];
json_buf[30] = tmp[1]; // pwr
json_buf[31] = tmp[2];
json_buf[32] = tmp[3];
// hex4_encode (p, tmp);
// json_buf[29] = tmp[0];
// json_buf[30] = tmp[1]; // pwr
// json_buf[31] = tmp[2];
// json_buf[32] = tmp[3];
hex4_encode (t, tmp);
json_buf[40] = tmp[0];
json_buf[41] = tmp[1]; // tmp
json_buf[42] = tmp[2];
json_buf[43] = tmp[3];
// hex4_encode (t, tmp);
// json_buf[40] = tmp[0];
// json_buf[41] = tmp[1]; // tmp
// json_buf[42] = tmp[2];
// json_buf[43] = tmp[3];
hex4_encode (h, tmp);
json_buf[51] = tmp[0];
json_buf[52] = tmp[1]; // hum
json_buf[53] = tmp[2];
json_buf[54] = tmp[3];
// hex4_encode (h, tmp);
// json_buf[51] = tmp[0];
// json_buf[52] = tmp[1]; // hum
// json_buf[53] = tmp[2];
// json_buf[54] = tmp[3];
return json_buf;
}
// return json_buf;
// }
#endif /* __SENSOR_JSON_H__ */
// #endif /* __SENSOR_JSON_H__ */
/**********************************************
* 使用示例:
* #include "sensor_json.h"
*
* void main() {
* char* data = make_sensor_json(
* 0x0D0C, // 电压
* 0x047E, // 电流
* 0x1284, // 功率
* 0x0CA2, // 温度
* 0x0BF6 // 湿度
* );
* // 通过P1口输出需按 配置端口模式)
* for(uint8_t i=0; data[i]; i++) {
* P1 = data[i]; // 假设P1接发送模块
* delay_ms(1);
* }
* }
**********************************************/
// /**********************************************
// * 使用示例:
// * #include "sensor_json.h"
// *
// * void main() {
// * char* data = make_sensor_json(
// * 0x0D0C, // 电压
// * 0x047E, // 电流
// * 0x1284, // 功率
// * 0x0CA2, // 温度
// * 0x0BF6 // 湿度
// * );
// * // 通过P1口输出需按 配置端口模式)
// * for(uint8_t i=0; data[i]; i++) {
// * P1 = data[i]; // 假设P1接发送模块
// * delay_ms(1);
// * }
// * }
// **********************************************/

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
{