More configuration options

- wifi provisioning
- auto start/stop of sessions
This commit is contained in:
Martin Bauer
2020-09-03 14:10:52 +02:00
parent 92e7399fe4
commit 041031709f
12 changed files with 547 additions and 157 deletions

View File

@@ -4,7 +4,7 @@
#include <functional>
#include "Dtypes.h"
constexpr int MAX_URI_HANDLERS = 20;
constexpr int MAX_URI_HANDLERS = 25;
esp_err_t rawCallback(httpd_req_t *req);

View File

@@ -2,32 +2,23 @@
#include "SwimTrackerConfig.h"
#include <cstdint>
template <int DIVIDER = 128>
class Scale
{
public:
bool measure(uint16_t &measurementOut)
{
//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 {
// long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
//
// Serial.printf("Measurement failed %ld\n", value);
// return false;
//}
long value = hx711_.read_average(CONFIG_MEASUREMENT_AVG_COUNT) - offset_;
if (value < 0)
measurementOut = (int16_t)(-(value >> valueRightShift_ ));
else
measurementOut = 0;
return true;
}
void begin(uint32_t pinDOUT, uint32_t pinSCK)
void begin(uint32_t pinDOUT, uint32_t pinSCK, int valueRightShift)
{
hx711_.begin(pinDOUT, pinSCK);
valueRightShift_ = valueRightShift;
};
void tare(uint32_t numMeasurementsToAverage = 50)
@@ -36,10 +27,11 @@ public:
offset_ = hx711_.read_average(numMeasurementsToAverage);
Serial.printf("Tare offset %ld\n", offset_);
}
const long &offset() const { return offset_; }
const long &offset() const { return offset_; }
int valueRightShift() const { return valueRightShift_; }
private:
HX711 hx711_;
long offset_ = 0;
int valueRightShift_;
};

View File

@@ -0,0 +1,79 @@
#pragma once
template <typename MeasurementT>
class AutoStart
{
public:
void begin(MeasurementT minThreshold, MeasurementT maxThreshold, uint32_t maxNumMeasurementsBetweenPeeks)
{
minThreshold_ = minThreshold;
maxThreshold_ = maxThreshold;
maxNumMeasurementsBetweenPeeks_ = maxNumMeasurementsBetweenPeeks;
measurementsSinceLastPeek_ = maxNumMeasurementsBetweenPeeks + 1;
up_ = false;
}
bool autoStart(MeasurementT measurement)
{
measurementsSinceLastPeek_ += 1;
if (!up_ && measurement > maxThreshold_)
{
up_ = true;
bool doAutoStart = measurementsSinceLastPeek_ < maxNumMeasurementsBetweenPeeks_;
measurementsSinceLastPeek_ = 0;
return doAutoStart;
}
if (up_ && measurement < minThreshold_)
up_ = false;
return false;
}
private:
MeasurementT minThreshold_;
MeasurementT maxThreshold_;
uint32_t maxNumMeasurementsBetweenPeeks_;
// time of last peak
uint32_t measurementsSinceLastPeek_;
// if measurements have been below min threshold (false) or above max threshold (true)
bool up_;
};
// ------------------------------------------------------------------------------------------
template <typename MeasurementT>
class AutoStop
{
public:
void begin(MeasurementT threshold, uint32_t numMeasurementsBelowThreshold)
{
threshold_ = threshold;
numMeasurementsBelowThreshold_ = numMeasurementsBelowThreshold;
counter_ = 0;
}
bool autoStop(MeasurementT measurement)
{
if (measurement > threshold_)
{
counter_ = 0;
return false;
}
else
{
++counter_;
if (counter_ >= numMeasurementsBelowThreshold_)
{
counter_ = 0;
return true;
}
return false;
}
}
private:
MeasurementT threshold_;
uint32_t numMeasurementsBelowThreshold_;
uint32_t counter_;
};

View File

