This commit is contained in:
2025-01-20 17:25:14 +08:00
parent 30ad59c3a7
commit 992b0e8498
4 changed files with 192 additions and 114 deletions

View File

@ -13,6 +13,7 @@ platform = http://111.198.24.44:11080/v6.5.0.zip
board = esp32-s3-devkitc-1
framework = arduino
lib_deps =
robtillaart/DS18B20@^0.2.4
esphome/ESPAsyncWebServer-esphome@^3.3.0
robtillaart/INA226@^0.6.0
robtillaart/DS18B20@^0.2.4
monitor_speed = 115200

View File

@ -239,7 +239,7 @@ bool BH1750::measurementReady(bool maxWait) {
float BH1750::readLightLevel() {
if (BH1750_MODE == UNCONFIGURED) {
Serial.println(F("[BH1750] Device is not configured!"));
//Serial.println(F("[BH1750] Device is not configured!"));
return -2.0;
}
@ -306,6 +306,7 @@ float getLightLevel(BH1750& sensor, const char* sensorName) {
return lightLevel; // 正常返回光照强度
}
}
//return -2.0;
}
void printLightLevels(BH1750& sensorA, BH1750& sensorB) {

View File

@ -226,7 +226,7 @@ void DS18B20::printTemperature()
//float temperature = getTempC(); // 获取温度(摄氏度)
//Serial.print("Temp: ");
Serial.print(getTempC());
Serial.print("");
Serial.println("");
//Serial.printf("t0.txt=\"%.2f\"\xff\xff\xff", temperature);
//printf("t1.txt=\"%s\"\xff\xff\xff",buf);
//Serial.println("\xff\xff\xff");

View File

@ -1,22 +1,26 @@
#include "DS18B20.h"
#include "BH1750.h"
#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"
MCP45HVX1 digiPot(0x3C);
// MCP45HVX1 数字电位器
MCP45HVX1 digiPot(0x3F);
// INA226 电流监控器
INA226 INA(0x40);
AsyncWebServer server(80);
const char* ssid = "SERVIRST-CT";
const char* password = "servirst8888";
// BH1750
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DS18B20 sensor(&oneWire);
@ -24,6 +28,7 @@ DS18B20 sensor(&oneWire);
BH1750 bh1750_a;
BH1750 bh1750_b;
//陶晶池串口屏
#define TJC Serial1
#define TJC_TX_Pin 42
#define TJC_RX_Pin 41
@ -50,7 +55,42 @@ const char index_html[] PROGMEM = R"rawliteral(
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!');
}
}
setInterval(fetchData, 1000);
</script>
</head>
@ -59,6 +99,14 @@ const char index_html[] PROGMEM = R"rawliteral(
<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";
@ -67,8 +115,8 @@ const char index_html[] PROGMEM = R"rawliteral(
void WebServer_Init(const char* ssid, const char* password) // 初始化Web
// 初始化Web
void WebServer_Init(const char* ssid, const char* password)
{
WiFi.begin(ssid, password); // WiFi
while (WiFi.status() != WL_CONNECTED)
@ -106,15 +154,57 @@ void WebServer_Init(const char* ssid, const char* password) // 初始化Web
request->send(200, "text/plain", lightB);
});
// 配置Wiper接口
server.on("/wiper", HTTP_GET, [](AsyncWebServerRequest *request)
{
String wiper = String(digiPot.readWiper());
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);
});
// 接收设置Wiper的请求
server.on("/setWiper", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("value", true)) {
int wiperValue = request->getParam("value", true)->value().toInt();
digiPot.writeWiper(wiperValue);
request->send(200, "text/plain", "Wiper value set");
} else {
request->send(400, "text/plain", "Missing value parameter");
}
});
server.begin();
Serial.println("Web server started");
//Serial.println("Web server started");
}
//陶晶池串口屏发送
void TJC_Show(void)
{
char str[64];
char str[128];
sprintf(str, "t0.txt=\"%.2f\"\xff\xff\xff", sensor.getTempC());//用sprintf来格式化字符串给t0的txt属性赋值
TJC.print(str); //把字符串发送出去
@ -122,89 +212,53 @@ void TJC_Show(void)
TJC.print(str);
sprintf(str, "t2.txt=\"%.f\"\xff\xff\xff", bh1750_b.readLightLevel());
TJC.print(str);
sprintf(str, "t8.txt=\"%d\"\xff\xff\xff", digiPot.readWiper());
TJC.print(str);
sprintf(str, "t9.txt=\"%.3f\"\xff\xff\xff", INA.getBusVoltage(), 3);
TJC.print(str);
sprintf(str, "t10.txt=\"%.3f\"\xff\xff\xff", INA.getShuntVoltage_mV(), 3);
TJC.print(str);
sprintf(str, "t11.txt=\"%.3f\"\xff\xff\xff", INA.getCurrent_mA(), 3);
TJC.print(str);
sprintf(str, "t12.txt=\"%.3f\"\xff\xff\xff", INA.getPower_mW(), 3);
TJC.print(str);
}
void MCP45HVX1_Test(void) //MCP45HVX1测试
{
digiPot.begin(5,4);
Serial.println("....... Functionality Test Begin ..........");
/* Wiper ........................... */
digiPot.writeWiper(127); // Baseline Establish
Serial.println("\n----- Wiper Register ----");
Serial.print("readWiper: ");
Serial.println(digiPot.readWiper());
Serial.print("writeWiper: ");
digiPot.writeWiper(200);
Serial.println(digiPot.readWiper());
Serial.print("incrementWiper: ");
digiPot.incrementWiper();
Serial.println(digiPot.readWiper());
Serial.print("incrementWiper by 2: ");
digiPot.incrementWiper(2);
Serial.println(digiPot.readWiper());
Serial.print("decrementWiper: ");
digiPot.decrementWiper();
Serial.println(digiPot.readWiper());
Serial.print("decrementWiper by 2: ");
digiPot.decrementWiper(2);
Serial.println(digiPot.readWiper());
/* TCON .......................... */
Serial.println("\n----- TCON Register ----");
digiPot.disconnectTerminalA();
Serial.print("disconnectTerminalA: ");
Serial.println(digiPot.readTCON());
digiPot.connectTerminalA();
digiPot.disconnectTerminalB();
Serial.print("disconnectTerminalB: ");
Serial.println(digiPot.readTCON());
digiPot.connectTerminalB();
digiPot.disconnectWiper();
Serial.print("disconnectWiper: ");
Serial.println(digiPot.readTCON());
digiPot.connectWiper();
digiPot.shutdown();
Serial.print("shutdown: ");
Serial.println(digiPot.readTCON());
digiPot.startup();
digiPot.startup();
Serial.print("startup: ");
Serial.println(digiPot.readTCON());
digiPot.startup();
digiPot.defaultTCON();
Serial.print("default: ");
Serial.println(digiPot.readTCON());
Serial.println("\n........ Functionality Test End ...........");
}
void setup(void)
{
Serial.begin(115200);
TJC.begin(115200, SERIAL_8N1, TJC_RX_Pin, TJC_TX_Pin); //串口屏
// 初始化 I2C 总线
Wire.begin(5, 4); // SDA SCL
// MCP45HVX1 初始化
digiPot.begin(5, 4);
// INA226 初始化
INA.setMaxCurrentShunt(10, 0.002); // 设置最大电流和分流电阻
// 串口屏初始化
TJC.begin(115200, SERIAL_8N1, TJC_RX_Pin, TJC_TX_Pin);
while (TJC.read() >= 0); //因为串口屏开机会发送88 ff ff ff,所以要清空串口缓冲区
TJC.print("page main\xff\xff\xff"); //发送命令让屏幕跳转到main页面
//sensor.begin();
// BH1750 初始化
bh1750_init(bh1750_a, bh1750_b, 36, 35, 21, 20); //1750初始化。sda, scl
// Wire.begin(36,35);
// Wire1.begin(21, 20);
// bh1750_a.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire);
// bh1750_b.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x23, &Wire1);
bh1750_init(bh1750_a, bh1750_b, 19, 18, 21, 20); //1750初始化。sda, scl
//WebServer 初始化
WebServer_Init(ssid, password);
//WebServer_Init(ssid, password);
MCP45HVX1_Test();
}
@ -217,7 +271,29 @@ void loop(void)
bh1750_a.readLightLevel(),
bh1750_b.readLightLevel());
// MCP45HVX1 操作
//digiPot.writeWiper(127); // 随机设置 Wiper 值,random(0, 256)
//delay(500);
Serial.print("Current Wiper Value: ");
Serial.println(digiPot.readWiper());
// INA226 数据读取
Serial.println("\nBUS\tSHUNT\tCURRENT\tPOWER");
Serial.print(INA.getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA.getShuntVoltage_mV(), 3);
Serial.print("\t");
Serial.print(INA.getCurrent_mA(), 3);
Serial.print("\t");
Serial.print(INA.getPower_mW(), 3);
Serial.println();
TJC_Show(); //串口屏
delay(1000);
}