Added restart/tare to API and various fixes
This commit is contained in:
parent
828e7b3405
commit
7ae434299f
|
@ -4,7 +4,7 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "Dtypes.h"
|
#include "Dtypes.h"
|
||||||
|
|
||||||
constexpr int MAX_URI_HANDLERS = 10;
|
constexpr int MAX_URI_HANDLERS = 20;
|
||||||
|
|
||||||
esp_err_t rawCallback(httpd_req_t *req);
|
esp_err_t rawCallback(httpd_req_t *req);
|
||||||
|
|
||||||
|
|
|
@ -8,17 +8,21 @@ class Scale
|
||||||
public:
|
public:
|
||||||
bool measure(uint16_t &measurementOut)
|
bool measure(uint16_t &measurementOut)
|
||||||
{
|
{
|
||||||
if (hx711_.is_ready())
|
//if (hx711_.is_ready())
|
||||||
{
|
//{
|
||||||
long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
|
long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
|
||||||
if (value < 0)
|
if (value < 0)
|
||||||
measurementOut = (int16_t)(-value / DIVIDER);
|
measurementOut = (int16_t)(-value / DIVIDER);
|
||||||
else
|
else
|
||||||
measurementOut = 0;
|
measurementOut = 0;
|
||||||
return true;
|
return true;
|
||||||
}
|
//}
|
||||||
else
|
//else {
|
||||||
return false;
|
// 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)
|
void begin(uint32_t pinDOUT, uint32_t pinSCK)
|
||||||
|
@ -28,10 +32,12 @@ public:
|
||||||
|
|
||||||
void tare(uint32_t numMeasurementsToAverage = 50)
|
void tare(uint32_t numMeasurementsToAverage = 50)
|
||||||
{
|
{
|
||||||
|
hx711_.read_average(3);
|
||||||
offset_ = hx711_.read_average(numMeasurementsToAverage);
|
offset_ = hx711_.read_average(numMeasurementsToAverage);
|
||||||
|
Serial.printf("Tare offset %ld\n", offset_);
|
||||||
}
|
}
|
||||||
|
|
||||||
long &offset() const { return offset_; }
|
const long &offset() const { return offset_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HX711 hx711_;
|
HX711 hx711_;
|
||||||
|
|
|
@ -13,6 +13,7 @@ public:
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
void tare();
|
void tare();
|
||||||
|
long tareOffset() const { return scale_.offset(); };
|
||||||
void startMeasurements();
|
void startMeasurements();
|
||||||
void stopMeasurements();
|
void stopMeasurements();
|
||||||
bool isMeasuring() const { return measuring_; }
|
bool isMeasuring() const { return measuring_; }
|
||||||
|
@ -51,6 +52,8 @@ SessionManager<SessionT>::SessionManager(int scaleDoutPin, int scaleSckPin, uint
|
||||||
template <typename SessionT>
|
template <typename SessionT>
|
||||||
void SessionManager<SessionT>::tare()
|
void SessionManager<SessionT>::tare()
|
||||||
{
|
{
|
||||||
|
if(measuring_)
|
||||||
|
stopMeasurements();
|
||||||
Serial.println("Beginning tare");
|
Serial.println("Beginning tare");
|
||||||
scale_.begin(scaleDoutPin_, scaleSckPin_);
|
scale_.begin(scaleDoutPin_, scaleSckPin_);
|
||||||
scale_.tare(CONFIG_TARE_AVG_COUNT);
|
scale_.tare(CONFIG_TARE_AVG_COUNT);
|
||||||
|
@ -69,6 +72,8 @@ void SessionManager<SessionT>::begin()
|
||||||
template <typename SessionT>
|
template <typename SessionT>
|
||||||
void SessionManager<SessionT>::startMeasurements()
|
void SessionManager<SessionT>::startMeasurements()
|
||||||
{
|
{
|
||||||
|
if(measuring_ == true)
|
||||||
|
return;
|
||||||
measuring_ = true;
|
measuring_ = true;
|
||||||
lastCallTime_ = 0;
|
lastCallTime_ = 0;
|
||||||
session_.init(timeClient_.getEpochTime());
|
session_.init(timeClient_.getEpochTime());
|
||||||
|
@ -77,6 +82,8 @@ void SessionManager<SessionT>::startMeasurements()
|
||||||
template <typename SessionT>
|
template <typename SessionT>
|
||||||
void SessionManager<SessionT>::stopMeasurements()
|
void SessionManager<SessionT>::stopMeasurements()
|
||||||
{
|
{
|
||||||
|
if(measuring_ == false)
|
||||||
|
return;
|
||||||
session_.finalize();
|
session_.finalize();
|
||||||
measuring_ = false;
|
measuring_ = false;
|
||||||
}
|
}
|
||||||
|
@ -88,8 +95,11 @@ void SessionManager<SessionT>::iteration()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uint16_t measurement = -1;
|
uint16_t measurement = -1;
|
||||||
scale_.measure(measurement);
|
bool measurementDone = false;
|
||||||
|
while(!measurementDone)
|
||||||
|
measurementDone = scale_.measure(measurement);
|
||||||
bool addPointSuccessful = session_.addPoint(measurement);
|
bool addPointSuccessful = session_.addPoint(measurement);
|
||||||
|
Serial.printf("Measured: %d\n", measurement);
|
||||||
if (!addPointSuccessful)
|
if (!addPointSuccessful)
|
||||||
{
|
{
|
||||||
Serial.println("Maximum time of session reached - stopping");
|
Serial.println("Maximum time of session reached - stopping");
|
||||||
|
|
|
@ -77,6 +77,10 @@ private:
|
||||||
if (portablefs::exists(filename.c_str()))
|
if (portablefs::exists(filename.c_str()))
|
||||||
{
|
{
|
||||||
auto file = portablefs::open(filename.c_str(), "a");
|
auto file = portablefs::open(filename.c_str(), "a");
|
||||||
|
file.seek(0, SeekSet);
|
||||||
|
StreamingMsgPackEncoder<portablefs::File> encoder(&file);
|
||||||
|
chunk->sendHeader(encoder, chunk->getStartTime(), 0);
|
||||||
|
|
||||||
file.seek(0, SeekEnd);
|
file.seek(0, SeekEnd);
|
||||||
size_t existingMeasurements = (file.size() - ChunkT::valueOffset()) / sizeof(Measurement_T);
|
size_t existingMeasurements = (file.size() - ChunkT::valueOffset()) / sizeof(Measurement_T);
|
||||||
Serial.printf("Incremental save, existing %d\n", existingMeasurements);
|
Serial.printf("Incremental save, existing %d\n", existingMeasurements);
|
||||||
|
|
|
@ -3,25 +3,32 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
// Uncomment for Version 2.0 where load cell is connected differently
|
||||||
|
#define _HW_V_20
|
||||||
|
//#define NEW_HEAVY_LOAD_CELL
|
||||||
|
|
||||||
// ------------------------------------------ WiFi ---------------------------------------------------------------------------------
|
// ------------------------------------------ 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_WIFI_PASSWORD = "Bau3rWLAN";
|
||||||
const char *CONFIG_HOSTNAME = "smartswim";
|
const char *CONFIG_HOSTNAME = "smartswim";
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------- Hardware & Measurement Settings ------------------------------------------------------------
|
// ------------------------------------- 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_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_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 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
|
const char *CONFIG_DATA_PATH = "/dat"; // folder in SPIFFS file system to store measurement data
|
||||||
using MeasurementT = uint16_t; // data type for one measurement
|
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 -----------------------------------------------------------------------------
|
// ------------------------------------- Derived Settings -----------------------------------------------------------------------------
|
||||||
|
|
|
@ -24,15 +24,25 @@ template <typename SessionT>
|
||||||
void httpSetup(SessionManager<SessionT> *sessionManager)
|
void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||||
{
|
{
|
||||||
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||||
httpd_resp_send(req, "Session started", -1);
|
httpd_resp_send(req, "Session started", -1);
|
||||||
sessionManager->startMeasurements();
|
sessionManager->startMeasurements();
|
||||||
Serial.println("Started session");
|
Serial.println("Started session");
|
||||||
};
|
};
|
||||||
auto cbStopSession = [sessionManager](httpd_req_t *req) {
|
auto cbStopSession = [sessionManager](httpd_req_t *req) {
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||||
httpd_resp_send(req, "Session stopped", -1);
|
httpd_resp_send(req, "Session stopped", -1);
|
||||||
sessionManager->stopMeasurements();
|
sessionManager->stopMeasurements();
|
||||||
Serial.println("Stopped session");
|
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) {
|
auto cbStatus = [sessionManager](httpd_req_t *req) {
|
||||||
String result;
|
String result;
|
||||||
result.reserve(512);
|
result.reserve(512);
|
||||||
|
@ -50,6 +60,12 @@ void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||||
else
|
else
|
||||||
result += "{},\n";
|
result += "{},\n";
|
||||||
}
|
}
|
||||||
|
// scale
|
||||||
|
{
|
||||||
|
const String tareOffset(sessionManager->tareOffset());
|
||||||
|
const String divider(CONFIG_VALUE_DIVIDER);
|
||||||
|
result += "\"scale\": { \"tare_offset\": " + tareOffset + ", \"divider\":" + divider + "},\n";
|
||||||
|
}
|
||||||
// flash
|
// flash
|
||||||
{
|
{
|
||||||
const String usedBytes(portablefs::usedBytes());
|
const String usedBytes(portablefs::usedBytes());
|
||||||
|
@ -74,12 +90,18 @@ void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||||
auto cbGetData = [sessionManager](httpd_req_t *req) {
|
auto cbGetData = [sessionManager](httpd_req_t *req) {
|
||||||
auto sessionId = sessionManager->session().getStartTime();
|
auto sessionId = sessionManager->session().getStartTime();
|
||||||
uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0);
|
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
|
//headers
|
||||||
httpd_resp_set_hdr(req, "Content-Type", "application/x-msgpack");
|
httpd_resp_set_hdr(req, "Content-Type", "application/x-msgpack");
|
||||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
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
|
//data
|
||||||
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
||||||
|
@ -104,7 +126,8 @@ void httpSetup(SessionManager<SessionT> *sessionManager)
|
||||||
|
|
||||||
espHttpServer.on("/api/session/data", HTTP_GET, cbGetData);
|
espHttpServer.on("/api/session/data", HTTP_GET, cbGetData);
|
||||||
espHttpServer.on("/api/status", HTTP_GET, cbStatus);
|
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");
|
auto webdav = webdavHandler("/webdav/", "/dat");
|
||||||
espHttpServer.on("/webdav/*?", HTTP_GET, webdav);
|
espHttpServer.on("/webdav/*?", HTTP_GET, webdav);
|
||||||
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav);
|
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdav);
|
||||||
|
|
Loading…
Reference in New Issue