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