Restructured full repo
This commit is contained in:
29
firmware/src/ConfigHardware.h
Normal file
29
firmware/src/ConfigHardware.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
//#define _HW_V_20
|
||||
|
||||
// HX711 load cell
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#ifdef _HW_V_20
|
||||
const int CONFIG_SCALE_DOUT_PIN = 23;
|
||||
const int CONFIG_SCALE_SCK_PIN = 22;
|
||||
#else
|
||||
const int CONFIG_SCALE_DOUT_PIN = 22;
|
||||
const int CONFIG_SCALE_SCK_PIN = 23;
|
||||
#endif
|
||||
|
||||
#else
|
||||
const int CONFIG_SCALE_DOUT_PIN = D2;
|
||||
const int CONFIG_SCALE_SCK_PIN = D3;
|
||||
#endif
|
||||
const uint8_t CONFIG_MEASUREMENT_AVG_COUNT = 1; // number of measurements in normal phase
|
||||
const uint8_t CONFIG_TARE_AVG_COUNT = 6; // number of measurements in tare-phase (to find 0 )
|
||||
const int CONFIG_MEASURE_DELAY = 100; // interval in ms between measurements
|
||||
//const int CONFIG_VALUE_DIVIDER = 8; // uint32 measurements are divided by this factor, before stored in uint16_t
|
||||
const int CONFIG_VALUE_DIVIDER = 256; // uint32 measurements are divided by this factor, before stored in uint16_t
|
||||
|
||||
const uint32_t CONFIG_SESSION_CHUNK_SIZE = 1024; //1024*8 - 16 * sizeof(uint32_t);
|
||||
5
firmware/src/ConfigWifi.h
Normal file
5
firmware/src/ConfigWifi.h
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
const char *CONFIG_WIFI_SSID = "WLAN";
|
||||
const char *CONFIG_WIFI_PASSWORD = "Bau3rWLAN";
|
||||
const char* CONFIG_HOSTNAME = "smartswim";
|
||||
75
firmware/src/DeviceInfoLog.h
Normal file
75
firmware/src/DeviceInfoLog.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#ifdef USE_ESP32
|
||||
#include "SPIFFS.h"
|
||||
#else
|
||||
#include <FS.h>
|
||||
#endif
|
||||
|
||||
inline void printDeviceInfo()
|
||||
{
|
||||
/*
|
||||
FSInfo fs_info;
|
||||
SPIFFS.info(fs_info);
|
||||
|
||||
float fileTotalKB = (float)fs_info.totalBytes / 1024.0;
|
||||
float fileUsedKB = (float)fs_info.usedBytes / 1024.0;
|
||||
|
||||
float flashChipSize = (float)ESP.getFlashChipSize() / 1024.0 / 1024.0;
|
||||
float realFlashChipSize = (float)ESP.getFlashChipRealSize() / 1024.0 / 1024.0;
|
||||
float flashFreq = (float)ESP.getFlashChipSpeed() / 1000.0 / 1000.0;
|
||||
FlashMode_t ideMode = ESP.getFlashChipMode();
|
||||
|
||||
Serial.printf("\n#####################\n");
|
||||
|
||||
Serial.printf("__________________________\n\n");
|
||||
Serial.println("Firmware: ");
|
||||
Serial.printf(" Chip Id: %08X\n", ESP.getChipId());
|
||||
Serial.print(" Core version: ");
|
||||
Serial.println(ESP.getCoreVersion());
|
||||
Serial.print(" SDK version: ");
|
||||
Serial.println(ESP.getSdkVersion());
|
||||
Serial.print(" Boot version: ");
|
||||
Serial.println(ESP.getBootVersion());
|
||||
Serial.print(" Boot mode: ");
|
||||
Serial.println(ESP.getBootMode());
|
||||
|
||||
Serial.printf("__________________________\n\n");
|
||||
|
||||
Serial.println("Flash chip information: ");
|
||||
Serial.printf(" Flash chip Id: %08X (for example: Id=001640E0 Manuf=E0, Device=4016 (swap bytes))\n", ESP.getFlashChipId());
|
||||
Serial.printf(" Sketch thinks Flash RAM is size: ");
|
||||
Serial.print(flashChipSize);
|
||||
Serial.println(" MB");
|
||||
Serial.print(" Actual size based on chip Id: ");
|
||||
Serial.print(realFlashChipSize);
|
||||
Serial.println(" MB ... given by (2^( Device - 1) / 8 / 1024");
|
||||
Serial.print(" Flash frequency: ");
|
||||
Serial.print(flashFreq);
|
||||
Serial.println(" MHz");
|
||||
Serial.printf(" Flash write mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
|
||||
|
||||
Serial.printf("__________________________\n\n");
|
||||
|
||||
Serial.println("File system (SPIFFS): ");
|
||||
Serial.print(" Total KB: ");
|
||||
Serial.print(fileTotalKB);
|
||||
Serial.println(" KB");
|
||||
Serial.print(" Used KB: ");
|
||||
Serial.print(fileUsedKB);
|
||||
Serial.println(" KB");
|
||||
Serial.printf(" Block size: %u\n", fs_info.blockSize);
|
||||
Serial.printf(" Page size: %u\n", fs_info.pageSize);
|
||||
Serial.printf(" Maximum open files: %u\n", fs_info.maxOpenFiles);
|
||||
Serial.printf(" Maximum path length: %u\n\n", fs_info.maxPathLength);
|
||||
|
||||
String str = "";
|
||||
Dir dir = SPIFFS.openDir("/dat");
|
||||
while (dir.next())
|
||||
{
|
||||
str += dir.fileName();
|
||||
str += " / ";
|
||||
str += dir.fileSize();
|
||||
str += "\r\n";
|
||||
}
|
||||
Serial.print(str);
|
||||
*/
|
||||
}
|
||||
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