@@ -4,18 +4,23 @@
#include <WiFiUdp.h>
#include <NTPClient.h>
#include "AutoStartStop.h"
template <typename SessionT>
class SessionManager
{
public:
using MeasurementType = typename SessionT::MeasurementType;
SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount);
void begin();
SessionManager();
void begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift, MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
MeasurementType autoStopTh, uint32_t autoStopNumMeasures);
void tare();
long tareOffset() const { return scale_.offset(); };
int valueRightShift() const { return scale_.valueRightShift(); }
void startMeasurements();
void stopMeasurements();
bool isMeasuring() const { return measuring_; }
@@ -24,10 +29,12 @@ public:
void iteration();
private:
void onMeasurementTaken(MeasurementType measurement);
WiFiUDP ntpUDP_;
NTPClient timeClient_;
Scale<CONFIG_VALUE_DIVIDER> scale_;
Scale scale_;
//MockScale scale;
SessionT session_;
bool measuring_;
@@ -36,45 +43,55 @@ private:
int scaleDoutPin_;
int scaleSckPin_;
uint8_t tareAvgCount_;
int valueRightShift_;
AutoStart<MeasurementT> autoStart_;
AutoStop<MeasurementT> autoStop_;
};
// ------------------------------------------------------------------------------------------------
template <typename SessionT>
SessionManager<SessionT>::SessionManager(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount)
SessionManager<SessionT>::SessionManager()
: timeClient_(ntpUDP_, "pool.ntp.org"),
measuring_(false),
lastCallTime_(0),
scaleDoutPin_(scaleDoutPin),
scaleSckPin_(scaleSckPin),
tareAvgCount_(tareAvgCount)
lastCallTime_(0)
{
}
template <typename SessionT>
void SessionManager<SessionT>::tare()
{
if(measuring_)
if (measuring_)
stopMeasurements();
Serial.println("Beginning tare");
scale_.begin(scaleDoutPin_, scaleSckPin_);
scale_.begin(scaleDoutPin_, scaleSckPin_, valueRightShift_);
scale_.tare(CONFIG_TARE_AVG_COUNT);
Serial.println("Finished tare");
}
template <typename SessionT>
void SessionManager<SessionT>::begin()
void SessionManager<SessionT>::begin(int scaleDoutPin, int scaleSckPin, uint8_t tareAvgCount, int valueRightShift,
MeasurementType autoStartMinTh, MeasurementType autoStartMaxTh, uint32_t autoStartMeasuresBetweenPeaks,
MeasurementType autoStopTh, uint32_t autoStopNumMeasures)
{
scaleDoutPin_ = scaleDoutPin;
scaleSckPin_ = scaleSckPin;
tareAvgCount_ = tareAvgCount;
timeClient_.begin();
timeClient_.update();
tare();
session_.init(timeClient_.getEpochTime());
autoStart_.begin(autoStartMinTh, autoStartMaxTh, autoStartMeasuresBetweenPeaks);
autoStop_.begin(autoStopTh, autoStopNumMeasures);
}
template <typename SessionT>
void SessionManager<SessionT>::startMeasurements()
{
if(measuring_ == true)
if (measuring_ == true)
return;
measuring_ = true;
lastCallTime_ = 0;
@@ -84,23 +101,25 @@ void SessionManager<SessionT>::startMeasurements()
template <typename SessionT>
void SessionManager<SessionT>::stopMeasurements()
{
if(measuring_ == false)
if (measuring_ == false)
return;
session_.finalize();
measuring_ = false;
}
/*
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{
if (!measuring_) {
if (!measuring_)
{
delay(1);
return; // give control to HTTP server thread
}
uint16_t measurement = -1;
MeasurementType measurement = -1;
bool measurementDone = false;
while(!measurementDone)
while (!measurementDone)
measurementDone = scale_.measure(measurement);
bool addPointSuccessful = session_.addPoint(measurement);
//Serial.printf("Measured: %d\n", measurement);
@@ -130,4 +149,68 @@ void SessionManager<SessionT>::iteration()
}
}
lastCallTime_ = millis();
}
}
*/
template <typename SessionT>
void SessionManager<SessionT>::iteration()
{
MeasurementType measurement = -1;
bool measurementDone = false;
while (!measurementDone)
measurementDone = scale_.measure(measurement);
onMeasurementTaken(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("Measurements skipped: %ld, cycleDuration %ld\n", skipped, cycleDuration);
for (int i = 0; i < skipped; ++i)
onMeasurementTaken(measurement);
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
}
}
lastCallTime_ = millis();
}
template <typename SessionT>
void SessionManager<SessionT>::onMeasurementTaken(MeasurementType measurement)
{
if (measuring_)
{
bool autoStop = autoStop_.autoStop(measurement);
if (autoStop)
{
Serial.println("Auto stop");
stopMeasurements();
return;
}
bool addPointSuccessful = session_.addPoint(measurement);
if (!addPointSuccessful)
{
Serial.println("Maximum time of session reached - stopping");
stopMeasurements();
return;
}
}
else
{
if (autoStart_.autoStart(measurement))
{
Serial.println("Auto start");
startMeasurements();
return;
}
}
}

