第一次
This commit is contained in:
153
src/main.cpp
Normal file
153
src/main.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
#include "DS18B20.h"
|
||||
|
||||
#include "BH1750.h"
|
||||
#include "Wire.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
//#include "TCJ_Show.h"
|
||||
//#include "ESP32_WebServer.h"
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
const char* ssid = "SERVIRST-CT";
|
||||
const char* password = "servirst8888";
|
||||
|
||||
|
||||
#define ONE_WIRE_BUS 4
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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());
|
||||
|
||||
TJC_Show(); //串口屏
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
Reference in New Issue
Block a user