Compare commits

...

2 Commits

Author SHA1 Message Date
933ce71db4 restart via http 2025-02-17 23:24:17 +01:00
4ca795501b centralized LittleFS + AsyncWebServer 2025-02-17 23:17:10 +01:00
8 changed files with 95 additions and 27 deletions

View File

@ -2,7 +2,6 @@
<html lang="de">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>G&auml;rbox</title>
<style>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gewächshaus</title>
<style>
body {
font-family: sans-serif;
font-size: 4vw;
margin: 1em;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
div {
overflow: hidden;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>Gewächshaus</h1>
TODO
</body>
</html>

View File

@ -1,6 +1,5 @@
#ifdef NODE_FERMENTER
#include <ESPAsyncWebServer.h>
#include <LedControl.h>
#include <LittleFS.h>
#include <ArduinoOTA.h>
@ -9,6 +8,7 @@
#include "patrix/PIDController.h"
#include "patrix/PWMOutput.h"
#include "patrix/Rotary.h"
#include "patrix/http.h"
#define HEATER_POWER_W 30
@ -16,8 +16,6 @@
void rotaryCallback(int delta);
AsyncWebServer server(80);
DS18B20 ds18b20("DS18B20", D4);
DS18B20Sensor input(ds18b20, 0, "");
@ -171,21 +169,7 @@ void httpTargetAdd(AsyncWebServerRequest *request) {
httpStatus(request);
}
void httpNotFound(AsyncWebServerRequest *request) {
if (request->method() == HTTP_OPTIONS) {
request->send(200);
} else {
request->send(404, "text/plain", "not found");
}
}
void patrixSetup() {
if (LittleFS.begin()) {
Log.info("Filesystem mounted.");
} else {
Log.error("Failed to mount filesystem!");
}
ds18b20.setup();
heater.setup();
rotary.setup();
@ -193,16 +177,10 @@ void patrixSetup() {
targetFileSetup();
pid.setup();
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Content-Type");
server.serveStatic("/", LittleFS, "/http/", "max-age=86400").setDefaultFile("index.html");
server.on("/status", httpStatus);
server.on("/status/", httpStatus);
server.on("/target/add", httpTargetAdd);
server.on("/target/add/", httpTargetAdd);
server.onNotFound(httpNotFound);
server.begin();
}
void patrixLoop() {

View File

@ -1,17 +1,26 @@
#include <LittleFS.h>
#include "Patrix.h"
#include "http.h"
void setup() {
Log.info("Startup.");
if (LittleFS.begin()) {
Log.info("Filesystem mounted.");
} else {
Log.error("Failed to mount filesystem!");
}
wifiConnect();
}
void loop() {
wifiLoop();
mqttLoop();
if (isAfterBootDelay()) {
if (isBootDelayComplete()) {
if (isSetupTimeAfterBootDelay()) {
patrixSetup();
httpSetup();
}
patrixLoop();
httpLoop();
}
}

39
src/patrix/http.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "http.h"
#include "mqtt.h"
#include <LittleFS.h>
AsyncWebServer server(80);
bool httpDoRestart = false;
void httpNotFound(AsyncWebServerRequest *request) {
if (request->method() == HTTP_OPTIONS) {
request->send(200);
} else {
request->send(404, "text/plain", "not found");
}
}
void httpRestart([[maybe_unused]] AsyncWebServerRequest *request) {
httpDoRestart = true;
request->send(200, "application/json", "true");
}
void httpSetup() {
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Content-Type");
server.on("/restart", httpRestart);
server.serveStatic("/", LittleFS, "/http/", "max-age=86400").setDefaultFile("index.html");
server.onNotFound(httpNotFound);
server.begin();
}
void httpLoop() {
if (httpDoRestart) {
Log.info("Restarting...");
delay(500);
EspClass::restart();
}
}

12
src/patrix/http.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef HELLIGKEIT_HTTP_H
#define HELLIGKEIT_HTTP_H
#include <ESPAsyncWebServer.h>
extern AsyncWebServer server;
void httpSetup();
void httpLoop();
#endif

View File

@ -156,6 +156,6 @@ bool isSetupTimeAfterBootDelay() {
return false;
}
bool isAfterBootDelay() {
bool isBootDelayComplete() {
return bootDelayOver;
}

View File

@ -13,7 +13,7 @@ bool isWifiConnected();
bool isSetupTimeAfterBootDelay();
bool isAfterBootDelay();
bool isBootDelayComplete();
uint64_t uptimeMillis();