update confg + ota

This commit is contained in:
2022-01-03 17:14:48 +01:00
parent c340ab9d2a
commit f2eeba9b73
12 changed files with 170 additions and 48 deletions

View File

@@ -10,5 +10,15 @@
{
"TagUID" : "23 0E 2C 19",
"audiofile" : "/ringoffire.mp3"
}]
}],
"WiFi": [
{
"ssid": "poes",
"psk": "Rijnstraat214"
},
{
"ssid": "wifi2",
"psk": "pass2"
}
]
}

View File

@@ -21,4 +21,6 @@ extra_scripts = ./littlefsbuilder.py
build_flags =
-DHARDWARE=2
-DCORE_DEBUG_LEVEL=4
-DNDEF_DEBUG=1
-DNDEF_DEBUG=1
#upload_protocol = espota
#upload_port = muziekdoos.local

View File

@@ -4,8 +4,8 @@
#include "LITTLEFS.h"
#include "ArduinoJson.h"
const char *tagConfigfile = "/tagconfig.json";
DynamicJsonDocument tagDoc(512);
const char *tagConfigfile = "/settings.json";
DynamicJsonDocument settingsDoc(512);
bool configInitOK = false;
struct tagConfig
@@ -16,21 +16,53 @@ struct tagConfig
std::vector<tagConfig> tags;
bool checkTagConfig(void)
{
JsonArray array = settingsDoc["tags"].as<JsonArray>();
log_i("JsonArray size = %d", array.size());
bool hasError = checkTagConfig();
return hasError;
}
String GetWifiPassword(String ssid)
{
log_i("Get wifi password for ssid=%s", ssid);
if(ssid == "") return "";
JsonArray array = settingsDoc["WiFi"].as<JsonArray>();
for (JsonVariant v : array)
{
String netname((const char*)v["ssid"]);
uint16_t result = ssid.compareTo(netname);
log_v("compare %s(config) with %s(read) = %d",netname.c_str(), ssid.c_str(), result);
if (!result)
{
String password((const char*)v["psk"]);
log_i("ssid found in config", password.c_str());
return password;
}
}
log_e("password for %s not found",ssid.c_str() );
return "";
}
void loadConfig(const char *fname)
{
log_i("config: load");
File file = LITTLEFS.open(fname);
DeserializationError error = deserializeJson(tagDoc, file);
DeserializationError error = deserializeJson(settingsDoc, file);
if (error)
{
log_e("Failed to deserialize err=%s", error.c_str());
}
else
{
serializeJsonPretty(tagDoc, Serial);
serializeJsonPretty(settingsDoc, Serial);
JsonArray array = tagDoc["tags"].as<JsonArray>();
JsonArray array = settingsDoc["tags"].as<JsonArray>();
log_i("JsonArray size = %d", array.size());
bool hasError = false;
for (JsonVariant v : array)
@@ -65,7 +97,7 @@ void handleConfig(void)
String getConfigSong(String uid)
{
JsonArray array = tagDoc["tags"].as<JsonArray>();
JsonArray array = settingsDoc["tags"].as<JsonArray>();
for (JsonVariant v : array)
{
String taguid((const char*)v["TagUID"]);

View File

@@ -3,6 +3,8 @@
#include "Arduino.h"
String getConfigSong(String uid);
String GetWifiPassword(String ssid);
void initConfig(void);
void handleConfig(void);

View File

@@ -28,7 +28,7 @@ void SetLedColor(CRGB color, bool blink)
void initLed(void)
{
FastLED.addLeds<SK6812, LED_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
FastLED.setBrightness(80);
FastLED.setBrightness(40);
}
void handleLed(void)

View File

@@ -2,37 +2,84 @@
OtaProcess_class ota(100);
bool OtaProcess_class::initialize(void)
{
if (m_newState)
{
log_i("Otastate = initialize");
m_newState = false;
m_otaState = otaInit;
m_otaState = otaScan;
}
switch (m_otaState)
{
case otaInit:
log_i("Otastate = initialize(init)");
WiFi.mode(WIFI_STA);
WiFi.begin(SECRET_SSID, SECRET_PASS);
m_otaState = otaConnect;
break;
case otaConnect:
log_i("Otastate = initialize(connect)");
case otaScan:
{
log_i("Otastate = initialize(scan)");
if (WiFi.waitForConnectResult() != WL_CONNECTED)
int n = WiFi.scanNetworks();
if (n == 0)
{
log_e("Connection Failed! Retry...");
log_e("no networks found");
m_otaState = otaError;
}
else
{
m_otaState = otaSetup;
log_i(" %d wifi networks found", n);
String tmppsk = "";
for (int i = 0; i < n; ++i)
{
tmppsk = GetWifiPassword(WiFi.SSID(i));
if(tmppsk != "" && m_ssid == "")
{
m_ssid = WiFi.SSID(i);
m_psk = tmppsk;
}
log_i("[%d] %s %s (%d) [%s]", i, WiFi.SSID(i), (WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*", WiFi.RSSI(i), (tmppsk != "")? "OK": "Not congigured");
}
}
break;
if(m_ssid != "")
{
m_otaState = otaInit;
log_i("Otastate = initialize(scan): done");
}
else
{
m_otaState = otaError;
log_e("Otastate = initialize(scan): NOT CONFIGURED");
}
}
break;
case otaInit:
{
log_i("Otastate = initialize(init)");
WiFi.begin(m_ssid.c_str(), m_psk.c_str());
m_otaState = otaConnect;
log_i("Otastate = initialize(init):done");
}
break;
case otaConnect:
{
log_i("Otastate = initialize(connect)");
uint32_t timeTemp = millis();
if (m_lastconnectTime - timeTemp > WIFICONNECTINTERVAL)
{
if (WiFi.status() != WL_CONNECTED)
{
log_e("Connection Failed! Retry...");
}
else
{
m_otaState = otaSetup;
}
m_lastconnectTime = timeTemp;
}
}
break;
case otaSetup:
{
@@ -111,7 +158,6 @@ void OtaProcess_class::idle(void)
}
}
void OtaProcess_class::active(void)
{
if (m_newState)
@@ -122,7 +168,6 @@ void OtaProcess_class::active(void)
idle();
}
void OtaProcess_class::disabled(void)
{
if (m_newState)
@@ -168,7 +213,7 @@ void otaDisable(void)
void initOta(void)
{
/* noting */
/* noting */
}
OTASTATES getOtaState(void)

View File

@@ -3,11 +3,15 @@
#include "secrets.h"
#include "defines.h"
#include "process.h"
#include "config.h"
#include "LITTLEFS.h"
#define WIFICONNECTINTERVAL 1000
typedef enum{
otaInit,
otaScan,
otaConnect,
otaSetup,
otaInitDone,
@@ -18,8 +22,9 @@
}OTASTATES;
class OtaProcess_class : public processClass
{
uint32_t m_lastconnectTime;
String m_psk = "";
String m_ssid = "";
void active(void);
bool initialize(void);

View File

@@ -7,6 +7,7 @@ uint32_t PowerOtaLongPressTime = 0;
uint64_t measure_timer = 0;
POWERSTATES powerstate = off;
POWERSTATES lastState = off;
Button buttonPower(PWR_BTN, 250UL, 1U, 0);
extern OtaProcess_class ota;
@@ -116,14 +117,14 @@ void handlePowerState(void)
break;
case poweringOn2:
{
if (!buttonread)
if (buttonPower.releasedFor(200))
{
powerstate = powerinit;
powerOn();
// if (measureBattery())
// {
// log_w("poweringOn: Lowbat");
// powerstate = lowBatt;
// //powerstate = lowBatt;
// }
}
else
@@ -148,13 +149,14 @@ void handlePowerState(void)
{
if (buttonPower.pressedFor(100))
{
lastState = on;
powerstate = poweringOff;
break;
}
// if (handleBattery())
// {
// log_w("on: Lowbat");
// powerstate = lowBatt;
// //powerstate = lowBatt;
// break;
// }
}
@@ -181,13 +183,13 @@ void handlePowerState(void)
}
else
{
powerstate = on;
powerstate = lastState;
}
}
break;
case poweringOff2:
{
if (!buttonread)
if (!buttonPower.releasedFor(200))
{
powerstate = off;
SetLedColor(CRGB::Red, true);
@@ -211,19 +213,32 @@ void handlePowerState(void)
log_w("lowbatt");
}
}
break;
case overTheAir:
{
if (!buttonread)
if (buttonPower.releasedFor(200))
{
powerstate = overTheAir2;
otaEnable();
SetLedColor(CRGB::Blue);
SetLedColor(CRGB::Blue, true);
powerOn();
}
log_i("ota state active, release powerbutton");
else
{
SetLedColor(CRGB::Blue, false);
log_i("ota state active, release powerbutton");
}
}
break;
case overTheAir2:
{
if(buttonPower.pressedFor(200))
{
log_i("ota poweroff");
lastState = overTheAir2;
powerstate = poweringOff;
}
if (getOtaState() == OTASTATES::otaBusy)
{
SetLedColor(CRGB::Blue, true);
@@ -234,9 +249,14 @@ void handlePowerState(void)
{
log_i("ota state active, ota Done ==> On");
powerstate = POWERSTATES::on;
powerstate = POWERSTATES::off;
SetLedColor(CRGB::Green, true);
}
if (getOtaState() == OTASTATES::otaError)
{
powerstate = POWERSTATES::off;
SetLedColor(CRGB::Red, true);
}
}
break;
}

View File

@@ -13,7 +13,7 @@
#define TIMEOUT_POWER (5 * 1000 * 60) //5minutes timeout
#define POWERBUTTONDELAY 1000
#define BATTERYMEASUREDELAY 60000
#define POWERBUTTONOTADELAY 10000
#define POWERBUTTONOTADELAY 7000
typedef enum
{

View File

@@ -40,16 +40,17 @@ void handleSensor(void)
{
uint32_t timeNow = millis();
if (lastVbatt - timeNow > VBATTINTERVALL - VBATTMEASPRECHARGE)
if (timeNow - lastVbatt > (VBATTINTERVALL - VBATTMEASPRECHARGE))
{
digitalWrite(MEAS_EN, HIGH);
digitalWrite(MEAS_EN, LOW);
log_v("precharge vbatt measurement");
}
if (lastVbatt - timeNow > VBATTINTERVALL)
if (timeNow - lastVbatt > VBATTINTERVALL)
{
BatterySensor = ADS.readADC(MEAS_ADC);
digitalWrite(MEAS_EN, LOW);
digitalWrite(MEAS_EN, HIGH);
log_i("read vbatt %4.2f",ADS.toVoltage(BatterySensor));
lastVbatt = timeNow;
}
@@ -88,6 +89,11 @@ void handleHallSensor(void)
last_hall_Delta = hall_delta;
if(skipfirstSample)
{
log_v("First sample skipped");
if(hall_idle_count)
{
hall_idle_count --;
}
return;
}
if (hall_delta > HALLIDLETHRESHOLD)

View File

@@ -4,13 +4,13 @@
#include "board.h"
#define ADSINTERVAL 100
#define VBATTINTERVALL 1000
#define VBATTMEASPRECHARGE 250
#define VBATTINTERVALL 6000
#define VBATTMEASPRECHARGE 240
#define HALLINTERVAL 100
#define HALLIDLETHRESHOLD 5
#define HALLIDLESAMPLES 4
#define HALLPLAYSAMPLES 8
#define HALLIDLETHRESHOLD 20
#define HALLIDLESAMPLES 8
#define HALLPLAYSAMPLES 16
void initSensor(void);