170 lines
4.9 KiB
C++
170 lines
4.9 KiB
C++
// Arduino & ESP headers
|
|
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESPAsyncTCP.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <WiFiUdp.h> // for NTP
|
|
#include <NTPClient.h> // for NTP
|
|
|
|
// Own libs
|
|
#include "Dtypes.h"
|
|
#include "MockScale.h"
|
|
#include "MeasurementSession.h"
|
|
#include "SpiffsStorage.h"
|
|
|
|
// Configuration
|
|
#include "ConfigWifi.h"
|
|
#include "ConfigHardware.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;
|
|
}
|
|
|
|
void stopMeasurements() {
|
|
measuring_ = false;
|
|
}
|
|
|
|
void iteration() {
|
|
if( ! measuring_ ) {
|
|
return;
|
|
}
|
|
uint16_t measurement;
|
|
scale.measure(measurement);
|
|
session.addPoint(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.print("Warning: measurements skipped: ");
|
|
Serial.println(skipped);
|
|
|
|
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:
|
|
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, [sessionManager](AsyncWebServerRequest * req) {
|
|
req->send(200, "text/plain", F("OK"));
|
|
sessionManager->startMeasurements();
|
|
Serial.println("Started measurements");
|
|
});
|
|
server.on("/api/session/stop", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) {
|
|
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();
|
|
}
|
|
|
|
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 {
|
|
Serial.print("Partial send maxLen ");
|
|
Serial.print(maxLen);
|
|
Serial.print(" index ");
|
|
Serial.println(index);
|
|
CopyWriter copyWriter(buffer);
|
|
ChunkedStreamingMsgPackEncoder<CopyWriter> encoder(©Writer, index, index + maxLen);
|
|
sessionManager->getSession().serialize(encoder, startIdx);
|
|
Serial.print("Bytes sent ");
|
|
Serial.println(encoder.sentBytes() - index);
|
|
return encoder.sentBytes() - index;
|
|
};
|
|
AsyncWebServerResponse *response = req->beginResponse("application/x-msgpack", totalSize, callback);
|
|
auto sessionId = sessionManager->getSession().getStartTime();
|
|
response->addHeader("content-disposition", "attachment; filename=\"" + String(sessionId) + ".st\"");
|
|
req->send(response);
|
|
});
|
|
|
|
server.onNotFound(onNotFound);
|
|
|
|
server.begin();
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
// Serial
|
|
Serial.begin(115200);
|
|
while(!Serial) {}
|
|
|
|
// WiFi
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
|
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);
|
|
}
|
|
|
|
void loop() {
|
|
sessionManager.iteration();
|
|
}
|