This commit is contained in:
2025-05-13 16:55:49 +08:00
parent 1c2201afc9
commit bf5270f337
16 changed files with 463 additions and 384 deletions

View File

@ -4,7 +4,7 @@
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Arduino.h>
#include "MCP45HVX1.h"
#include "INA226.h"
#include <Ticker.h>
#include <Preferences.h> // 用于存储非易失性数据
@ -12,17 +12,15 @@
#include "RunTime.h"
#include "WebServer.h"
#include "WiFiControl.h"
#include "DigitalPot.h"
#include "DS3502.h"
#include "LightControl.h"
// Ticker 用于定时检查电流状态
Ticker currentCheckTicker;
// MCP45HVX1 数字电位器
MCP45HVX1 mcp45hvx1(0x3C);
//TPL0401A 数字电位器
TPL0401A digiPot(0x2E);
// DS3502 数字电位器
DS3502 ds3502(0x28);
// INA226 电流监控器
INA226 INA(0x40);
@ -38,6 +36,9 @@ DS18B20 ds18b20(&oneWire);
BH1750 bh1750_a;
BH1750 bh1750_b;
// 按键引脚
#define INCREASE_BUTTON_PIN 3
#define DECREASE_BUTTON_PIN 46
// 风扇控制引脚
#define FAN_PIN1 15
@ -50,6 +51,12 @@ extern RunTime runtime;
Preferences prefs;
TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, ds3502, INA);
// 比例调节bh1750_a
LightControl lightCtrl(bh1750_a, ds3502);
bool autoAdjustEnabled = false; // 是否启用自动调节,默认关闭
float targetLightValue = 2000; // 默认目标照度
void WebServer_Init(void) {
// 根路径的页面
@ -77,7 +84,7 @@ void WebServer_Init(void) {
// 配置Wiper接口
server.on("/wiper", HTTP_GET, [](AsyncWebServerRequest *request) {
String wiper = String(digiPot.readWiperValue());
String wiper = String(ds3502.getWiper());
request->send(200, "text/plain", wiper);
});
@ -116,7 +123,7 @@ void WebServer_Init(void) {
server.on("/setWiper", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("value", true)) {
int wiperValue = request->getParam("value", true)->value().toInt();
digiPot.writeWiper(wiperValue);
ds3502.setWiper(wiperValue);
request->send(200, "text/plain", "Wiper value set");
} else {
request->send(400, "text/plain", "Missing value parameter");
@ -129,7 +136,7 @@ void WebServer_Init(void) {
// 风扇控制
void FansControl(void) {
if (ds18b20.getTempC() > 25) {
if (ds18b20.getTempC() > 30) {
digitalWrite(FAN_PIN1, HIGH);
digitalWrite(FAN_PIN2, HIGH);
} else {
@ -140,7 +147,7 @@ void FansControl(void) {
// 蜂鸣器
void Buzzer(void) {
if (ds18b20.getTempC() > 20) {
if (ds18b20.getTempC() > 45) {
digitalWrite(BUZZER_PIN, HIGH);
//tone(BUZZER_PIN, 1000, 1000);
} else {
@ -149,8 +156,33 @@ void Buzzer(void) {
}
}
//按键控制
void ButtonControl(void) {
// 按键检测
static int lastWiperValue = ds3502.getWiper();
static const int step = 5; // 步进值
if (digitalRead(INCREASE_BUTTON_PIN) == LOW) {
int currentWiperValue = ds3502.getWiper();
if (currentWiperValue < 127) {
ds3502.setWiper(currentWiperValue + step);
lastWiperValue = currentWiperValue + step;
Serial.printf("Increased Wiper Value: %d\n", lastWiperValue);
}
delay(200); // 防抖动
}
if (digitalRead(DECREASE_BUTTON_PIN) == LOW) {
int currentWiperValue = ds3502.getWiper();
if (currentWiperValue > 0) {
ds3502.setWiper(currentWiperValue - step);
lastWiperValue = currentWiperValue - step;
Serial.printf("Decreased Wiper Value: %d\n", lastWiperValue);
}
delay(200); // 防抖动
}
}
TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, digiPot, INA);
void setup(void)
@ -160,7 +192,7 @@ void setup(void)
// 初始化 I2C 总线
Wire.begin(5, 4); // SDA SCL
mcp45hvx1.begin(5, 4);
ds3502.begin(5, 4);
// INA226 初始化
INA.setMaxCurrentShunt(10.0, 0.005); // 设置最大电流和分流电阻
@ -192,7 +224,18 @@ void setup(void)
tjcShow.init();
tjcShow.showQR();
digiPot.writeWiper(30);
ds3502.setWiper(2);
delay(300);
Serial.printf("pot: %d\n", ds3502.getWiper());
pinMode(FAN_PIN1, OUTPUT);
pinMode(FAN_PIN2, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// 初始化按键引脚
pinMode(INCREASE_BUTTON_PIN, INPUT_PULLUP);
pinMode(DECREASE_BUTTON_PIN, INPUT_PULLUP);
}
@ -205,11 +248,11 @@ void loop(void)
bh1750_a.readLightLevel(),
bh1750_b.readLightLevel());
// MCP45HVX1 操作
//digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256)
mcp45hvx1.writeWiper(random(0, 127));
Serial.print("Current Wiper Value: ");
Serial.println(mcp45hvx1.readWiper());
// // MCP45HVX1 操作
// //digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256)
// ds3502.setWiper(random(0, 127));
// Serial.print("Current Wiper Value: ");
// Serial.println(ds3502.getWiper());
// INA226 数据读取
@ -218,9 +261,9 @@ void loop(void)
Serial.print("\t");
Serial.print(INA.getShuntVoltage_mV(), 3); //分流电压
Serial.print("\t");
Serial.print(INA.getCurrent_mA(), 3); //通过分流器的电流
Serial.print(INA.getCurrent(), 3); //通过分流器的电流
Serial.print("\t");
Serial.print(INA.getPower_mW(), 3); //Current x BusVoltage
Serial.print(INA.getPower(), 3); //Current x BusVoltage
Serial.println();
@ -229,6 +272,12 @@ void loop(void)
FansControl();
Buzzer();
ButtonControl();
if (autoAdjustEnabled) {
lightCtrl.adjustWiper(); // 比例调节
}
delay(1000);
}