#include "DS18B20.h" #include "BH1750.h" #include "Wire.h" #include #include //#include "TCJ_Show.h" //#include "ESP32_WebServer.h" #include #include "MCP45HVX1.h" #include "INA226.h" // 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); BH1750 bh1750_a; BH1750 bh1750_b; //陶晶池串口屏 #define TJC Serial1 #define TJC_TX_Pin 42 #define TJC_RX_Pin 41 // HTML 页面内容 const char index_html[] PROGMEM = R"rawliteral( ESP32 Data Display

实时数据显示

温度: 加载中... °C

光照强度(A): 加载中... lx

光照强度(B): 加载中... lx

电位器Wiper值: 加载中...

总线电压: 加载中... V

分流电压: 加载中... mV

电流: 加载中... mA

功率: 加载中... mW

设置Wiper值

)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.println("Connected to WiFi"); Serial.println(WiFi.localIP()); // 根路径的页面 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send_P(200, "text/html", index_html); }); // 温度接口 server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request) { String temp = String(sensor.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(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"); } //陶晶池串口屏发送 void TJC_Show(void) { char str[128]; sprintf(str, "t0.txt=\"%.2f\"\xff\xff\xff", sensor.getTempC());//用sprintf来格式化字符串,给t0的txt属性赋值 TJC.print(str); //把字符串发送出去 sprintf(str, "t1.txt=\"%.f\"\xff\xff\xff", bh1750_a.readLightLevel()); 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 setup(void) { Serial.begin(115200); // 初始化 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); //WebServer 初始化 WebServer_Init(ssid, password); } void loop(void) {// put your main code here, to run repeatedly: sensor.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) //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); }