2022-05-09 09:42:42 +02:00
// Standard ESP8266 libs from project folder
2018-06-23 21:49:11 +02:00
# include <ESP8266mDNS.h>
2017-11-07 22:20:14 +01:00
# include <ESP8266WiFi.h>
2023-01-10 12:21:02 +01:00
# include <ESP8266WiFiType.h>
2023-01-09 12:07:01 +01:00
# include <esp.h>
2023-01-12 20:21:34 +01:00
# include <user_interface.h>
2022-05-15 22:34:36 +02:00
# include <WiFiClient.h> // WiFiClient
2019-02-03 16:12:19 +01:00
// Project includes
2022-05-23 12:09:45 +02:00
# include "constants.h"
2017-11-07 22:20:14 +01:00
# include "config.h"
2022-05-23 12:09:45 +02:00
# include "config_user.h"
2023-02-09 17:24:05 +01:00
# ifdef ENABLE_PING_HOST_TEST
# include <ESP8266Ping.h>
# endif
2022-10-31 11:56:44 +01:00
# ifndef DISABLE_WIFIMANAGER
2022-11-11 10:49:42 +01:00
# include <WiFiManager.h> // WiFiManager from bib manager
2022-10-31 11:56:44 +01:00
# endif
2022-11-10 14:00:13 +01:00
# ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
# include <ESP8266HTTPClient.h>
# endif
# ifdef HTTP_CALL_SEND_JSON_DATA
# include <ESP8266HTTPClient.h>
2022-11-11 11:11:10 +01:00
# include <UrlEncode.h> // from bib manager UrlEncode by Masayuki
2022-11-10 14:00:13 +01:00
# endif
2022-05-23 12:09:45 +02:00
//*************************************************************************//
// check if some settings are correct
2019-02-03 16:12:19 +01:00
2022-05-22 08:51:24 +02:00
# ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
2022-05-23 12:09:45 +02:00
# if (HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S)
2022-05-22 08:51:24 +02:00
# error "HTTP_CALL_ON_WINDSPEED_INTERVAL_S < WIND_SENSOR_MEAS_TIME_S"
# endif
# endif
2018-06-24 12:00:51 +02:00
//*************************************************************************//
2022-05-23 12:09:45 +02:00
// constant variables
2018-06-24 12:00:51 +02:00
2022-11-11 10:49:42 +01:00
const uint8_t VALUES = 8 ; // see constants.h file - count of number of SENSOR_ defines
2022-09-12 15:26:30 +02:00
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 " ) } ;
2017-11-07 22:20:14 +01:00
2022-11-11 10:49:42 +01:00
uint32_t update_sensor_cnt = 0 ;
uint32_t update_webserver_cnt = 0 ;
2022-09-12 20:27:42 +02:00
uint32_t update_windspeed_exceed_cnt = 0 ;
2022-09-12 21:44:10 +02:00
uint32_t wifi_check_interval_counter = 0 ;
2022-11-11 10:49:42 +01:00
uint32_t http_call_send_json_data_cnt = 0 ;
2022-08-28 13:47:10 +02:00
2022-11-02 19:10:39 +01:00
# ifndef DISABLE_WIFIMANAGER
2022-09-12 15:26:30 +02:00
const String wifiName = " oko-weather- " + DEVICE_NAME ;
2017-11-12 22:34:08 +01:00
WiFiManager wifiManager ;
2022-10-31 11:56:44 +01:00
# endif
2019-03-31 17:01:04 +02:00
2022-09-14 11:58:13 +02:00
uint8_t fsm_state = FSM_STATE_1 ;
2022-09-15 13:14:58 +02:00
2022-09-12 19:09:43 +02:00
uint8_t sensor_cnt = 0 ;
2022-09-12 15:26:30 +02:00
2022-09-12 21:44:10 +02:00
boolean validData = false ;
2022-09-13 23:18:05 +02:00
boolean do_not_read_windsensor = false ;
2022-09-14 20:05:50 +02:00
uint32_t wifi_reconnect_cnt = 0 ;
2018-06-24 12:00:51 +02:00
//*************************************************************************//
2018-06-23 21:49:11 +02:00
2022-11-11 10:49:42 +01:00
void debug ( String x ) {
2019-08-03 23:17:17 +02:00
# ifdef DEBUG
Serial . println ( x ) ;
# endif
2022-09-12 19:31:16 +02:00
# ifdef USE_LOGGER
logdata ( String ( millis ( ) ) + " : " + x ) ;
# endif
2019-08-03 23:17:17 +02:00
}
2022-09-12 15:26:30 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void setup ( ) {
2019-02-06 21:26:13 +01:00
2023-01-10 12:21:02 +01:00
# ifdef ENABLE_WATCHDOG
//wdt_disable();
# endif
2023-01-12 20:21:34 +01:00
# if defined(DEBUG) || defined(SERIAL_FEATURE) || defined(DEBUG_RESET_REASON)
2022-11-02 19:10:39 +01:00
Serial . begin ( SERIAL_BAUD_RATE ) ;
2018-06-23 20:13:33 +02:00
# endif
2017-11-12 22:34:08 +01:00
// Pin settings
2022-11-11 10:49:42 +01:00
pinMode ( BAT_CHARGED_PIN , INPUT ) ;
pinMode ( BAT_CHARGING_PIN , INPUT ) ;
pinMode ( STATUS_LED_PIN , OUTPUT ) ;
pinMode ( ANEMOMETER_PIN , INPUT_PULLUP ) ;
pinMode ( A0 , INPUT ) ;
2017-11-12 22:34:08 +01:00
2022-11-10 13:49:50 +01:00
digitalWrite ( STATUS_LED_PIN , HIGH ) ;
2018-07-12 22:30:30 +02:00
2020-02-20 19:49:44 +01:00
# ifndef BAT_PINS_D34
debug ( " D5 D6 used as battery pins " ) ;
# else
debug ( " D3 D4 used as battery pins " ) ;
# endif
2019-01-27 18:49:28 +01:00
# ifdef BATTERY_POWERED
2019-01-06 15:21:03 +01:00
criticalBatCheck ( ) ;
2019-01-27 18:49:28 +01:00
# endif
2022-05-23 12:09:45 +02:00
2022-09-12 15:26:30 +02:00
wifiConnect ( ) ;
2019-08-03 23:17:17 +02:00
debug ( " Connected! " ) ;
2017-11-20 20:15:14 +01:00
2022-09-20 08:21:24 +02:00
initWifiBasedSW ( ) ;
2017-11-20 20:42:02 +01:00
2022-09-12 15:26:30 +02:00
initSensors ( ) ;
//It's magic! leave in
delay ( 100 ) ;
2023-02-09 16:24:55 +01:00
# ifdef DEBUG_RESET_REASON
debugResetReason ( ) ;
# endif
2022-09-12 15:26:30 +02:00
# ifdef BATTERY_POWERED
debug ( " battery powered " ) ;
2022-09-14 11:58:13 +02:00
2022-09-14 12:18:58 +02:00
_battery_mode_main ( ) ;
2022-09-12 15:26:30 +02:00
2022-11-10 13:49:50 +01:00
digitalWrite ( STATUS_LED_PIN , HIGH ) ;
2022-09-12 15:26:30 +02:00
criticalBatCheck ( ) ;
WiFi . mode ( WIFI_OFF ) ;
WiFi . forceSleepBegin ( ) ;
debug ( " deep sleep " ) ;
// the ESP.deepSleep requires microseconds as input, after the
// sleep the system will run into the setup routine
ESP . deepSleep ( POWERSAVING_SLEEP_S * 1000000 , WAKE_RF_DEFAULT ) ;
delay ( 100 ) ;
2022-11-11 10:49:42 +01:00
# else // not in battery mode
2022-09-14 12:18:58 +02:00
2022-09-12 15:26:30 +02:00
# ifdef ENABLE_WATCHDOG
2023-01-09 12:07:01 +01:00
wdt_disable ( ) ;
wdt_reset ( ) ;
2022-09-12 15:26:30 +02:00
// Enable the internal watchdog
2023-01-09 12:07:01 +01:00
wdt_enable ( WATCHDOG_TIMEOUT_MS ) ;
debug ( " Watchdog enabled " ) ;
2022-09-12 15:26:30 +02:00
# endif
2022-09-14 11:58:13 +02:00
# endif
2022-09-12 15:26:30 +02:00
}
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void initWifiBasedSW ( ) {
2022-09-20 08:21:24 +02:00
# ifdef INFLUXDB_FEATURE
influxdb_begin ( ) ;
# endif
# ifdef WEBUPDATER_FEATURE
# ifndef BATTERY_POWERED
setupWebUpdater ( DEVICE_NAME , WiFi . localIP ( ) . toString ( ) ) ;
# endif
# endif
}
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void initSensors ( ) {
2017-11-20 20:42:02 +01:00
// Initialize and configure the sensors
2019-08-04 11:56:18 +02:00
# ifdef SENSOR_APDS9930
2022-09-13 23:18:05 +02:00
if ( sensor_apds9930_begin ( ) ;
2019-08-04 11:56:18 +02:00
# endif
2019-08-03 23:17:17 +02:00
2019-08-04 11:56:18 +02:00
# ifdef SENSOR_APDS9960
2022-09-13 23:18:05 +02:00
sensor_apds9960_begin ( ) ;
2019-08-04 11:56:18 +02:00
# endif
2019-08-03 23:17:17 +02:00
2019-08-04 11:56:18 +02:00
# ifdef SENSOR_BME280
2022-11-02 19:10:39 +01:00
//Temperature + pressure + humidity
2022-09-13 23:18:05 +02:00
sensor_bme280_begin ( BME_ADDRESS ) ;
2019-08-04 11:56:18 +02:00
# endif
2022-11-02 19:10:39 +01:00
# ifdef SENSOR_BMP280
//Temperature + pressure + humidity
sensor_bmp280_begin ( BMP_ADDRESS ) ;
# endif
2022-09-13 23:18:05 +02:00
}
2022-09-14 20:05:50 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
float readSensors ( uint8_t s ) {
2022-09-13 23:18:05 +02:00
float ret = nan ( " no value " ) ;
2019-08-04 11:56:18 +02:00
2022-11-11 10:49:42 +01:00
switch ( s ) {
case SENSOR_LIGHT : // Initialize and configure the sensors
2022-09-13 23:18:05 +02:00
# ifdef SENSOR_APDS9930
ret = apds9930_light ( ) ;
# endif
# ifdef SENSOR_APDS9960
ret = apds9960_light ( ) ;
# endif
break ;
case SENSOR_TEMPERATURE :
# ifdef SENSOR_BME280
ret = bme280_temperature ( ) ;
2022-11-02 19:10:39 +01:00
# endif
# ifdef SENSOR_BMP280
ret = bmp280_temperature ( ) ;
2022-09-13 23:18:05 +02:00
# endif
break ;
case SENSOR_HUMIDITY :
# ifdef SENSOR_BME280
ret = bme280_humidity ( ) ;
# endif
break ;
case SENSOR_PRESSURE :
# ifdef SENSOR_BME280
ret = bme280_pressure ( ) ;
2022-11-02 19:10:39 +01:00
# endif
# ifdef SENSOR_BMP280
ret = bmp280_pressure ( ) ;
2022-09-13 23:18:05 +02:00
# endif
break ;
case SENSOR_WINDSPEED :
2019-08-04 11:56:18 +02:00
# ifdef SENSOR_WIND
2022-11-11 10:49:42 +01:00
if ( do_not_read_windsensor = = false ) {
2022-09-13 23:18:05 +02:00
ret = wind_speed ( ) ;
}
# endif
break ;
case SENSOR_BAT_VOLTAGE :
# ifdef SENSOR_BATTERY
ret = battery_voltage ( ) ;
# endif
break ;
case SENSOR_ESAVEMODE :
# ifdef SENSOR_BATTERY
ret = battery_charging ( ) ;
2019-08-04 11:56:18 +02:00
# endif
2022-09-13 23:18:05 +02:00
break ;
2019-08-04 11:56:18 +02:00
2022-09-13 23:18:05 +02:00
case SENSOR_BATCHARGESTATE :
2019-08-04 11:56:18 +02:00
# ifdef SENSOR_BATTERY
2022-09-13 23:18:05 +02:00
ret = isEnergySavingMode ( ) ;
2019-08-04 11:56:18 +02:00
# endif
2022-09-13 23:18:05 +02:00
break ;
default :
break ;
}
return ret ;
2022-09-12 15:26:30 +02:00
}
2017-11-20 20:42:02 +01:00
2022-09-12 15:26:30 +02:00
//*************************************************************************//
2018-07-13 10:21:59 +02:00
2022-11-11 10:49:42 +01:00
void wifiConnectionCheck ( ) {
2018-07-12 22:30:30 +02:00
2023-02-09 17:24:05 +01:00
bool success = false ;
2022-11-11 10:49:42 +01:00
if ( ( wifi_check_interval_counter + WIFI_CHECK_INTERVAL_MS ) > millis ( ) ) {
2022-09-12 15:26:30 +02:00
// if check interval is not exceeded abort check
return ;
}
2019-08-03 23:17:17 +02:00
2022-09-12 15:26:30 +02:00
wifi_check_interval_counter = millis ( ) ;
2022-06-22 13:52:55 +02:00
2023-02-09 17:24:05 +01:00
/*if (WiFi.status() == WL_CONNECTED) {
2022-09-12 23:07:05 +02:00
// if we are connected
return ;
2023-02-09 17:24:05 +01:00
} */
# ifdef ENABLE_PING_HOST_TEST
debug ( " Ping 192.168.0.85 " ) ;
success = Ping . ping ( PING_HOST_IP , 3 ) ;
if ( success )
{
debug ( " Ping success " ) ;
return ;
2022-09-12 23:07:05 +02:00
}
2023-02-09 17:24:05 +01:00
# endif // ENABLE_PING_HOST_TEST
2022-09-12 23:07:05 +02:00
2023-02-09 17:24:05 +01:00
debug ( " Connection problem, resetting ESP " ) ;
2023-02-09 16:57:37 +01:00
# ifdef ENABLE_WATCHDOG
// loop endless, watchdog will reset the device
while ( 1 = = 1 ) { }
# endif
ESP . reset ( ) ;
2022-09-14 20:05:50 +02:00
2023-01-10 12:21:02 +01:00
}
2022-10-31 11:56:44 +01:00
2023-01-10 12:21:02 +01:00
# ifdef DISABLE_WIFIMANAGER
void wifi_disconnected ( WiFiEvent_t event ) {
2022-09-12 20:27:42 +02:00
2022-11-11 10:49:42 +01:00
if ( wifi_reconnect_cnt > = 5 ) {
2023-01-10 12:21:02 +01:00
debug ( " \n Reboot, to much reconnects to wifi done before " ) ;
2022-11-11 10:49:42 +01:00
ESP . restart ( ) ;
2023-01-10 12:21:02 +01:00
2022-10-31 11:56:44 +01:00
} else {
2023-01-10 12:21:02 +01:00
debug ( " no wifi connection, try to reconnect " + String ( wifi_reconnect_cnt ) ) ;
wifi_reconnect_cnt + + ;
2022-11-11 10:49:42 +01:00
wifiConnect ( ) ;
2022-10-31 11:56:44 +01:00
2023-01-10 12:21:02 +01:00
# ifdef WEBUPDATER_FEATURE
setWifiReconnectCnt ( wifi_reconnect_cnt ) ;
# endif
2022-10-31 11:56:44 +01:00
}
2023-01-10 12:21:02 +01:00
2017-10-20 00:13:40 +02:00
}
2023-01-10 12:21:02 +01:00
# endif
2017-10-20 00:13:40 +02:00
2022-09-14 20:05:50 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void wifiConnect ( ) {
2022-09-12 15:26:30 +02:00
// Establish WiFi connection if not already applied
2022-07-27 10:29:01 +02:00
2022-10-31 11:56:44 +01:00
# ifndef DISABLE_WIFIMANAGER
2022-07-27 10:29:01 +02:00
wifiManager . setMinimumSignalQuality ( WIFI_MINIMUM_SIGNAL_QUALITY ) ;
// the time in seconds to wait for the known wifi connection
wifiManager . setConnectTimeout ( WIFI_AUTOCONNECT_TIMEOUT_S ) ;
// the time in seconds to wait for the user to configure the device
wifiManager . setTimeout ( WIFI_CONFIG_PORTAL_TIMEOUT_S ) ;
2022-11-11 10:49:42 +01:00
while ( ! wifiManager . autoConnect ( wifiName . c_str ( ) , " DEADBEEF " ) ) {
2022-09-13 09:59:35 +02:00
2022-07-27 10:29:01 +02:00
# ifdef SLEEP_IF_NO_WLAN_CONNECTION
2022-09-13 09:59:35 +02:00
// If autoconnect to WLAN failed and no client connected, go to deep sleep
2022-07-27 10:29:01 +02:00
ESP . deepSleep ( POWERSAVING_SLEEP_S * 1000000 , WAKE_RF_DEFAULT ) ;
delay ( 100 ) ;
# endif
2022-09-13 09:59:35 +02:00
2022-07-27 10:29:01 +02:00
# ifndef SLEEP_IF_NO_WLAN_CONNECTION
// sleep a few seconds and go on trying to connect
2022-09-12 15:26:30 +02:00
debug ( " WiFi connection failed, try again in 5 seconds... " ) ;
2022-07-27 10:29:01 +02:00
delay ( 5000 ) ;
# endif
}
2023-01-10 12:21:02 +01:00
# else // DISABLE_WIFIMANAGER is defined
2022-10-31 11:56:44 +01:00
2022-11-11 10:49:42 +01:00
if ( ! WiFi . config ( local_IP , gateway , subnet ) ) {
2022-11-02 19:22:05 +01:00
debug ( " Failed to set IP configuration " ) ;
} else {
debug ( " Successful set IP configuration " ) ;
}
2023-01-10 12:21:02 +01:00
// bind the check function to the disconnected wifi event
WiFi . onEvent ( wifi_disconnected , WIFI_EVENT_STAMODE_DISCONNECTED ) ;
2022-10-31 11:56:44 +01:00
WiFi . begin ( WIFI_SSID , WIFI_PASSWD ) ;
2022-12-31 09:35:56 +01:00
WiFi . setAutoReconnect ( true ) ;
2022-10-31 11:56:44 +01:00
debug ( " Connecting to WLAN " ) ;
2022-11-11 10:49:42 +01:00
while ( WiFi . status ( ) ! = WL_CONNECTED ) {
2022-10-31 11:56:44 +01:00
delay ( 100 ) ;
}
2022-11-11 10:49:42 +01:00
# endif // DISABLE_WIFIMANAGER
2022-07-27 10:29:01 +02:00
}
//*************************************************************************//
2019-01-27 18:49:28 +01:00
# ifdef BATTERY_POWERED
2022-11-11 10:49:42 +01:00
void criticalBatCheck ( ) {
2019-08-04 11:56:18 +02:00
float volt = battery_voltage ( ) ;
2022-11-11 10:49:42 +01:00
if ( volt < = BAT_EMERGENCY_DEEPSLEEP_VOLTAGE ) {
2019-08-03 23:17:17 +02:00
debug ( " Bat Voltage: " + String ( volt ) + " V " ) ;
debug ( " Low battery, going into deep sleep. " ) ;
2019-02-03 19:10:38 +01:00
// Casting to an unsigned int, so it fits into the integer range
2022-11-11 10:49:42 +01:00
ESP . deepSleep ( 1U * EMERGENCY_SLEEP_S * 1000000u ) ; // battery low, shutting down
2018-07-19 20:13:08 +02:00
delay ( 100 ) ;
2018-07-13 10:02:50 +02:00
}
2018-07-13 20:20:56 +02:00
}
2019-01-27 18:49:28 +01:00
# endif
2018-07-13 20:20:56 +02:00
2022-09-12 15:26:30 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void loop ( ) {
2022-06-22 13:52:55 +02:00
2019-01-27 18:49:28 +01:00
# ifdef BATTERY_POWERED
2018-07-12 22:30:30 +02:00
delay ( 50 ) ;
return ;
2023-01-10 12:21:02 +01:00
# else // not in BATTERY_POWERED mode
# ifdef ENABLE_WATCHDOG
WDT_FEED ( ) ;
# endif
2022-09-14 11:58:13 +02:00
// call fsm loop function
_fsm_loop ( ) ;
2018-07-12 22:30:30 +02:00
2022-05-25 21:45:09 +02:00
// Needed to give WIFI time to function properly
2018-07-12 22:30:30 +02:00
delay ( DELAY_LOOP_MS ) ;
2022-09-14 11:58:13 +02:00
# endif
2018-07-12 22:30:30 +02:00
}
2018-06-23 20:13:33 +02:00
2022-09-12 15:26:30 +02:00
//*************************************************************************//
2022-09-14 11:58:13 +02:00
# ifndef BATTERY_POWERED
2022-11-11 13:25:28 +01:00
void _fsm_loop ( )
{
2018-07-12 22:30:30 +02:00
2022-09-14 00:10:03 +02:00
# ifdef WEBUPDATER_FEATURE
2022-11-11 13:25:28 +01:00
if ( ( update_webserver_cnt + ( UPDATE_WEBSERVER_INTVERVAL_MS ) ) < = millis ( ) )
{
2022-09-14 00:10:03 +02:00
//debug("web updater call");
update_webserver_cnt = millis ( ) ;
doWebUpdater ( ) ;
}
2023-01-10 12:21:02 +01:00
# ifdef ENABLE_WATCHDOG
WDT_FEED ( ) ;
# endif
2022-09-14 00:10:03 +02:00
# endif
2022-11-11 10:49:42 +01:00
# ifdef HTTP_CALL_SEND_JSON_DATA
if ( ( http_call_send_json_data_cnt + ( HTTP_CALL_SEND_JSON_DATA_INTERVAL_S * 1000 ) ) < = millis ( ) and validData = = true )
2022-05-23 12:09:45 +02:00
{
2022-11-11 10:49:42 +01:00
// send the data to the server
debug ( " Sending weather json data to http webserver " ) ;
2022-11-11 13:25:28 +01:00
//debug(String(0) + "=" + String(currentSensorData[0]));
//debug(String(SENSOR_TEMPERATURE) + "=" + String(currentSensorData[SENSOR_TEMPERATURE]));
2022-11-11 10:49:42 +01:00
http_call_send_json_data_cnt = millis ( ) ;
http_call_send_json_data ( ) ;
}
2023-01-10 12:21:02 +01:00
# ifdef ENABLE_WATCHDOG
WDT_FEED ( ) ;
# endif
2022-11-11 10:49:42 +01:00
# endif
2022-11-11 13:25:28 +01:00
switch ( fsm_state )
{
2022-09-12 15:26:30 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_1 :
2022-09-13 12:46:18 +02:00
//debug("wind speed exceeded check if required");
2022-05-22 08:51:24 +02:00
# ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
2022-11-11 10:49:42 +01:00
if ( ( update_windspeed_exceed_cnt + ( HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000 ) ) < = millis ( ) ) {
2022-09-12 21:44:10 +02:00
debug ( " reading wind sensor exceed " ) ;
2022-09-14 11:58:13 +02:00
// reset the wait timer to get a value every HTTP_CALL_ON_WINDSPEED_INTERVAL_S independently to the runtime of the measurement
2022-09-12 20:27:42 +02:00
update_windspeed_exceed_cnt = millis ( ) ;
2022-09-12 21:44:10 +02:00
2022-09-15 13:04:42 +02:00
// start measurement of wind speed
start_measure_wind ( ) ;
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_11 ; // wait untile the wind meas time exceeded
break ; // abort case here to prevent read of next sensor in list
2022-05-22 08:51:24 +02:00
}
# endif
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_2 ;
2022-09-12 15:26:30 +02:00
break ;
2022-05-22 08:51:24 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_2 :
2022-09-12 21:44:10 +02:00
//debug("reset time check if required");
2022-05-25 20:14:22 +02:00
# ifdef RESET_ESP_TIME_INTERVAL
2022-09-12 15:26:30 +02:00
// if millis() reached interval restart ESP
2022-11-11 10:49:42 +01:00
if ( RESET_ESP_TIME_INTERVAL_MS < = millis ( ) ) {
2022-09-12 19:09:43 +02:00
debug ( " resetting firmware intentionally " ) ;
2022-09-12 15:26:30 +02:00
// Push reset button after flashing once or do a manual power cycle to get the functionality working.
ESP . restart ( ) ;
}
2018-07-12 22:30:30 +02:00
# endif
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_3 ;
2022-09-13 09:59:35 +02:00
break ;
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_3 :
2022-09-13 09:59:35 +02:00
wifiConnectionCheck ( ) ;
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_4 ;
2022-09-12 15:26:30 +02:00
break ;
2019-08-03 23:17:17 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_4 :
2022-09-12 21:44:10 +02:00
//debug("disable measure of wind speed if required");
2019-08-04 11:56:18 +02:00
# ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND)
2022-11-11 10:49:42 +01:00
if ( energySavingMode ( ) = = 1 ) {
2022-09-12 15:26:30 +02:00
// Disable expensive tasks
2022-09-13 23:18:05 +02:00
//sensors[SENSOR_WINDSPEED] = 0;
2022-09-12 21:44:10 +02:00
//debug("read of wind sensor because of low battery disabled");
2022-09-13 23:18:05 +02:00
do_not_read_windsensor = true ;
2022-09-13 11:05:19 +02:00
2022-09-12 15:26:30 +02:00
} else {
2022-09-13 23:18:05 +02:00
//sensors[SENSOR_WINDSPEED] = &wind_speed;
2022-09-12 21:44:10 +02:00
//debug("read of wind sensor because of high battery enabled");
2022-09-13 23:18:05 +02:00
do_not_read_windsensor = false ;
2022-09-12 15:26:30 +02:00
}
2019-03-31 16:17:57 +02:00
# endif
2022-09-12 19:31:16 +02:00
sensor_cnt = 0 ;
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_5 ;
2022-09-12 15:26:30 +02:00
break ;
2019-08-04 11:56:18 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_5 :
2022-09-12 21:44:10 +02:00
//debug("read sensor data check");
2022-11-11 10:49:42 +01:00
if ( ( update_sensor_cnt + ( UPDATE_SENSOR_INTERVAL_S * 1000 ) ) < = millis ( ) or validData = = false ) {
2022-09-12 19:31:16 +02:00
debug ( " read sensor data " + String ( sensor_cnt ) ) ;
2022-09-13 09:59:35 +02:00
2022-11-11 10:49:42 +01:00
if ( sensor_cnt ! = SENSOR_WINDSPEED ) {
2022-09-14 11:58:13 +02:00
// read data from sensor
currentSensorData [ sensor_cnt ] = readSensors ( sensor_cnt ) ;
2022-09-14 20:05:50 +02:00
//debug(String(sensor_cnt) + "=" + String(currentSensorData[sensor_cnt]));
2022-09-14 11:58:13 +02:00
} else {
2022-09-14 20:05:50 +02:00
2022-11-11 10:49:42 +01:00
start_measure_wind ( ) ; // start measurement of wind speed
fsm_state = FSM_STATE_9 ; // wait untile the wind meas time exceeded
break ; // abort case here to prevent read of next sensor in list
2022-09-14 11:58:13 +02:00
}
2022-11-11 10:49:42 +01:00
if ( sensor_cnt < VALUES - 1 ) {
2022-09-12 19:09:43 +02:00
sensor_cnt + + ;
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_5 ; // jump to same state again, more sensors to read
2022-09-13 11:05:19 +02:00
2022-09-12 15:26:30 +02:00
} else {
2022-09-13 11:05:19 +02:00
2022-11-11 10:49:42 +01:00
update_sensor_cnt = millis ( ) ; // reset the update interval counter
2022-09-12 19:09:43 +02:00
sensor_cnt = 0 ;
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_6 ; // next state
2022-09-12 15:26:30 +02:00
}
2022-09-13 11:05:19 +02:00
2022-09-12 19:09:43 +02:00
} else {
2022-09-13 11:05:19 +02:00
2022-09-13 12:46:18 +02:00
//debug("skip read sensor data");
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_1 ; // no new data, reset FSM
2022-09-12 15:26:30 +02:00
}
break ;
2019-08-03 23:17:17 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_6 :
2022-09-13 12:46:18 +02:00
//debug("log to serial if required");
2019-08-04 12:08:31 +02:00
# ifdef SERIAL_FEATURE
2022-09-12 15:26:30 +02:00
logToSerial ( currentSensorData ) ;
2019-08-04 12:08:31 +02:00
# endif
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_7 ;
2022-09-12 15:26:30 +02:00
break ;
2017-10-20 00:13:40 +02:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_7 :
2022-09-13 12:46:18 +02:00
//debug("send data to influxdb if required");
2019-08-04 12:08:31 +02:00
# ifdef INFLUXDB_FEATURE
2022-11-11 10:49:42 +01:00
for ( uint8_t i = 0 ; i < 5 and validData = = false ; i + + ) { // only check sensor data 0 to 4 -> SENSOR_TEMPERATURE, SENSOR_HUMIDITY, SENSOR_LIGHT, SENSOR_WINDSPEED, SENSOR_PRESSURE
if ( currentSensorData [ i ] ! = 0 and currentSensorData [ i ] ! = nanf ( " no value " ) and ( not isnan ( currentSensorData [ i ] ) ) ) {
2022-09-12 20:27:42 +02:00
validData = true ;
2022-09-12 15:26:30 +02:00
}
}
2022-09-12 21:44:10 +02:00
2022-11-11 10:49:42 +01:00
if ( validData = = true ) {
2022-09-12 21:44:10 +02:00
// send data only if valid data is available
pushToInfluxDB ( DEVICE_NAME , currentSensorData ) ;
}
2019-08-04 12:08:31 +02:00
# endif
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_8 ;
2022-09-12 15:26:30 +02:00
break ;
2019-01-27 18:49:28 +01:00
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
case FSM_STATE_8 :
2022-09-13 12:46:18 +02:00
//debug("set sensor data in webupdater if required");
2018-12-02 17:06:06 +01:00
# ifdef WEBUPDATER_FEATURE
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2022-09-12 15:26:30 +02:00
setSensorData ( currentSensorData ) ;
2022-11-15 14:39:21 +01:00
# endif
2018-07-12 22:30:30 +02:00
# endif
2022-11-11 10:49:42 +01:00
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_1 ;
break ;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_9 :
# ifdef SENSOR_WIND
2022-11-11 10:49:42 +01:00
if ( check_measure_wind_done ( ) = = false ) {
2022-09-14 11:58:13 +02:00
//debug("wait for wind sensor finish");
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_9 ; // stay here until the wind measurement is done
2022-09-14 11:58:13 +02:00
} else {
2022-09-14 12:18:58 +02:00
//debug("wind sensor read finish");
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_10 ;
}
# else
// in case that the wind sensor is not used skip this step
sensor_cnt + + ;
fsm_state = FSM_STATE_5 ;
# endif
break ;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_10 :
# ifdef SENSOR_WIND
2022-11-11 14:34:42 +01:00
currentSensorData [ SENSOR_WINDSPEED ] = measure_wind_result ( ) ;
debug ( " wind sensor " + String ( currentSensorData [ SENSOR_WINDSPEED ] ) ) ;
2022-09-14 11:58:13 +02:00
# endif
// step into read of next sensor read
sensor_cnt + + ;
fsm_state = FSM_STATE_5 ;
break ;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_11 :
# ifdef SENSOR_WIND
2022-11-11 10:49:42 +01:00
if ( check_measure_wind_done ( ) = = false ) {
2022-09-14 11:58:13 +02:00
//debug("wait for wind sensor finish");
2022-11-11 10:49:42 +01:00
fsm_state = FSM_STATE_11 ; // stay here until the wind measurement is done
2022-09-14 11:58:13 +02:00
} else {
2022-09-14 12:18:58 +02:00
//debug("wind sensor read finish");
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_12 ;
}
# else
// in case that the wind sensor is not used skip this step
sensor_cnt + + ;
fsm_state = FSM_STATE_5 ;
# endif
break ;
/* -------------------------------------------------------------------------------- */
case FSM_STATE_12 :
# ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
2022-11-11 14:34:42 +01:00
currentSensorData [ SENSOR_WINDSPEED ] = measure_wind_result ( ) ;
debug ( " wind sensor value " + String ( currentSensorData [ SENSOR_WINDSPEED ] ) ) ;
2022-09-14 11:58:13 +02:00
2022-11-11 10:49:42 +01:00
if ( currentSensorData [ SENSOR_WINDSPEED ] > = HTTP_CALL_ON_WINDSPEED_EXCEED_MPS ) {
2022-09-14 11:58:13 +02:00
// windspeed exceeded send http call
2022-11-10 13:49:50 +01:00
digitalWrite ( STATUS_LED_PIN , LOW ) ;
2022-11-11 10:49:42 +01:00
2022-09-14 11:58:13 +02:00
// call the url HTTP_CALL_ON_WINDSPEED_URL
WiFiClient client ;
HTTPClient http ;
2022-11-11 10:49:42 +01:00
2022-09-14 11:58:13 +02:00
http . begin ( client , String ( HTTP_CALL_ON_WINDSPEED_URL ) . c_str ( ) ) ;
// Send HTTP GET request
int httpResponseCode = http . GET ( ) ;
2022-11-11 10:49:42 +01:00
2023-01-10 12:21:02 +01:00
# ifdef ENABLE_WATCHDOG
WDT_FEED ( ) ;
# endif
2022-11-11 10:49:42 +01:00
if ( httpResponseCode > 0 ) {
2022-09-14 11:58:13 +02:00
String response = http . getString ( ) ;
debug ( " http response code: " + String ( httpResponseCode ) + " = " + response ) ;
// TODO handle response
}
2022-11-11 10:49:42 +01:00
2022-09-14 11:58:13 +02:00
http . end ( ) ;
debug ( " Called windspeed exceed callout " ) ;
2022-11-10 13:49:50 +01:00
digitalWrite ( STATUS_LED_PIN , HIGH ) ;
2022-09-14 11:58:13 +02:00
}
# ifdef WEBUPDATER_FEATURE
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2022-09-14 20:24:43 +02:00
sentWindspeed ( currentSensorData [ SENSOR_WINDSPEED ] ) ;
2022-11-15 14:39:21 +01:00
# endif // SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
# endif // WEBUPDATER_FEATURE
# endif // HTTP_CALL_ON_WINDSPEED_EXCEED
2022-09-14 11:58:13 +02:00
// step into read of next fsm state
fsm_state = FSM_STATE_2 ;
2022-09-12 15:26:30 +02:00
break ;
2022-09-14 11:58:13 +02:00
/* -------------------------------------------------------------------------------- */
2022-09-12 15:26:30 +02:00
default :
2022-09-14 11:58:13 +02:00
fsm_state = FSM_STATE_1 ;
2022-09-12 15:26:30 +02:00
break ;
2022-05-09 09:42:42 +02:00
2022-11-11 10:49:42 +01:00
} // close of switch
2022-09-12 19:09:43 +02:00
2022-09-12 19:31:16 +02:00
//debug("FSM state = " + String(fsm_state));
2022-09-14 11:58:13 +02:00
/*if (fsm_state == FSM_STATE_1)
2022-09-12 19:31:16 +02:00
{
debug ( " ---------- " ) ;
2022-09-12 21:44:10 +02:00
} */
}
2022-09-14 11:58:13 +02:00
# endif
2022-09-12 21:44:10 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
void _battery_mode_main ( ) {
2022-09-12 21:44:10 +02:00
2022-11-11 10:49:42 +01:00
if ( energySavingMode ( ) = = 1 ) {
2022-09-14 11:58:13 +02:00
// Disable expensive tasks
//debug("read of wind sensor because of low battery disabled");
do_not_read_windsensor = true ;
2022-09-12 21:44:10 +02:00
2022-09-14 11:58:13 +02:00
} else {
//debug("read of wind sensor because of high battery enabled");
do_not_read_windsensor = false ;
}
2022-09-12 21:44:10 +02:00
2022-11-11 10:49:42 +01:00
for ( uint8_t i = 0 ; i < VALUES ; i + + ) {
2022-09-14 11:58:13 +02:00
currentSensorData [ i ] = readSensors ( i ) ;
}
2022-09-12 21:44:10 +02:00
2022-09-14 11:58:13 +02:00
# ifdef SERIAL_FEATURE
logToSerial ( currentSensorData ) ;
# endif
2022-09-13 10:48:58 +02:00
2022-09-14 11:58:13 +02:00
# ifdef INFLUXDB_FEATURE
pushToInfluxDB ( DEVICE_NAME , currentSensorData ) ;
# endif
2022-09-13 23:18:05 +02:00
2022-09-13 10:48:58 +02:00
# ifdef WEBUPDATER_FEATURE
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2022-09-13 23:18:05 +02:00
setSensorData ( currentSensorData ) ;
2022-09-13 10:48:58 +02:00
# endif
2022-11-15 14:39:21 +01:00
# endif
2018-06-24 12:00:51 +02:00
}
2022-09-12 21:44:10 +02:00
//*************************************************************************//
2022-09-14 11:58:13 +02:00
# ifdef SERIAL_FEATURE
2022-11-11 10:49:42 +01:00
void logToSerial ( float sensorValues [ ] ) {
2019-08-04 12:08:31 +02:00
Serial . println ( " " ) ;
Serial . println ( " Current readings: " ) ;
Serial . println ( " Temperature: " + String ( sensorValues [ SENSOR_TEMPERATURE ] ) + " °C " ) ;
Serial . println ( " Humidity: " + String ( sensorValues [ SENSOR_HUMIDITY ] ) + " % " ) ;
Serial . println ( " Light: " + String ( sensorValues [ SENSOR_LIGHT ] ) + " Lux " ) ;
Serial . println ( " Windspeed: " + String ( sensorValues [ SENSOR_WINDSPEED ] ) + " km/h " ) ;
Serial . println ( " Pressure: " + String ( sensorValues [ SENSOR_PRESSURE ] ) + " hPa " ) ;
Serial . println ( " Bat Voltage: " + String ( sensorValues [ SENSOR_BAT_VOLTAGE ] ) + " V " ) ;
Serial . println ( " Bat charge state: " + String ( sensorValues [ SENSOR_BATCHARGESTATE ] ) ) ;
Serial . println ( " Energy saving: " + String ( sensorValues [ SENSOR_ESAVEMODE ] ) ) ;
2022-05-09 09:42:42 +02:00
}
2022-09-14 11:58:13 +02:00
# endif
2022-09-12 21:44:10 +02:00
//*************************************************************************//
2022-11-11 10:49:42 +01:00
# ifdef HTTP_CALL_SEND_JSON_DATA
String getJsonData ( )
{
2022-11-11 14:34:42 +01:00
debug ( String ( SENSOR_TEMPERATURE ) + " = " + String ( currentSensorData [ SENSOR_TEMPERATURE ] ) ) ;
2022-11-11 10:49:42 +01:00
String msg = hb_ws_msg_start +
hb_ws_msg_temp +
String ( currentSensorData [ SENSOR_TEMPERATURE ] , 2 ) +
" , " +
hb_ws_msg_humi +
2022-11-11 13:25:28 +01:00
String ( ( isnan ( currentSensorData [ SENSOR_HUMIDITY ] ) ? 0.0 : currentSensorData [ SENSOR_HUMIDITY ] ) , 2 ) +
2022-11-11 10:49:42 +01:00
" , " +
hb_ws_msg_light +
String ( currentSensorData [ SENSOR_LIGHT ] , 0 ) + // The light level for the homebridge-http-lux2 plugin is only able to parse integer values
" , " +
hb_ws_msg_windspeed +
String ( currentSensorData [ SENSOR_WINDSPEED ] , 2 ) +
" , " +
hb_ws_msg_pressure +
String ( currentSensorData [ SENSOR_PRESSURE ] , 2 ) +
" , " +
hb_ws_msg_timestamp +
String ( millis ( ) ) +
" , " +
hb_ws_msg_valid +
String ( validData ) +
hb_ws_msg_end ;
return msg ;
}
void http_call_send_json_data ( )
{
2022-11-11 11:23:08 +01:00
//debug("http call to " + String(HTTP_CALL_SEND_JSON_DATA_URL));
2022-11-11 14:34:42 +01:00
//debug(String(SENSOR_TEMPERATURE) + "=" + String(currentSensorData[SENSOR_TEMPERATURE]));
2022-11-11 10:49:42 +01:00
// windspeed exceeded send http call
digitalWrite ( STATUS_LED_PIN , LOW ) ;
// call the url HTTP_CALL_SEND_JSON_DATA_URL
WiFiClient client ;
HTTPClient http ;
String tmp_str = HTTP_CALL_SEND_JSON_DATA_URL + urlEncode ( getJsonData ( ) ) ;
debug ( " send: " + tmp_str ) ;
http . begin ( client , String ( tmp_str ) . c_str ( ) ) ;
// Send HTTP GET request
int httpResponseCode = http . GET ( ) ;
2023-01-10 12:21:02 +01:00
# ifdef ENABLE_WATCHDOG
WDT_FEED ( ) ;
# endif
2022-11-11 10:49:42 +01:00
if ( httpResponseCode > 0 ) {
String response = http . getString ( ) ;
debug ( " http response code: " + String ( httpResponseCode ) + " = " + response ) ;
// TODO handle response
}
http . end ( ) ;
digitalWrite ( STATUS_LED_PIN , HIGH ) ;
}
# endif
2023-01-12 20:21:34 +01:00
# ifdef DEBUG_RESET_REASON
void debugResetReason ( ) {
rst_info * resetInfo ;
// 1. read eeprom reset reason
//int eep_reset_reason = eeprom_read(0);
//debug("EEPROM reset reason " + String(eep_reset_reason));
// 2. read real reset reason
int reset_reason = resetInfo - > reason ;
debug ( " New reset reason " + String ( reset_reason ) ) ;
//eeprom_write(0, reset_reason);
}
# endif