Restructured full repo
This commit is contained in:
239
firmware/src/firmware_main.cpp
Normal file
239
firmware/src/firmware_main.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
#define USE_ESP32
|
||||
|
||||
// Arduino & ESP headers
|
||||
#include <Arduino.h>
|
||||
#ifdef USE_ESP32
|
||||
#include <WiFi.h>
|
||||
#else
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#endif
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <WiFiUdp.h> // for NTP
|
||||
#include <NTPClient.h> // for NTP
|
||||
|
||||
// Own libs
|
||||
#include "Dtypes.h"
|
||||
#include "MockScale.h"
|
||||
#include "Scale.h"
|
||||
#include "MeasurementSession.h"
|
||||
#include "SpiffsStorage.h"
|
||||
#include "DeviceInfoLog.h"
|
||||
|
||||
// Configuration
|
||||
#include "ConfigWifi.h"
|
||||
#include "ConfigHardware.h"
|
||||
|
||||
#include "AsyncWebDav.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>
|
||||
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() {
|
||||
measuring_ = true;
|
||||
lastCallTime_ = 0;
|
||||
session.init( timeClient.getEpochTime() );
|
||||
}
|
||||
|
||||
void stopMeasurements() {
|
||||
measuring_ = false;
|
||||
session.finalize();
|
||||
}
|
||||
|
||||
bool isMeasuring() const {
|
||||
return measuring_;
|
||||
}
|
||||
|
||||
void iteration() {
|
||||
if( ! measuring_ ) {
|
||||
//Serial.println("Disabled");
|
||||
return;
|
||||
}
|
||||
uint16_t measurement=-1;
|
||||
scale.measure(measurement);
|
||||
session.addPoint(measurement);
|
||||
Serial.print("Measurement: ");
|
||||
Serial.println(measurement);
|
||||
if( lastCallTime_ != 0) {
|
||||
const long cycleDuration = millis() - lastCallTime_;
|
||||
if( cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||
{
|
||||
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
|
||||
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||
}
|
||||
}
|
||||
lastCallTime_ = millis();
|
||||
}
|
||||
|
||||
Session_T & getSession() { return session; }
|
||||
|
||||
private:
|
||||
Scale<CONFIG_VALUE_DIVIDER> scale;
|
||||
//MockScale scale;
|
||||
Session_T session;
|
||||
bool measuring_;
|
||||
long lastCallTime_;
|
||||
};
|
||||
|
||||
SessionManager<Session_T> sessionManager;
|
||||
|
||||
void onNotFound(AsyncWebServerRequest *request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
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"));
|
||||
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"));
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
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) {}
|
||||
Serial.println(" ");
|
||||
Serial.println("----- New start -----");
|
||||
|
||||
// File system
|
||||
bool spiffsResult = SPIFFS.begin(true);
|
||||
Serial.printf("Spiffs begin %d\n", spiffsResult);
|
||||
|
||||
printDeviceInfo();
|
||||
|
||||
// WiFi
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||
#ifdef USE_ESP32
|
||||
WiFi.setHostname(CONFIG_HOSTNAME);
|
||||
#else
|
||||
WIFI.hostname(CONFIG_HOSTNAME);
|
||||
#endif
|
||||
Serial.print(F("\n\n"));
|
||||
Serial.println(F("Waiting for WIFI connection..."));
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
}
|
||||
Serial.print(F("Connected to WiFi. IP:"));
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// NTP
|
||||
timeClient.begin();
|
||||
timeClient.update();
|
||||
|
||||
// Session
|
||||
sessionManager.begin();
|
||||
|
||||
// HTTP & Websocket server
|
||||
httpSetup(&sessionManager);
|
||||
|
||||
Serial.println("Spiffs listing:");
|
||||
listDir(SPIFFS, "/", 3);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sessionManager.iteration();
|
||||
}
|
||||
Reference in New Issue
Block a user