tpl0401a
This commit is contained in:
181
src/main.cpp
181
src/main.cpp
@ -3,8 +3,6 @@
|
||||
#include "Wire.h"
|
||||
#include <WiFi.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
//#include "TCJ_Show.h"
|
||||
//#include "ESP32_WebServer.h"
|
||||
#include <Arduino.h>
|
||||
#include "MCP45HVX1.h"
|
||||
#include "INA226.h"
|
||||
@ -12,26 +10,29 @@
|
||||
#include <Preferences.h> // 用于存储非易失性数据
|
||||
#include "TJC_Show.h"
|
||||
#include "RunTime.h"
|
||||
|
||||
#include "WebServer.h"
|
||||
#include "WiFiControl.h"
|
||||
#include "DigitalPot.h"
|
||||
|
||||
|
||||
// Ticker 用于定时检查电流状态
|
||||
Ticker currentCheckTicker;
|
||||
|
||||
// MCP45HVX1 数字电位器
|
||||
MCP45HVX1 digiPot(0x3E);
|
||||
MCP45HVX1 mcp45hvx1(0x3C);
|
||||
|
||||
//TPL0401A 数字电位器
|
||||
TPL0401A digiPot(0x2E);
|
||||
|
||||
// INA226 电流监控器
|
||||
INA226 INA(0x40);
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
const char* ssid = "SERVIRST-CT";
|
||||
const char* password = "servirst8888";
|
||||
|
||||
// DS18B20 温度传感器
|
||||
#define ONE_WIRE_BUS 8
|
||||
OneWire oneWire(ONE_WIRE_BUS);
|
||||
#define DS18B20_PIN 8
|
||||
OneWire oneWire(DS18B20_PIN);
|
||||
DS18B20 ds18b20(&oneWire);
|
||||
|
||||
BH1750 bh1750_a;
|
||||
@ -46,171 +47,56 @@ BH1750 bh1750_b;
|
||||
#define BUZZER_PIN 17
|
||||
|
||||
extern RunTime runtime;
|
||||
int activeDuration;
|
||||
|
||||
Preferences prefs;
|
||||
|
||||
// HTML 页面内容
|
||||
const char index_html[] PROGMEM = R"rawliteral(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>ESP32 Data Display</title>
|
||||
<meta charset="UTF-8">
|
||||
<script>
|
||||
async function fetchData()
|
||||
{
|
||||
const durationResponse = await fetch('/duration');
|
||||
const durationText = await durationResponse.text();
|
||||
document.getElementById('activeDuration').innerText = durationText;
|
||||
|
||||
const tempResponse = await fetch('/temperature');
|
||||
const tempText = await tempResponse.text();
|
||||
document.getElementById('temperature').innerText = tempText;
|
||||
|
||||
const lightAResponse = await fetch('/lightA');
|
||||
const lightAText = await lightAResponse.text();
|
||||
document.getElementById('lightA').innerText = lightAText;
|
||||
|
||||
const lightBResponse = await fetch('/lightB');
|
||||
const lightBText = await lightBResponse.text();
|
||||
document.getElementById('lightB').innerText = lightBText;
|
||||
|
||||
const wiperResponse = await fetch('/wiper');
|
||||
const wiperText = await wiperResponse.text();
|
||||
document.getElementById('wiper').innerText = wiperText;
|
||||
|
||||
const busResponse = await fetch('/busVoltage');
|
||||
const busText = await busResponse.text();
|
||||
document.getElementById('busVoltage').innerText = busText;
|
||||
|
||||
const shuntResponse = await fetch('/shuntVoltage');
|
||||
const shuntText = await shuntResponse.text();
|
||||
document.getElementById('shuntVoltage').innerText = shuntText;
|
||||
|
||||
const currentResponse = await fetch('/current');
|
||||
const currentText = await currentResponse.text();
|
||||
document.getElementById('current').innerText = currentText;
|
||||
|
||||
const powerResponse = await fetch('/power');
|
||||
const powerText = await powerResponse.text();
|
||||
document.getElementById('power').innerText = powerText;
|
||||
}
|
||||
|
||||
async function setWiper()
|
||||
{
|
||||
const wiperValue = document.getElementById('wiperValue').value;
|
||||
const response = await fetch('/setWiper', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: 'value=' + encodeURIComponent(wiperValue)
|
||||
});
|
||||
if (response.ok) {
|
||||
alert('Wiper value set successfully!');
|
||||
} else {
|
||||
alert('Failed to set Wiper value!');
|
||||
}
|
||||
}
|
||||
|
||||
async function resetDuration()
|
||||
{
|
||||
const response = await fetch('/resetDuration', { method: 'POST' });
|
||||
if (response.ok) {
|
||||
alert('累计使用时长已清空');
|
||||
} else {
|
||||
alert('清空失败');
|
||||
}
|
||||
}
|
||||
|
||||
setInterval(fetchData, 1000);
|
||||
</script>
|
||||
</head>
|
||||
<body onload="fetchData()">
|
||||
<h1>实时数据显示</h1>
|
||||
<p>累计使用时长: <span id="activeDuration">加载中...</span></p>
|
||||
<button onclick="resetDuration()">清空累计时长</button>
|
||||
<p>温度: <span id="temperature">加载中...</span> °C</p>
|
||||
<p>光照强度(A): <span id="lightA">加载中...</span> lx</p>
|
||||
<p>光照强度(B): <span id="lightB">加载中...</span> lx</p>
|
||||
<p>电位器Wiper值: <span id="wiper">加载中...</span></p>
|
||||
<p>总线电压: <span id="busVoltage">加载中...</span> V</p>
|
||||
<p>分流电压: <span id="shuntVoltage">加载中...</span> mV</p>
|
||||
<p>电流: <span id="current">加载中...</span> mA</p>
|
||||
<p>功率: <span id="power">加载中...</span> mW</p>
|
||||
<h2>设置Wiper值</h2>
|
||||
<input type="number" id="wiperValue" placeholder="输入Wiper值" />
|
||||
<button onclick="setWiper()">设置</button>
|
||||
</body>
|
||||
</html>
|
||||
)rawliteral";
|
||||
|
||||
|
||||
// 初始化Web
|
||||
void WebServer_Init(const char* ssid, const char* password)
|
||||
{
|
||||
WiFi.begin(ssid, password); // WiFi
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(1000);
|
||||
Serial.println("Connecting to WiFi...");
|
||||
}
|
||||
Serial.print("Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
void WebServer_Init(void) {
|
||||
// 根路径的页面
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send_P(200, "text/html", index_html);
|
||||
});
|
||||
|
||||
// 温度接口
|
||||
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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(digiPot.readWiper());
|
||||
server.on("/wiper", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
String wiper = String(digiPot.readWiperValue());
|
||||
request->send(200, "text/plain", wiper);
|
||||
});
|
||||
|
||||
server.on("/busVoltage", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
server.on("/power", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
String power = String(INA.getPower_mW(), 3);
|
||||
request->send(200, "text/plain", power);
|
||||
});
|
||||
@ -238,7 +124,6 @@ void WebServer_Init(const char* ssid, const char* password)
|
||||
});
|
||||
|
||||
server.begin();
|
||||
//Serial.println("Web server started");
|
||||
}
|
||||
|
||||
|
||||
@ -265,7 +150,7 @@ void Buzzer(void) {
|
||||
}
|
||||
|
||||
|
||||
TJC_Show tjcShow(TJC, ds18b20, bh1750_a, bh1750_b, digiPot, INA);
|
||||
TJC_Show tjcShow(ds18b20, bh1750_a, bh1750_b, digiPot, INA);
|
||||
|
||||
|
||||
void setup(void)
|
||||
@ -275,14 +160,15 @@ void setup(void)
|
||||
// 初始化 I2C 总线
|
||||
Wire.begin(5, 4); // SDA SCL
|
||||
|
||||
mcp45hvx1.begin(5, 4);
|
||||
|
||||
// INA226 初始化
|
||||
INA.setMaxCurrentShunt(10.0, 0.005); // 设置最大电流和分流电阻
|
||||
|
||||
// MCP45HVX1 初始化
|
||||
digiPot.begin(5, 4);
|
||||
//digiPot.begin(5, 4);
|
||||
|
||||
// 启动定时器,每秒检查电流状态
|
||||
//currentCheckTicker.attach(1, checkCurrent);
|
||||
currentCheckTicker.attach(1, []() { runtime.checkCurrent(); });
|
||||
|
||||
//sensor.begin();
|
||||
@ -292,22 +178,26 @@ void setup(void)
|
||||
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(ssid, password);
|
||||
WebServer_Init();
|
||||
|
||||
// 初始化 runtime NVS
|
||||
runtime.begin();
|
||||
|
||||
// 串口屏初始化
|
||||
tjcShow.TJC_Show_Init();
|
||||
|
||||
tjcShow.init();
|
||||
tjcShow.showQR();
|
||||
|
||||
digiPot.writeWiper(30);
|
||||
}
|
||||
|
||||
|
||||
void loop(void)
|
||||
{// put your main code here, to run repeatedly:
|
||||
{
|
||||
ds18b20.printTemperature(); //ds18b20温度
|
||||
|
||||
//printLightLevels(bh1750_a, bh1750_b); //bh1750照度
|
||||
@ -317,9 +207,10 @@ void loop(void)
|
||||
|
||||
// MCP45HVX1 操作
|
||||
//digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256)
|
||||
|
||||
mcp45hvx1.writeWiper(random(0, 127));
|
||||
Serial.print("Current Wiper Value: ");
|
||||
Serial.println(digiPot.readWiper());
|
||||
Serial.println(mcp45hvx1.readWiper());
|
||||
|
||||
|
||||
// INA226 数据读取
|
||||
Serial.println("\nBUS\tSHUNT\tCURRENT\tPOWER");
|
||||
@ -333,7 +224,7 @@ void loop(void)
|
||||
Serial.println();
|
||||
|
||||
|
||||
tjcShow.showINFO(activeDuration); //串口屏
|
||||
tjcShow.showInfo(); //串口屏
|
||||
|
||||
|
||||
FansControl();
|
||||
|
||||
Reference in New Issue
Block a user