加入nvs

This commit is contained in:
2025-01-23 16:36:27 +08:00
parent eec83b3539
commit 994cc56f3e
2 changed files with 23 additions and 10 deletions

View File

@ -29,7 +29,7 @@ class MCP45HVX1
}TCON_Register; }TCON_Register;
/* Setup ............................................................... */ /* Setup ............................................................... */
MCP45HVX1(uint8_t address = 0x3C); MCP45HVX1(uint8_t address = 0x3e);
void begin(int sda = -1, int scl = -1, TwoWire &inWire = Wire); void begin(int sda = -1, int scl = -1, TwoWire &inWire = Wire);

View File

@ -9,8 +9,11 @@
#include "MCP45HVX1.h" #include "MCP45HVX1.h"
#include "INA226.h" #include "INA226.h"
#include <Ticker.h> #include <Ticker.h>
#include <Preferences.h> // 用于存储非易失性数据
// 灯泡计时器相关变量 Preferences preferences;
// 灯泡计时器
unsigned long activeDuration = 0; // 累计使用时长(秒) unsigned long activeDuration = 0; // 累计使用时长(秒)
unsigned long lastMillis = 0; // 上次更新计时时间 unsigned long lastMillis = 0; // 上次更新计时时间
bool isActive = false; // 是否正在计时 bool isActive = false; // 是否正在计时
@ -19,7 +22,7 @@ bool isActive = false; // 是否正在计时
Ticker currentCheckTicker; Ticker currentCheckTicker;
// MCP45HVX1 数字电位器 // MCP45HVX1 数字电位器
MCP45HVX1 digiPot(0x3F); MCP45HVX1 digiPot(0x3E);
// INA226 电流监控器 // INA226 电流监控器
INA226 INA(0x40); INA226 INA(0x40);
@ -142,12 +145,13 @@ const char index_html[] PROGMEM = R"rawliteral(
// 清空累计使用时长 // 清空累计使用时长
void resetActiveDuration() { void resetActiveDuration() {
activeDuration = 0; activeDuration = 0;
preferences.putULong("activeDuration", activeDuration); // 保存到 NVS
} }
// 检查电流并更新计时状态 // 检查电流并更新计时状态
void checkCurrent() { void checkCurrent() {
float current = INA.getCurrent_mA(); float current = INA.getCurrent_mA();
if (current > 1000.0) { if (current > 100.0) {
if (!isActive) { if (!isActive) {
// 开始计时 // 开始计时
isActive = true; isActive = true;
@ -158,6 +162,7 @@ void checkCurrent() {
// 停止计时并累加 // 停止计时并累加
activeDuration += (millis() - lastMillis) / 1000; activeDuration += (millis() - lastMillis) / 1000;
isActive = false; isActive = false;
preferences.putULong("activeDuration", activeDuration); // 保存到 NVS
} }
} }
@ -165,6 +170,7 @@ void checkCurrent() {
if (isActive) { if (isActive) {
activeDuration += (millis() - lastMillis) / 1000; activeDuration += (millis() - lastMillis) / 1000;
lastMillis = millis(); lastMillis = millis();
preferences.putULong("activeDuration", activeDuration); // 保存到 NVS
} }
} }
@ -172,7 +178,7 @@ void checkCurrent() {
String formatDuration(unsigned long seconds) { String formatDuration(unsigned long seconds) {
unsigned long hours = seconds / 3600; unsigned long hours = seconds / 3600;
unsigned long minutes = (seconds % 3600) / 60; unsigned long minutes = (seconds % 3600) / 60;
return String(hours) + " 小时 " + String(minutes) + " 分钟"; return String(hours) + " : " + String(minutes);
} }
// 初始化Web // 初始化Web
@ -294,6 +300,9 @@ void TJC_Show(void)
sprintf(str, "t2.txt=\"%.f\"\xff\xff\xff", bh1750_b.readLightLevel()); sprintf(str, "t2.txt=\"%.f\"\xff\xff\xff", bh1750_b.readLightLevel());
TJC.print(str); TJC.print(str);
sprintf(str, "t14.txt=\"%s\"\xff\xff\xff", formatDuration(activeDuration));
TJC.print(str);
sprintf(str, "t8.txt=\"%d\"\xff\xff\xff", digiPot.readWiper()); sprintf(str, "t8.txt=\"%d\"\xff\xff\xff", digiPot.readWiper());
TJC.print(str); TJC.print(str);
@ -313,6 +322,13 @@ void setup(void)
{ {
Serial.begin(115200); Serial.begin(115200);
// 初始化 NVS 存储
preferences.begin("timing", false);
activeDuration = preferences.getULong("activeDuration", 0); // 从 NVS 加载数据
// 启动定时器,每秒检查电流状态
currentCheckTicker.attach(1, checkCurrent);
// 初始化 I2C 总线 // 初始化 I2C 总线
Wire.begin(5, 4); // SDA SCL Wire.begin(5, 4); // SDA SCL
@ -320,22 +336,19 @@ void setup(void)
digiPot.begin(5, 4); digiPot.begin(5, 4);
// INA226 初始化 // INA226 初始化
INA.setMaxCurrentShunt(10, 0.002); // 设置最大电流和分流电阻 INA.setMaxCurrentShunt(10, 0.005); // 设置最大电流和分流电阻
//sensor.begin(); //sensor.begin();
// BH1750 初始化 // BH1750 初始化
//bh1750_init(bh1750_a, bh1750_b, 36, 35, 21, 20); //1750初始化。sda, scl //bh1750_init(bh1750_a, bh1750_b, 36, 35, 21, 20); //1750初始化。sda, scl
//Wire.begin(36,35); //Wire.begin(36,35);
Wire1.begin(21, 20); Wire1.begin(36, 35);
bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire); bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1); bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1);
//WebServer 初始化 //WebServer 初始化
WebServer_Init(ssid, password); WebServer_Init(ssid, password);
// 启动定时器,每秒检查电流状态
currentCheckTicker.attach(1, checkCurrent);
// 串口屏初始化 // 串口屏初始化
TJC.begin(115200, SERIAL_8N1, TJC_RX_Pin, TJC_TX_Pin); TJC.begin(115200, SERIAL_8N1, TJC_RX_Pin, TJC_TX_Pin);
while (TJC.read() >= 0); //因为串口屏开机会发送88 ff ff ff,所以要清空串口缓冲区 while (TJC.read() >= 0); //因为串口屏开机会发送88 ff ff ff,所以要清空串口缓冲区