View File

@@ -0,0 +1,105 @@
#include "WifiManager.h"
void WifiManager::begin(const String &hostname)
{
hostname_ = hostname;
prefs_.begin("st_wifi_manager");
startWifi();
}
void WifiManager::startWifi()
{
WiFi.disconnect();
const String apPassword = prefs_.getString("apPassword", "");
const String staSSID = prefs_.getString("staSSID", "");
const String staPassword = prefs_.getString("staPassword", "");
if (staSSID.length() > 0 && staPassword.length() > 0)
{
// station mode
WiFi.mode(WIFI_STA);
WiFi.begin(staSSID.c_str(), staPassword.c_str());
WiFi.setHostname(hostname_.c_str());
int connectCounter = 0;
bool successful = true;
while (WiFi.status() != WL_CONNECTED)
{
delay(2000);
WiFi.begin(staSSID.c_str(), staPassword.c_str());
connectCounter += 1;
if (connectCounter >= 60) // for two minutes no connection
{
successful = false;
break; // fallback to AP mode
}
}
state_ = STA;
if (successful)
return;
}
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(192, 168, 42, 1), IPAddress(192, 168, 42, 1), IPAddress(255, 255, 255, 0));
WiFi.softAPsetHostname(hostname_.c_str());
if (apPassword.length() > 0)
{
WiFi.softAP(hostname_.c_str(), apPassword.c_str());
state_ = AP_SECURE;
}
else
{
WiFi.softAP(hostname_.c_str());
state_ = AP_PROVISIONING;
}
}
void WifiManager::setStaCredentials(const char *wifiName, const char *password)
{
assert(state_ != INVALID);
prefs_.putString("staSSID", wifiName);
prefs_.putString("staPassword", password);
}
void WifiManager::setApCredentials(const char *password)
{
assert(state_ != INVALID);
prefs_.remove("staSSID");
prefs_.remove("staPassword");
prefs_.putString("apPassword", password);
}
void WifiManager::resetToApProvisioning()
{
prefs_.remove("apPassword");
prefs_.remove("staSSID");
prefs_.remove("staPassword");
}
void WifiManager::wifiWatchdog()
{
if (state_ == STA && WiFi.status() != WL_CONNECTED) {
startWifi();
Serial.println("Connection lost - Restarting WIFI");
}
}
const char * WifiManager::stateToString(WifiManager::State state)
{
switch (state)
{
case INVALID:
return "INVALID";
case STA:
return "STATION_MODE";
case AP_PROVISIONING:
return "AP_PROVISIONING";
case AP_SECURE:
return "AP_SECURE";
default:
return "";
};
}

View File

@@ -0,0 +1,51 @@
#pragma once
#include "Preferences.h"
#include <WiFi.h>
/**
* Manages connection and provisioning of WiFI
*
* Starts in state AP_PROVISIONING, where device is an access point without password.
* Device should not work normally in this mode, only API is served to set password.
* Two options:
* - connect to existing WiFi (station mode), via setStaCredentials
* - create secured access point, via setApCredentials
*
* When operating in access point mode, the device IP is 192.168.42.1
*
* call wifiWatchdog regularly to reconnect in station mode if connection was lost
*/
class WifiManager
{
public:
enum State
{
INVALID,
STA,
AP_PROVISIONING,
AP_SECURE,
};
WifiManager() : state_(INVALID) {}
void begin(const String &hostname);
void setStaCredentials(const char *wifiName, const char *password);
void setApCredentials(const char *password);
void resetToApProvisioning();
void wifiWatchdog();
bool inProvisioningMode() const { return state_ == INVALID || state_ == AP_PROVISIONING; }
State state() const { return state_; }
const char *stateStr() const { return stateToString(state_); }
static const char *stateToString(State state);
private:
void startWifi();
Preferences prefs_;
State state_;
String hostname_;
};