From 79b61baf71e8513c3d9abd0792facd1c690e8b6f Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 23 May 2022 12:09:45 +0200 Subject: [PATCH] Moved some parts of config.h to the user config file. Also added a constants header. --- .gitignore | 1 + firmware/config.h | 37 ++-------------------------------- firmware/config_user.h.example | 37 +++++++++++++++++++++++++++------- firmware/constants.h | 20 ++++++++++++++++++ firmware/firmware.ino | 30 +++++++++++++++------------ 5 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 firmware/constants.h diff --git a/.gitignore b/.gitignore index 5bf2e9d..3762d44 100755 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ firmware/.DS_Store .DS_Store schematics/#auto_saved_files# schematics/_autosave-oko-weatherstation.sch +firmware.ino.bin diff --git a/firmware/config.h b/firmware/config.h index aeb536e..392fdb7 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -1,20 +1,7 @@ #ifndef __CONFIG_H__ #define __CONFIG_H__ -#define SENSOR_TEMPERATURE 0 -#define SENSOR_HUMIDITY 1 -#define SENSOR_LIGHT 2 -#define SENSOR_WINDSPEED 3 -#define SENSOR_PRESSURE 4 -#define SENSOR_BAT_VOLTAGE 5 -#define SENSOR_ESAVEMODE 6 -#define SENSOR_BATCHARGESTATE 7 - -#define BAT_CHARGE_STATE_CHARGED 2.0 -#define BAT_CHARGE_STATE_CHARGING 1.0 -#define BAT_CHARGE_STATE_NOTCHARGING 0.0 -#define ENERGY_SAVE_MODE_ENABLED 1.0 -#define ENERGY_SAVE_MODE_DISABLED 0.0 +// config general setting and behavior of the weatherstation #define WIFI_AUTOCONNECT_TIMEOUT_S 120 #define WIFI_CONFIG_PORTAL_TIMEOUT_S 120 @@ -25,30 +12,11 @@ #define EMERGENCY_SLEEP_S 172800 // Sleep for 2 days to recover #define RESET_ESP_TIME_INTERVAL_MS 3600000 #define WIND_SENSOR_MEAS_TIME_S 15.0 +#define INITIAL_WEBSERVER_TIME 20 #define ENERGY_SAVING_ITERATIONS 30 #define WIFI_MINIMUM_SIGNAL_QUALITY 10 // percent -// thingiverse anomometer settings: https://www.thingiverse.com/thing:2559929/files -#define ROTOR_LENGTH_CM 8.25 -#define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) -//#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) // configuration example for generalization -//#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) // configuration example for generalization -#define MPS_CORRECT_FACT 9.28 -//#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) // configuration exampe for generalization -#define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) -// only this define is used for calculation, all other before are only used to describe the math v_wind = correction_factor * rotations * 2 * pi * radius / time_of_measurement_in_sec -// and if required the result has t be multiplied by another factor to convert it from m/s -#define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) - -// china aliexpress anemometer settings (calculation unknown) -//#define WINDSPEED_FACTOR 2.4 - -// enable HTTP_CALL_ON_WINDSPEED_EXCEED in config_user.h to enable this feature -#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h -#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S -#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.178.100:3001/button-windspeedexceed?event=click" - #define BAT_LOW_VOLTAGE 3.6 #define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5 @@ -71,7 +39,6 @@ #define BME_CS 10 #define BME_ADDRESS 0x76 -#define INITIAL_WEBSERVER_TIME 20 #define WEB_UPDATER_HTTP_PORT 8080 #endif diff --git a/firmware/config_user.h.example b/firmware/config_user.h.example index b8d4f83..6dbd1b4 100755 --- a/firmware/config_user.h.example +++ b/firmware/config_user.h.example @@ -7,6 +7,11 @@ // Debug output on the serial console #define DEBUG +// Device name +// WARNING: Keep the name short! If your access point did not show up, you +// maybe have a TOO LONG SSID! +String DEVICE_NAME = "weatherstation"; + // Enable/Disable features //#define WEBUPDATER_FEATURE #define INFLUXDB_FEATURE @@ -19,13 +24,13 @@ #define SENSOR_BME280 #define SENSOR_BATTERY //#define BAT_PINS_D34 -// only possible if webupdater is also enabled +// Homebridge Webstat is only possible if webupdater is also enabled //#define HOMEBRIDGE_WEBSTAT // retries to connect after 5 seconds or starts the wifimanager #define SLEEP_IF_NO_WLAN_CONNECTION // Restarts the firmware every n seconds -//#define RESET_ESP_TIMEINTERVAL -//#define HTTP_CALL_ON_WINDSPEED_EXCEED +//#define RESET_ESP_TIMEINTERVAL // BETA STATUS +//#define HTTP_CALL_ON_WINDSPEED_EXCEED // BETA STATUS const float HUMIDITY_FACTOR = 1.0; const float LIGHT_FACTOR = 1.0; @@ -39,15 +44,33 @@ const char *INFLUXDB_USER = "user"; const char *INFLUXDB_PASS = "password"; // InfluxDB2 credentials +/* const char *INFLUXDB_URL = "http://192.168.0.123:3124"; const char *INFLUXDB_ORG = "home_org"; const char *INFLUXDB_BUCKET = "mybucket"; const char *INFLUXDB_TOKEN = "your api token"; +*/ -// Device name -// WARNING: Keep the name short! If your access point did not show up, you -// maybe have a TOO LONG SSID! -String DEVICE_NAME = "devicename"; +// enable HTTP_CALL_ON_WINDSPEED_EXCEED to enable this feature +#define HTTP_CALL_ON_WINDSPEED_EXCEED_MPS 5.0 // 5.0 m/s == 18 km/h +#define HTTP_CALL_ON_WINDSPEED_INTERVAL_S 60 // it's required to be bigger than WIND_SENSOR_MEAS_TIME_S +#define HTTP_CALL_ON_WINDSPEED_URL "http://192.168.178.100:3001/button-windspeedexceed?event=click" + +// anemometer settings, to use enable SENSOR_WIND +// thingiverse anemometer settings: https://www.thingiverse.com/thing:2559929/files +#define ROTOR_LENGTH_CM 8.25 +#define ROTOR_LENGTH_M (ROTOR_LENGTH_CM / 100.0) +//#define ROTOR_LENGTH_KM (ROTOR_LENGTH_M / 1000.0) // configuration example for generalization +//#define SEC_TO_HOUR_FACTOR (60.0 * 60.0) // configuration example for generalization +#define MPS_CORRECT_FACT 9.28 +//#define COUNT_TO_KMH ((TWO_PI * ROTOR_LENGTH_KM / WIND_SENSOR_MEAS_TIME_S) * SEC_TO_HOUR_FACTOR) // configuration exampe for generalization +#define COUNT_TO_MPS (TWO_PI * ROTOR_LENGTH_M / WIND_SENSOR_MEAS_TIME_S) +// only this define is used for calculation, all other before are only used to describe the math v_wind = correction_factor * rotations * 2 * pi * radius / time_of_measurement_in_sec +// and if required the result has t be multiplied by another factor to convert it from m/s +#define WINDSPEED_FACTOR (COUNT_TO_MPS * MPS_CORRECT_FACT) + +// china aliexpress anemometer settings (calculation unknown) +//#define WINDSPEED_FACTOR 2.4 #endif diff --git a/firmware/constants.h b/firmware/constants.h new file mode 100644 index 0000000..6710d2e --- /dev/null +++ b/firmware/constants.h @@ -0,0 +1,20 @@ + +#ifndef __CONSTANTS_H__ +#define __CONSTANTS_H__ + +#define SENSOR_TEMPERATURE 0 +#define SENSOR_HUMIDITY 1 +#define SENSOR_LIGHT 2 +#define SENSOR_WINDSPEED 3 +#define SENSOR_PRESSURE 4 +#define SENSOR_BAT_VOLTAGE 5 +#define SENSOR_ESAVEMODE 6 +#define SENSOR_BATCHARGESTATE 7 + +#define BAT_CHARGE_STATE_CHARGED 2.0 +#define BAT_CHARGE_STATE_CHARGING 1.0 +#define BAT_CHARGE_STATE_NOTCHARGING 0.0 +#define ENERGY_SAVE_MODE_ENABLED 1.0 +#define ENERGY_SAVE_MODE_DISABLED 0.0 + +#endif diff --git a/firmware/firmware.ino b/firmware/firmware.ino index aac9672..f5c05ef 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -10,17 +10,21 @@ #include // WiFiManager from bib manager // Project includes +#include "constants.h" #include "config.h" +#include "config_user.h" + +//*************************************************************************// +// check if some settings are correct #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED -#if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) +#if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S) #error "HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S" #endif #endif -#include "config_user.h" - //*************************************************************************// +// constant variables const uint8_t VALUES = 8; float currentSensorData[VALUES] = {nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value"), nanf("no value")}; @@ -66,6 +70,7 @@ void setup() #ifdef BATTERY_POWERED criticalBatCheck(); #endif + // Establish WiFi connection String wifiName = "oko-weather-" + DEVICE_NAME; @@ -181,7 +186,6 @@ void criticalBatCheck() } #endif - void loop() { #ifdef BATTERY_POWERED @@ -189,14 +193,7 @@ void loop() return; #endif -#ifdef WEBUPDATER_FEATURE - if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) - { - update_webserver_cnt = 0; - doWebUpdater(); - } -#endif - + // call sub loop function _loop(); //Needed to give WIFI time to function properly @@ -215,6 +212,14 @@ void loop() void _loop() { +#ifdef WEBUPDATER_FEATURE + if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) + { + update_webserver_cnt = 0; + doWebUpdater(); + } +#endif + #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_windspeed_exceed_cnt) { @@ -256,7 +261,6 @@ void _loop() { #endif update_sensor_cnt = 0; - for (uint8_t i = 0; i < VALUES; i++) { if (sensors[i]) { currentSensorData[i] = sensors[i]();