Switched to native ESP32 webserver
- new status API - webdav without crashing
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#endif
|
||||
#include <ESPAsyncWebServer.h>
|
||||
//#include <ESPAsyncWebServer.h>
|
||||
#include <WiFiUdp.h> // for NTP
|
||||
#include <NTPClient.h> // for NTP
|
||||
|
||||
@@ -23,55 +23,65 @@
|
||||
#include "ConfigWifi.h"
|
||||
#include "ConfigHardware.h"
|
||||
|
||||
#include "AsyncWebDav.h"
|
||||
#include "EspHttp.h"
|
||||
#include "WebDAV.h"
|
||||
|
||||
AsyncWebServer server(80);
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
||||
|
||||
typedef MeasurementSession<uint16_t, SpiffsStorageReader, SpiffsStorageWriter, CONFIG_SESSION_CHUNK_SIZE> Session_T;
|
||||
|
||||
template<typename Session_T>
|
||||
template <typename Session_T>
|
||||
class SessionManager
|
||||
{
|
||||
public:
|
||||
SessionManager() : measuring_(false), lastCallTime_(0)
|
||||
{}
|
||||
|
||||
void begin() {
|
||||
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
||||
scale.tare( CONFIG_TARE_AVG_COUNT );
|
||||
session.init( timeClient.getEpochTime() );
|
||||
{
|
||||
}
|
||||
|
||||
void startMeasurements() {
|
||||
void begin()
|
||||
{
|
||||
Serial.println("Beginning tare");
|
||||
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
||||
scale.tare(CONFIG_TARE_AVG_COUNT);
|
||||
Serial.println("Finished tare");
|
||||
session.init(timeClient.getEpochTime());
|
||||
}
|
||||
|
||||
void startMeasurements()
|
||||
{
|
||||
measuring_ = true;
|
||||
lastCallTime_ = 0;
|
||||
session.init( timeClient.getEpochTime() );
|
||||
session.init(timeClient.getEpochTime());
|
||||
}
|
||||
|
||||
void stopMeasurements() {
|
||||
void stopMeasurements()
|
||||
{
|
||||
measuring_ = false;
|
||||
session.finalize();
|
||||
}
|
||||
|
||||
bool isMeasuring() const {
|
||||
bool isMeasuring() const
|
||||
{
|
||||
return measuring_;
|
||||
}
|
||||
|
||||
void iteration() {
|
||||
if( ! measuring_ ) {
|
||||
void iteration()
|
||||
{
|
||||
if (!measuring_)
|
||||
{
|
||||
//Serial.println("Disabled");
|
||||
return;
|
||||
}
|
||||
uint16_t measurement=-1;
|
||||
uint16_t measurement = -1;
|
||||
scale.measure(measurement);
|
||||
session.addPoint(measurement);
|
||||
Serial.print("Measurement: ");
|
||||
Serial.println(measurement);
|
||||
if( lastCallTime_ != 0) {
|
||||
if (lastCallTime_ != 0)
|
||||
{
|
||||
const long cycleDuration = millis() - lastCallTime_;
|
||||
if( cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||
if (cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||
{
|
||||
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||
}
|
||||
@@ -80,8 +90,8 @@ public:
|
||||
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
||||
//Serial.printf("Warning: measurements skipped: %d, cycleDuration %d", skipped, cycleDuration);
|
||||
|
||||
for(int i=0; i < skipped; ++i)
|
||||
session.addPoint(measurement);
|
||||
for (int i = 0; i < skipped; ++i)
|
||||
session.addPoint(measurement);
|
||||
|
||||
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||
}
|
||||
@@ -89,7 +99,7 @@ public:
|
||||
lastCallTime_ = millis();
|
||||
}
|
||||
|
||||
Session_T & getSession() { return session; }
|
||||
Session_T &getSession() { return session; }
|
||||
|
||||
private:
|
||||
Scale<CONFIG_VALUE_DIVIDER> scale;
|
||||
@@ -101,99 +111,105 @@ private:
|
||||
|
||||
SessionManager<Session_T> sessionManager;
|
||||
|
||||
void onNotFound(AsyncWebServerRequest *request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
}
|
||||
EspHttp espHttpServer;
|
||||
|
||||
template<typename Session_T>
|
||||
void httpSetup(SessionManager<Session_T> * sessionManager)
|
||||
template <typename Session_T>
|
||||
void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||
{
|
||||
server.on("/api/session/start", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
||||
AsyncWebServerResponse *response = req->beginResponse(200, "text/plain", F("OK"));
|
||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
req->send(response);
|
||||
//req->send(200, "text/plain", F("OK"));
|
||||
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
||||
httpd_resp_send(req, "Session started", -1);
|
||||
sessionManager->startMeasurements();
|
||||
Serial.println("Started measurements");
|
||||
});
|
||||
server.on("/api/session/stop", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
||||
AsyncWebServerResponse *response = req->beginResponse(200, "text/plain", F("OK"));
|
||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
req->send(response);
|
||||
//req->send(200, "text/plain", F("OK"));
|
||||
Serial.println("Started session");
|
||||
};
|
||||
auto cbStopSession = [sessionManager](httpd_req_t *req) {
|
||||
httpd_resp_send(req, "Session stopped", -1);
|
||||
sessionManager->stopMeasurements();
|
||||
Serial.println("Stopped measurements");
|
||||
});
|
||||
server.on("/api/session/data", HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
||||
uint32_t startIdx = 0;
|
||||
if( req->hasParam("startIdx") ) {
|
||||
startIdx = req->getParam("startIdx")->value().toInt();
|
||||
}
|
||||
Serial.print("Data request, start index: ");
|
||||
Serial.println(startIdx);
|
||||
Serial.println("Stopped session");
|
||||
};
|
||||
auto cbStatus = [sessionManager](httpd_req_t *req) {
|
||||
String result;
|
||||
result.reserve(512);
|
||||
httpd_resp_set_hdr(req, "Content-Type", "application/json");
|
||||
|
||||
// session
|
||||
{
|
||||
result += "{ \"session\": ";
|
||||
if (sessionManager->isMeasuring())
|
||||
{
|
||||
const auto &session = sessionManager->getSession();
|
||||
result += "{ \"started\":" + String(session.getStartTime()) + ", ";
|
||||
result += "\"num_measurements\":" + String(session.numMeasurements()) + "},\n";
|
||||
}
|
||||
else
|
||||
result += "{},\n";
|
||||
}
|
||||
// flash
|
||||
{
|
||||
const String usedBytes(portablefs::usedBytes());
|
||||
const String freeBytes(portablefs::totalBytes() - portablefs::usedBytes());
|
||||
result += "\"file_system\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
||||
}
|
||||
// RAM
|
||||
{
|
||||
const String freeBytes(ESP.getFreeHeap());
|
||||
const String usedBytes(ESP.getHeapSize() - ESP.getFreeHeap());
|
||||
result += "\"ram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
||||
}
|
||||
// PSRAM
|
||||
{
|
||||
const String freeBytes(ESP.getFreePsram());
|
||||
const String usedBytes(ESP.getPsramSize() - ESP.getFreePsram());
|
||||
result += "\"psram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "}\n";
|
||||
}
|
||||
result += "}";
|
||||
httpd_resp_send(req, result.c_str(), result.length());
|
||||
};
|
||||
auto cbGetData = [sessionManager](httpd_req_t *req) {
|
||||
auto sessionId = sessionManager->getSession().getStartTime();
|
||||
uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0);
|
||||
Serial.printf("Data request, start index: %d", startIdx);
|
||||
|
||||
//headers
|
||||
httpd_resp_set_hdr(req, "Content-Type", "application/x-msgpack");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "content-disposition", ("attachment; filename=\"" + String(sessionId) + ".st\"").c_str());
|
||||
|
||||
//data
|
||||
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
||||
encoderToDetermineSize.setSizeCountMode(true);
|
||||
sessionManager->getSession().serialize(encoderToDetermineSize, startIdx);
|
||||
auto totalSize = encoderToDetermineSize.getContentLength();
|
||||
Serial.print("Sending started of total size ");
|
||||
Serial.println(totalSize);
|
||||
auto callback = [=](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
|
||||
CopyWriter copyWriter(buffer);
|
||||
ChunkedStreamingMsgPackEncoder<CopyWriter> encoder(©Writer, index, index + maxLen);
|
||||
sessionManager->getSession().serialize(encoder, startIdx);
|
||||
return encoder.sentBytes() - index;
|
||||
};
|
||||
AsyncWebServerResponse *response = req->beginResponse("application/x-msgpack", totalSize, callback);
|
||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
auto sessionId = sessionManager->getSession().getStartTime();
|
||||
response->addHeader("content-disposition", "attachment; filename=\"" + String(sessionId) + ".st\"");
|
||||
req->send(response);
|
||||
});
|
||||
server.addHandler(new SpiffsWebDavHandler("/webdav", "/dat"));
|
||||
server.onNotFound(onNotFound);
|
||||
char *buf = (char *)malloc(totalSize);
|
||||
CopyWriter copyWriter((uint8_t *)buf);
|
||||
StreamingMsgPackEncoder<CopyWriter> encoder(©Writer);
|
||||
sessionManager->getSession().serialize(encoder, startIdx);
|
||||
httpd_resp_send(req, buf, totalSize);
|
||||
free(buf);
|
||||
};
|
||||
|
||||
server.begin();
|
||||
espHttpServer.start();
|
||||
espHttpServer.on("/api/session/start", HTTP_GET, cbStartSession);
|
||||
espHttpServer.on("/api/session/start", HTTP_POST, cbStartSession);
|
||||
|
||||
espHttpServer.on("/api/session/stop", HTTP_GET, cbStopSession);
|
||||
espHttpServer.on("/api/session/stop", HTTP_POST, cbStopSession);
|
||||
|
||||
espHttpServer.on("/api/session/data", HTTP_GET, cbGetData);
|
||||
espHttpServer.on("/api/status", HTTP_GET, cbStatus);
|
||||
|
||||
espHttpServer.on("/webdav/*?", HTTP_GET, webdavHandler);
|
||||
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdavHandler);
|
||||
espHttpServer.on("/webdav/*?", HTTP_DELETE, webdavHandler);
|
||||
}
|
||||
|
||||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
||||
Serial.printf("Listing directory: %s\r\n", dirname);
|
||||
|
||||
File root = fs.open(dirname);
|
||||
if(!root){
|
||||
Serial.println("- failed to open directory");
|
||||
return;
|
||||
}
|
||||
if(!root.isDirectory()){
|
||||
Serial.println(" - not a directory");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
while(file){
|
||||
if(file.isDirectory()){
|
||||
Serial.print(" DIR : ");
|
||||
Serial.println(file.name());
|
||||
if(levels){
|
||||
listDir(fs, file.name(), levels -1);
|
||||
}
|
||||
} else {
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(file.name());
|
||||
Serial.print("\tSIZE: ");
|
||||
Serial.println(file.size());
|
||||
}
|
||||
file = root.openNextFile();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Serial
|
||||
Serial.begin(115200);
|
||||
while(!Serial) {}
|
||||
while (!Serial)
|
||||
{
|
||||
}
|
||||
Serial.println(" ");
|
||||
Serial.println("----- New start -----");
|
||||
|
||||
@@ -203,18 +219,27 @@ void setup()
|
||||
|
||||
printDeviceInfo();
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
// WiFi
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||
#ifdef PLATFORM_ESP32
|
||||
#ifdef PLATFORM_ESP32
|
||||
WiFi.setHostname(CONFIG_HOSTNAME);
|
||||
#else
|
||||
#else
|
||||
WIFI.hostname(CONFIG_HOSTNAME);
|
||||
#endif
|
||||
#endif
|
||||
Serial.print(F("\n\n"));
|
||||
Serial.println(F("Waiting for WIFI connection..."));
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
int connectCounter = 0;
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(1000);
|
||||
connectCounter += 1;
|
||||
if (connectCounter > 5)
|
||||
{
|
||||
Serial.println("Couldn't obtain WIFI - trying begin() again");
|
||||
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||
}
|
||||
}
|
||||
Serial.print(F("Connected to WiFi. IP:"));
|
||||
Serial.println(WiFi.localIP());
|
||||
@@ -229,14 +254,10 @@ void setup()
|
||||
// HTTP & Websocket server
|
||||
httpSetup(&sessionManager);
|
||||
|
||||
Serial.println("Spiffs listing:");
|
||||
listDir(SPIFFS, "/", 3);
|
||||
|
||||
//
|
||||
Serial.print("Free Heap");
|
||||
Serial.println(ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sessionManager.iteration();
|
||||
void loop()
|
||||
{
|
||||
delay(10000);
|
||||
//sessionManager.iteration();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user