2025-01-14 13:46:57 +08:00
|
|
|
|
#include "DS18B20.h"
|
|
|
|
|
|
#include "BH1750.h"
|
|
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
|
#include <ESPAsyncWebServer.h>
|
2025-01-20 17:25:14 +08:00
|
|
|
|
#include <Arduino.h>
|
2025-05-13 16:55:49 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
#include "INA226.h"
|
2025-01-21 17:31:53 +08:00
|
|
|
|
#include <Ticker.h>
|
2025-01-23 16:36:27 +08:00
|
|
|
|
#include <Preferences.h> // 用于存储非易失性数据
|
2025-02-08 16:24:54 +08:00
|
|
|
|
#include "TJC_Show.h"
|
|
|
|
|
|
#include "RunTime.h"
|
2025-02-12 11:38:33 +08:00
|
|
|
|
#include "WebServer.h"
|
|
|
|
|
|
#include "WiFiControl.h"
|
2025-05-13 16:55:49 +08:00
|
|
|
|
#include "DS3502.h"
|
|
|
|
|
|
#include "LightControl.h"
|
2025-02-07 15:02:44 +08:00
|
|
|
|
|
2025-01-21 17:31:53 +08:00
|
|
|
|
|
|
|
|
|
|
// Ticker 用于定时检查电流状态
|
|
|
|
|
|
Ticker currentCheckTicker;
|
2025-01-20 17:25:14 +08:00
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
// DS3502 数字电位器
|
|
|
|
|
|
DS3502 ds3502(0x28);
|
2025-01-15 16:52:01 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// INA226 电流监控器
|
2025-02-08 16:24:54 +08:00
|
|
|
|
INA226 INA(0x40);
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-21 17:31:53 +08:00
|
|
|
|
// DS18B20 温度传感器
|
2025-02-12 11:38:33 +08:00
|
|
|
|
#define DS18B20_PIN 8
|
|
|
|
|
|
OneWire oneWire(DS18B20_PIN);
|
2025-02-08 16:24:54 +08:00
|
|
|
|
DS18B20 ds18b20(&oneWire);
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
BH1750 bh1750_a;
|
|
|
|
|
|
BH1750 bh1750_b;
|
|
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
// 按键引脚
|
|
|
|
|
|
#define INCREASE_BUTTON_PIN 3
|
|
|
|
|
|
#define DECREASE_BUTTON_PIN 46
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
2025-02-07 15:02:44 +08:00
|
|
|
|
// 风扇控制引脚
|
|
|
|
|
|
#define FAN_PIN1 15
|
|
|
|
|
|
#define FAN_PIN2 16
|
|
|
|
|
|
|
|
|
|
|
|
// 蜂鸣器引脚
|
|
|
|
|
|
#define BUZZER_PIN 17
|
|
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
extern RunTime runtime;
|
|
|
|
|
|
|
|
|
|
|
|
Preferences prefs;
|
2025-02-07 15:02:44 +08:00
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, ds3502, INA);
|
|
|
|
|
|
|
|
|
|
|
|
// 比例调节,bh1750_a
|
|
|
|
|
|
LightControl lightCtrl(bh1750_a, ds3502);
|
|
|
|
|
|
bool autoAdjustEnabled = false; // 是否启用自动调节,默认关闭
|
|
|
|
|
|
float targetLightValue = 2000; // 默认目标照度
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
void WebServer_Init(void) {
|
2025-01-14 13:46:57 +08:00
|
|
|
|
// 根路径的页面
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-14 13:46:57 +08:00
|
|
|
|
request->send_P(200, "text/html", index_html);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 温度接口
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-02-08 16:24:54 +08:00
|
|
|
|
String temp = String(ds18b20.getTempC());
|
2025-01-14 13:46:57 +08:00
|
|
|
|
request->send(200, "text/plain", temp);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 光照强度(传感器A)
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/lightA", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-14 13:46:57 +08:00
|
|
|
|
String lightA = String(bh1750_a.readLightLevel());
|
|
|
|
|
|
request->send(200, "text/plain", lightA);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 光照(传感器B)
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/lightB", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-14 13:46:57 +08:00
|
|
|
|
String lightB = String(bh1750_b.readLightLevel());
|
|
|
|
|
|
request->send(200, "text/plain", lightB);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// 配置Wiper接口
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/wiper", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-05-13 16:55:49 +08:00
|
|
|
|
String wiper = String(ds3502.getWiper());
|
2025-01-20 17:25:14 +08:00
|
|
|
|
request->send(200, "text/plain", wiper);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/busVoltage", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-20 17:25:14 +08:00
|
|
|
|
String busVoltage = String(INA.getBusVoltage(), 3);
|
|
|
|
|
|
request->send(200, "text/plain", busVoltage);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/shuntVoltage", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-20 17:25:14 +08:00
|
|
|
|
String shuntVoltage = String(INA.getShuntVoltage_mV(), 3);
|
|
|
|
|
|
request->send(200, "text/plain", shuntVoltage);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/current", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-20 17:25:14 +08:00
|
|
|
|
String current = String(INA.getCurrent_mA(), 3);
|
|
|
|
|
|
request->send(200, "text/plain", current);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
server.on("/power", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-01-20 17:25:14 +08:00
|
|
|
|
String power = String(INA.getPower_mW(), 3);
|
|
|
|
|
|
request->send(200, "text/plain", power);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-21 17:31:53 +08:00
|
|
|
|
// 获取累计时长接口
|
|
|
|
|
|
server.on("/duration", HTTP_GET, [](AsyncWebServerRequest *request) {
|
2025-02-08 16:24:54 +08:00
|
|
|
|
request->send(200, "text/plain", runtime.formatDuration(runtime.getActiveDuration()));
|
2025-01-21 17:31:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 清空累计时长接口
|
|
|
|
|
|
server.on("/resetDuration", HTTP_POST, [](AsyncWebServerRequest *request) {
|
2025-02-08 16:24:54 +08:00
|
|
|
|
runtime.resetActiveDuration();
|
2025-01-21 17:31:53 +08:00
|
|
|
|
request->send(200, "text/plain", "Duration reset");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// 接收设置Wiper的请求
|
|
|
|
|
|
server.on("/setWiper", HTTP_POST, [](AsyncWebServerRequest *request) {
|
|
|
|
|
|
if (request->hasParam("value", true)) {
|
|
|
|
|
|
int wiperValue = request->getParam("value", true)->value().toInt();
|
2025-05-13 16:55:49 +08:00
|
|
|
|
ds3502.setWiper(wiperValue);
|
2025-01-20 17:25:14 +08:00
|
|
|
|
request->send(200, "text/plain", "Wiper value set");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
request->send(400, "text/plain", "Missing value parameter");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-14 13:46:57 +08:00
|
|
|
|
server.begin();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-13 17:34:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-07 15:02:44 +08:00
|
|
|
|
// 风扇控制
|
|
|
|
|
|
void FansControl(void) {
|
2025-05-13 16:55:49 +08:00
|
|
|
|
if (ds18b20.getTempC() > 30) {
|
2025-02-07 15:02:44 +08:00
|
|
|
|
digitalWrite(FAN_PIN1, HIGH);
|
|
|
|
|
|
digitalWrite(FAN_PIN2, HIGH);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
digitalWrite(FAN_PIN1, LOW);
|
|
|
|
|
|
digitalWrite(FAN_PIN2, LOW);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
// 蜂鸣器
|
2025-02-07 15:02:44 +08:00
|
|
|
|
void Buzzer(void) {
|
2025-05-13 16:55:49 +08:00
|
|
|
|
if (ds18b20.getTempC() > 45) {
|
2025-02-08 16:24:54 +08:00
|
|
|
|
digitalWrite(BUZZER_PIN, HIGH);
|
|
|
|
|
|
//tone(BUZZER_PIN, 1000, 1000);
|
2025-02-07 15:02:44 +08:00
|
|
|
|
} else {
|
2025-02-08 16:24:54 +08:00
|
|
|
|
digitalWrite(BUZZER_PIN, LOW);
|
|
|
|
|
|
//noTone(BUZZER_PIN);
|
2025-02-07 15:02:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
//按键控制
|
|
|
|
|
|
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); // 防抖动
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-07 15:02:44 +08:00
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
|
2025-01-14 13:46:57 +08:00
|
|
|
|
void setup(void)
|
|
|
|
|
|
{
|
2025-02-08 16:24:54 +08:00
|
|
|
|
Serial.begin(115200);
|
2025-01-23 16:36:27 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// 初始化 I2C 总线
|
|
|
|
|
|
Wire.begin(5, 4); // SDA SCL
|
|
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
ds3502.begin(5, 4);
|
2025-02-12 11:38:33 +08:00
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
// INA226 初始化
|
|
|
|
|
|
INA.setMaxCurrentShunt(10.0, 0.005); // 设置最大电流和分流电阻
|
|
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// MCP45HVX1 初始化
|
2025-02-12 11:38:33 +08:00
|
|
|
|
//digiPot.begin(5, 4);
|
2025-01-20 17:25:14 +08:00
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
// 启动定时器,每秒检查电流状态
|
|
|
|
|
|
currentCheckTicker.attach(1, []() { runtime.checkCurrent(); });
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
//sensor.begin();
|
2025-01-20 17:25:14 +08:00
|
|
|
|
// BH1750 初始化
|
2025-01-21 17:31:53 +08:00
|
|
|
|
//bh1750_init(bh1750_a, bh1750_b, 36, 35, 21, 20); //1750初始化。sda, scl
|
|
|
|
|
|
//Wire.begin(36,35);
|
2025-01-23 16:36:27 +08:00
|
|
|
|
Wire1.begin(36, 35);
|
2025-01-21 17:31:53 +08:00
|
|
|
|
bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
|
|
|
|
|
|
bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1);
|
2025-02-12 11:38:33 +08:00
|
|
|
|
|
|
|
|
|
|
//WiFi 初始化
|
|
|
|
|
|
WiFi_Init();
|
2025-01-15 16:52:01 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
//WebServer 初始化
|
2025-02-12 11:38:33 +08:00
|
|
|
|
WebServer_Init();
|
2025-01-21 17:31:53 +08:00
|
|
|
|
|
2025-02-08 16:24:54 +08:00
|
|
|
|
// 初始化 runtime NVS
|
|
|
|
|
|
runtime.begin();
|
|
|
|
|
|
|
2025-01-21 17:31:53 +08:00
|
|
|
|
// 串口屏初始化
|
2025-02-12 11:38:33 +08:00
|
|
|
|
tjcShow.init();
|
2025-02-08 16:24:54 +08:00
|
|
|
|
tjcShow.showQR();
|
2025-02-12 11:38:33 +08:00
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-01-14 13:46:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void loop(void)
|
2025-02-12 11:38:33 +08:00
|
|
|
|
{
|
2025-02-08 16:24:54 +08:00
|
|
|
|
ds18b20.printTemperature(); //ds18b20温度
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
//printLightLevels(bh1750_a, bh1750_b); //bh1750照度
|
|
|
|
|
|
Serial.printf("A: %.0f lux :: B: %.0f lux \n",
|
|
|
|
|
|
bh1750_a.readLightLevel(),
|
|
|
|
|
|
bh1750_b.readLightLevel());
|
|
|
|
|
|
|
2025-05-13 16:55:49 +08:00
|
|
|
|
// // MCP45HVX1 操作
|
|
|
|
|
|
// //digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256)
|
|
|
|
|
|
// ds3502.setWiper(random(0, 127));
|
|
|
|
|
|
// Serial.print("Current Wiper Value: ");
|
|
|
|
|
|
// Serial.println(ds3502.getWiper());
|
2025-02-12 11:38:33 +08:00
|
|
|
|
|
2025-01-20 17:25:14 +08:00
|
|
|
|
|
|
|
|
|
|
// INA226 数据读取
|
2025-01-21 17:31:53 +08:00
|
|
|
|
Serial.println("\nBUS\tSHUNT\tCURRENT\tPOWER");
|
2025-02-07 15:23:30 +08:00
|
|
|
|
Serial.print(INA.getBusVoltage(), 3); //总线电压 MAX 36V
|
2025-01-20 17:25:14 +08:00
|
|
|
|
Serial.print("\t");
|
2025-02-07 15:23:30 +08:00
|
|
|
|
Serial.print(INA.getShuntVoltage_mV(), 3); //分流电压
|
2025-01-20 17:25:14 +08:00
|
|
|
|
Serial.print("\t");
|
2025-05-13 16:55:49 +08:00
|
|
|
|
Serial.print(INA.getCurrent(), 3); //通过分流器的电流
|
2025-01-20 17:25:14 +08:00
|
|
|
|
Serial.print("\t");
|
2025-05-13 16:55:49 +08:00
|
|
|
|
Serial.print(INA.getPower(), 3); //Current x BusVoltage
|
2025-01-20 17:25:14 +08:00
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 11:38:33 +08:00
|
|
|
|
tjcShow.showInfo(); //串口屏
|
2025-02-08 16:24:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FansControl();
|
|
|
|
|
|
Buzzer();
|
2025-05-13 16:55:49 +08:00
|
|
|
|
ButtonControl();
|
|
|
|
|
|
|
|
|
|
|
|
if (autoAdjustEnabled) {
|
|
|
|
|
|
lightCtrl.adjustWiper(); // 比例调节
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
delay(1000);
|
2025-01-20 17:25:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|