From 7ae434299fef465d9286651e0be80a76b13bb3e5 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Tue, 23 Jun 2020 21:35:28 +0200 Subject: [PATCH] Added restart/tare to API and various fixes --- firmware/lib/esphttp/EspHttp.h | 2 +- firmware/lib/scale/Scale.h | 18 ++++++++---- firmware/lib/session/SessionManager.h | 12 +++++++- .../lib/session/SimpleMeasurementSession.h | 4 +++ firmware/src/SwimTrackerConfig.h | 17 +++++++---- firmware/src/firmware_main.cpp | 29 +++++++++++++++++-- 6 files changed, 66 insertions(+), 16 deletions(-) diff --git a/firmware/lib/esphttp/EspHttp.h b/firmware/lib/esphttp/EspHttp.h index 20f50f1..d7ccf61 100644 --- a/firmware/lib/esphttp/EspHttp.h +++ b/firmware/lib/esphttp/EspHttp.h @@ -4,7 +4,7 @@ #include #include "Dtypes.h" -constexpr int MAX_URI_HANDLERS = 10; +constexpr int MAX_URI_HANDLERS = 20; esp_err_t rawCallback(httpd_req_t *req); diff --git a/firmware/lib/scale/Scale.h b/firmware/lib/scale/Scale.h index 4555192..90ce534 100644 --- a/firmware/lib/scale/Scale.h +++ b/firmware/lib/scale/Scale.h @@ -8,17 +8,21 @@ class Scale public: bool measure(uint16_t &measurementOut) { - if (hx711_.is_ready()) - { + //if (hx711_.is_ready()) + //{ long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_; if (value < 0) measurementOut = (int16_t)(-value / DIVIDER); else measurementOut = 0; return true; - } - else - return false; + //} + //else { + // long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_; +// + // Serial.printf("Measurement failed %ld\n", value); + // return false; + //} } void begin(uint32_t pinDOUT, uint32_t pinSCK) @@ -28,10 +32,12 @@ public: void tare(uint32_t numMeasurementsToAverage = 50) { + hx711_.read_average(3); offset_ = hx711_.read_average(numMeasurementsToAverage); + Serial.printf("Tare offset %ld\n", offset_); } - long &offset() const { return offset_; } + const long &offset() const { return offset_; } private: HX711 hx711_; diff --git a/firmware/lib/session/SessionManager.h b/firmware/lib/session/SessionManager.h index 8473268..ea974be 100644 --- a/firmware/lib/session/SessionManager.h +++ b/firmware/lib/session/SessionManager.h @@ -13,6 +13,7 @@ public: void begin(); void tare(); + long tareOffset() const { return scale_.offset(); }; void startMeasurements(); void stopMeasurements(); bool isMeasuring() const { return measuring_; } @@ -51,6 +52,8 @@ SessionManager::SessionManager(int scaleDoutPin, int scaleSckPin, uint template void SessionManager::tare() { + if(measuring_) + stopMeasurements(); Serial.println("Beginning tare"); scale_.begin(scaleDoutPin_, scaleSckPin_); scale_.tare(CONFIG_TARE_AVG_COUNT); @@ -69,6 +72,8 @@ void SessionManager::begin() template void SessionManager::startMeasurements() { + if(measuring_ == true) + return; measuring_ = true; lastCallTime_ = 0; session_.init(timeClient_.getEpochTime()); @@ -77,6 +82,8 @@ void SessionManager::startMeasurements() template void SessionManager::stopMeasurements() { + if(measuring_ == false) + return; session_.finalize(); measuring_ = false; } @@ -88,8 +95,11 @@ void SessionManager::iteration() return; uint16_t measurement = -1; - scale_.measure(measurement); + bool measurementDone = false; + while(!measurementDone) + measurementDone = scale_.measure(measurement); bool addPointSuccessful = session_.addPoint(measurement); + Serial.printf("Measured: %d\n", measurement); if (!addPointSuccessful) { Serial.println("Maximum time of session reached - stopping"); diff --git a/firmware/lib/session/SimpleMeasurementSession.h b/firmware/lib/session/SimpleMeasurementSession.h index ab61abf..87081e7 100644 --- a/firmware/lib/session/SimpleMeasurementSession.h +++ b/firmware/lib/session/SimpleMeasurementSession.h @@ -77,6 +77,10 @@ private: if (portablefs::exists(filename.c_str())) { auto file = portablefs::open(filename.c_str(), "a"); + file.seek(0, SeekSet); + StreamingMsgPackEncoder encoder(&file); + chunk->sendHeader(encoder, chunk->getStartTime(), 0); + file.seek(0, SeekEnd); size_t existingMeasurements = (file.size() - ChunkT::valueOffset()) / sizeof(Measurement_T); Serial.printf("Incremental save, existing %d\n", existingMeasurements); diff --git a/firmware/src/SwimTrackerConfig.h b/firmware/src/SwimTrackerConfig.h index 3291fd6..859d337 100644 --- a/firmware/src/SwimTrackerConfig.h +++ b/firmware/src/SwimTrackerConfig.h @@ -3,25 +3,32 @@ #include +// Uncomment for Version 2.0 where load cell is connected differently +#define _HW_V_20 +//#define NEW_HEAVY_LOAD_CELL + // ------------------------------------------ WiFi --------------------------------------------------------------------------------- -const char *CONFIG_WIFI_SSID = "WLAN"; +//const char *CONFIG_WIFI_SSID = "WLAN"; +const char *CONFIG_WIFI_SSID = "RepeaterWZ"; const char *CONFIG_WIFI_PASSWORD = "Bau3rWLAN"; const char *CONFIG_HOSTNAME = "smartswim"; // ------------------------------------- Hardware & Measurement Settings ------------------------------------------------------------ -// Uncomment for Version 2.0 where load cell is connected differently -//#define _HW_V_20 const uint8_t CONFIG_MEASUREMENT_AVG_COUNT = 1; // number of measurements in normal phase -const uint8_t CONFIG_TARE_AVG_COUNT = 10; // number of measurements in tare-phase (to find 0 ) +const uint8_t CONFIG_TARE_AVG_COUNT = 20; // 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 = 128; // uint32 measurements are divided by this factor, before stored in uint16_t const uint32_t CONFIG_SESSION_MAX_LENGTH_HOURS = 3; // maximum length of one session const char *CONFIG_DATA_PATH = "/dat"; // folder in SPIFFS file system to store measurement data using MeasurementT = uint16_t; // data type for one measurement +#ifdef NEW_HEAVY_LOAD_CELL +const int CONFIG_VALUE_DIVIDER = 8; // uint32 measurements are divided by this factor, before stored in uint16_t +#else +const int CONFIG_VALUE_DIVIDER = 128; // uint32 measurements are divided by this factor, before stored in uint16_t +#endif // ------------------------------------- Derived Settings ----------------------------------------------------------------------------- diff --git a/firmware/src/firmware_main.cpp b/firmware/src/firmware_main.cpp index 2878742..17a1c75 100644 --- a/firmware/src/firmware_main.cpp +++ b/firmware/src/firmware_main.cpp @@ -24,15 +24,25 @@ template void httpSetup(SessionManager *sessionManager) { auto cbStartSession = [sessionManager](httpd_req_t *req) { + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_send(req, "Session started", -1); sessionManager->startMeasurements(); Serial.println("Started session"); }; auto cbStopSession = [sessionManager](httpd_req_t *req) { + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); httpd_resp_send(req, "Session stopped", -1); sessionManager->stopMeasurements(); Serial.println("Stopped session"); }; + auto cbRestart = [](httpd_req_t *req) { + Serial.println("Restarted requested"); + ESP.restart(); + }; + auto cbTare = [sessionManager](httpd_req_t *req) { + Serial.println("Tare"); + sessionManager->tare(); + }; auto cbStatus = [sessionManager](httpd_req_t *req) { String result; result.reserve(512); @@ -50,6 +60,12 @@ void httpSetup(SessionManager *sessionManager) else result += "{},\n"; } + // scale + { + const String tareOffset(sessionManager->tareOffset()); + const String divider(CONFIG_VALUE_DIVIDER); + result += "\"scale\": { \"tare_offset\": " + tareOffset + ", \"divider\":" + divider + "},\n"; + } // flash { const String usedBytes(portablefs::usedBytes()); @@ -74,12 +90,18 @@ void httpSetup(SessionManager *sessionManager) auto cbGetData = [sessionManager](httpd_req_t *req) { auto sessionId = sessionManager->session().getStartTime(); uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0); - Serial.printf("Data request, start index: %d", startIdx); + //Serial.printf("Data request, start index: %d\n", startIdx); + if (startIdx >= sessionManager->session().numMeasurements()) + { + httpd_resp_send_404(req); + return; + } //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()); + String contentDisp = "attachment; filename=\"" + String(sessionId) + ".st\""; + httpd_resp_set_hdr(req, "content-disposition", contentDisp.c_str()); //data StreamingMsgPackEncoder encoderToDetermineSize(nullptr); @@ -104,7 +126,8 @@ void httpSetup(SessionManager *sessionManager) espHttpServer.on("/api/session/data", HTTP_GET, cbGetData); espHttpServer.on("/api/status", HTTP_GET, cbStatus); - + espHttpServer.on("/api/tare", HTTP_GET, cbTare); + espHttpServer.on("/api/restart", HTTP_GET, cbRestart); auto webdav = webdavHandler("/webdav/", "/dat"); espHttpServer.on("/webdav/*?", HTTP_GET, webdav); espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav);