From 7cac6f89f1922f0d4445ce29eff3b3406eb0f200 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Sat, 10 Sep 2022 23:18:52 +0200 Subject: [PATCH] make sampling periode configurable --- WifiControlSensor/BME680_BSEC.ino | 6 ++--- WifiControlSensor/EEPROM.h | 1 + WifiControlSensor/EEPROM.ino | 4 ++- WifiControlSensor/WebServer.ino | 34 ++++++++++++++++++------- WifiControlSensor/WifiControlSensor.ino | 8 +++--- WifiControlSensor/config.h | 4 +-- WifiControlSensor/config_device.h | 2 +- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/WifiControlSensor/BME680_BSEC.ino b/WifiControlSensor/BME680_BSEC.ino index 20817ab..1e895e2 100644 --- a/WifiControlSensor/BME680_BSEC.ino +++ b/WifiControlSensor/BME680_BSEC.ino @@ -16,12 +16,12 @@ generic_33v_300s_28d */ const uint8_t bsec_config_iaq[] = { -#if CONFIG_SAMPLING_PERIODE_MS == 3000 +#if CONFIG_SAMPLING_PERIOD_MS == 3000 #include "config/generic_33v_3s_4d/bsec_iaq.txt" -#elif CONFIG_SAMPLING_PERIODE_MS == 300000 +#elif CONFIG_SAMPLING_PERIOD_MS == 300000 #include "config/generic_33v_300s_4d/bsec_iaq.txt" #else -#error "Unsupport CONFIG_SAMPLING_PERIODE_MS (3000 and 300000 are supported)" +#error "Unsupport CONFIG_SAMPLING_PERIOD_MS (3000 and 300000 are supported)" #endif }; diff --git a/WifiControlSensor/EEPROM.h b/WifiControlSensor/EEPROM.h index ed397c4..399186a 100644 --- a/WifiControlSensor/EEPROM.h +++ b/WifiControlSensor/EEPROM.h @@ -17,6 +17,7 @@ typedef struct productConfig_t { uint32_t dns2; uint8_t channel; char *bssid; + uint32_t samplingPeriod; } productConfig; diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index 8c37df3..f2b6c45 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -24,7 +24,7 @@ int EepromSaveConfig(productConfig &config) { + String(config.ip_mode) + ";" + config.ip + ";" + config.gw + ";" + config.mask + ";" + config.dns + ";" + config.dns2 + ";" + config.channel + ";" - + config.bssid + ";"; + + config.bssid + ";" + config.samplingPeriod + ";"; if (eeprom.length() > CONFIG_EEPROM_SIZE ) return -EMSGSIZE; @@ -104,6 +104,8 @@ void EepromReadConfig(productConfig &config) { readConfElement(&tmpString, i); config.channel = atoi(tmpString); readConfElement(&config.bssid, i); + readConfElement(&tmpString, i); + config.samplingPeriod = atoi(tmpString); } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index ca3e2df..d18dc93 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -27,7 +27,7 @@ void WebHandleRoot() { } server.send(200, "text/html", - "" + "" "

You are connected to " + String(conf.host) + "

" "
" "Sensors" @@ -129,6 +129,10 @@ void WebHandleSetup() { "

" "

(8883 for secure Mqtts)
" "
" + "
" + "Sensor:" + "

" + "
" "
" ""); } @@ -172,7 +176,7 @@ void WebHandleSave() { || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip") || !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns") - || !server.hasArg("dns2") || !server.hasArg("channel") || ! server.hasArg("bssid")) { + || !server.hasArg("dns2") || !server.hasArg("channel") || !server.hasArg("bssid") || !server.hasArg("samplingPeriod")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } @@ -187,16 +191,28 @@ void WebHandleSave() { } } - productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").c_str()), - strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()), - strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(), - server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), - static_cast(mask), static_cast(dns), static_cast(dns2), static_cast(server.arg("channel").toInt()), strdup(server.arg("bssid").c_str()) - }; + productConfig newConf = {BOOTMODE_NORMAL, + strdup(server.arg("ssid").c_str()), + strdup(server.arg("password").c_str()), + strdup(server.arg("host").c_str()), + strdup(server.arg("mqttServer").c_str()), + strdup(server.arg("mqttUser").c_str()), + strdup(server.arg("mqttPasswd").c_str()), + server.arg("mqttPort").toInt(), + server.arg("ip_config").toInt(), + static_cast(ip), + static_cast(gw), + static_cast(mask), + static_cast(dns), + static_cast(dns2), + static_cast(server.arg("channel").toInt()), + strdup(server.arg("bssid").c_str()), + static_cast(server.arg("samplingPeriod").toInt())}; if (EepromSaveConfig(newConf) < 0) { - WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); + WebSendError("Cannot Save configuration ( Credentials too long ?Contains \";\"?)\r\n"); return; } + samplingPeriod = newConf.samplingPeriod; if (WiFi.softAPIP() != IPAddress((uint32_t)0)) { //In STA mode, we can test the AP connection WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str()); diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index f9cb6a7..4239db2 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -69,12 +69,14 @@ float bme680BSECT, bme680BSECP, bme680BSECH, bme680BSECIaq, bme680BSECIaqAcc; int dryness; uint8_t mode; int reconfig = 0; -productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL}; +productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL, CONFIG_SAMPLING_PERIOD_MS}; // Should have less that MAXSUBSCRIPTIONS elements // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; const int gpioObserved[] = CONFIG_OBSERVED_GPIO; const int pwmControlled[] = CONFIG_CONTROLLED_PWM; +uint samplingPeriod = CONFIG_SAMPLING_PERIOD_MS; +uint nbCycle = UINT_MAX - 1; /* Set these to your desired credentials. */ const char *ssid = CONFIG_SSID_NAME; @@ -291,12 +293,12 @@ void setup() } WebSetupServer(mode); } + samplingPeriod = conf.samplingPeriod; #ifdef CONFIG_ENABLE_POWER_SAVE wifi_set_sleep_type(LIGHT_SLEEP_T); #endif } -uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS; void loop() { if (mode == BOOTMODE_OTA) { ArduinoOTA.handle(); @@ -309,7 +311,7 @@ void loop() { delay(CONFIG_WEB_DELAY_MS); nbCycle++; - if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) { + if (nbCycle > samplingPeriod / CONFIG_WEB_DELAY_MS) { std::vector batchInfo; if (!BMP180GetTempAndPressure(temp, pressure)) { SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure); diff --git a/WifiControlSensor/config.h b/WifiControlSensor/config.h index 5132ef1..5eae27d 100644 --- a/WifiControlSensor/config.h +++ b/WifiControlSensor/config.h @@ -9,8 +9,8 @@ #define CONFIG_WEB_DELAY_MS 100 #endif -#ifndef CONFIG_SAMPLING_PERIODE_MS -#define CONFIG_SAMPLING_PERIODE_MS 60000 +#ifndef CONFIG_SAMPLING_PERIOD_MS +#define CONFIG_SAMPLING_PERIOD_MS 60000 #endif #if defined(CONFIG_ENABLE_BME680) && defined(CONFIG_BME680_BSEC_ENABLE) diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h index c9a60ba..8f5280d 100644 --- a/WifiControlSensor/config_device.h +++ b/WifiControlSensor/config_device.h @@ -55,7 +55,7 @@ //#define CONFIG_WEB_DELAY_MS 100 // Get sensors value every X ms -#define CONFIG_SAMPLING_PERIODE_MS 30000 +#define CONFIG_SAMPLING_PERIOD_MS 3000 // Name of the SSID when in AP mode for configuration #define CONFIG_SSID_NAME "ESPConfiguratorBureau"