2025-01-14 13:46:57 +08:00
|
|
|
|
#include "DS18B20.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "BH1750.h"
|
|
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
|
//#include "TCJ_Show.h"
|
|
|
|
|
|
//#include "ESP32_WebServer.h"
|
2025-01-15 16:52:01 +08:00
|
|
|
|
#include "MCP45HVX1.h"
|
|
|
|
|
|
|
|
|
|
|
|
MCP45HVX1 digiPot(0x3C);
|
2025-01-14 13:46:57 +08:00
|
|
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
|
|
|
|
|
|
|
const char* ssid = "SERVIRST-CT";
|
|
|
|
|
|
const char* password = "servirst8888";
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-15 16:52:01 +08:00
|
|
|
|
#define ONE_WIRE_BUS 8
|
2025-01-14 13:46:57 +08:00
|
|
|
|
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(
|
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
<html>
|
|
|
|
|
|
<head>
|
|
|
|
|
|
<title>ESP32 Data Display</title>
|
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
|
<script>
|
|
|
|
|
|
async function fetchData() {
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
setInterval(fetchData, 1000);
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</head>
|
|
|
|
|
|
<body onload="fetchData()">
|
|
|
|
|
|
<h1>实时数据显示</h1>
|
|
|
|
|
|
<p>温度: <span id="temperature">加载中...</span> °C</p>
|
|
|
|
|
|
<p>光照强度(A): <span id="lightA">加载中...</span> lx</p>
|
|
|
|
|
|
<p>光照强度(B): <span id="lightB">加载中...</span> lx</p>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
)rawliteral";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void WebServer_Init(const char* ssid, const char* password) // 初始化Web
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
server.begin();
|
|
|
|
|
|
Serial.println("Web server started");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//陶晶池串口屏发送
|
|
|
|
|
|
void TJC_Show(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
char str[64];
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 16:52:01 +08:00
|
|
|
|
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 ...........");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-14 13:46:57 +08:00
|
|
|
|
void setup(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
|
|
|
|
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_init(bh1750_a, bh1750_b, 19, 18, 21, 20); //1750初始化。sda, scl
|
2025-01-15 16:52:01 +08:00
|
|
|
|
|
|
|
|
|
|
//WebServer_Init(ssid, password);
|
|
|
|
|
|
|
|
|
|
|
|
MCP45HVX1_Test();
|
2025-01-14 13:46:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
|
|
TJC_Show(); //串口屏
|
|
|
|
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
}
|