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>
|
|
|
|
|
#include <ESP8266HTTPUpdateServer.h>
|
2017-11-07 22:20:14 +01:00
|
|
|
|
#include <ESP8266WiFi.h>
|
2017-11-12 22:34:08 +01:00
|
|
|
|
#include <DNSServer.h>
|
|
|
|
|
#include <ESP8266WebServer.h>
|
2022-05-22 08:51:24 +02:00
|
|
|
|
#include <ESP8266HTTPClient.h>
|
2017-11-12 22:34:08 +01:00
|
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
|
#include <WiFiClient.h> // WiFiClient
|
|
|
|
|
#include <WiFiManager.h> // WiFiManager from bib manager
|
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"
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
// 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-08-28 13:47:10 +02: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") };
|
2019-08-04 11:56:18 +02:00
|
|
|
|
float (*sensors[VALUES])() = {};
|
2017-11-07 22:20:14 +01:00
|
|
|
|
|
2022-09-13 10:48:58 +02:00
|
|
|
|
uint8_t sensor_value[] = { SENSOR_TEMPERATURE,
|
|
|
|
|
SENSOR_HUMIDITY,
|
|
|
|
|
SENSOR_LIGHT,
|
|
|
|
|
SENSOR_WINDSPEED,
|
|
|
|
|
SENSOR_PRESSURE,
|
|
|
|
|
SENSOR_BAT_VOLTAGE,
|
|
|
|
|
SENSOR_ESAVEMODE,
|
|
|
|
|
SENSOR_BATCHARGESTATE };
|
|
|
|
|
|
2022-09-12 20:27:42 +02:00
|
|
|
|
uint32_t update_sensor_cnt = 0;
|
|
|
|
|
uint32_t update_webserver_cnt = 0;
|
|
|
|
|
uint32_t update_windspeed_exceed_cnt = 0;
|
2022-09-12 21:44:10 +02:00
|
|
|
|
uint32_t wifi_check_interval_counter = 0;
|
2022-08-28 13:47:10 +02:00
|
|
|
|
|
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;
|
2019-03-31 17:01:04 +02:00
|
|
|
|
|
2022-09-12 19:09:43 +02:00
|
|
|
|
uint8_t fsm_state = FSM_STATE_WU;
|
|
|
|
|
|
|
|
|
|
uint8_t sensor_cnt = 0;
|
2022-09-12 15:26:30 +02:00
|
|
|
|
|
2022-09-12 21:44:10 +02:00
|
|
|
|
boolean validData = false;
|
|
|
|
|
|
2018-06-24 12:00:51 +02:00
|
|
|
|
//*************************************************************************//
|
2018-06-23 21:49:11 +02:00
|
|
|
|
|
2022-05-15 22:34:36 +02: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-05-15 22:34:36 +02:00
|
|
|
|
void setup()
|
|
|
|
|
{
|
2019-02-06 21:26:13 +01:00
|
|
|
|
|
2019-08-04 12:08:31 +02:00
|
|
|
|
#if defined(DEBUG) || defined(SERIAL_FEATURE)
|
2018-06-23 20:13:33 +02:00
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-11-12 22:34:08 +01:00
|
|
|
|
// Pin settings
|
2022-05-17 10:13:17 +02: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
|
|
|
|
|
2018-12-02 17:06:06 +01:00
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
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
|
|
|
|
|
2019-08-04 12:08:31 +02:00
|
|
|
|
#ifdef INFLUXDB_FEATURE
|
|
|
|
|
influxdb_begin();
|
|
|
|
|
#endif
|
2017-11-20 20:42:02 +01:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
initSensors();
|
|
|
|
|
|
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
|
|
|
|
#ifndef BATTERY_POWERED
|
|
|
|
|
setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString());
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//It's magic! leave in
|
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
|
|
#ifdef BATTERY_POWERED
|
|
|
|
|
debug("battery powered");
|
|
|
|
|
_loop();
|
|
|
|
|
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_WATCHDOG
|
|
|
|
|
// Enable the internal watchdog
|
|
|
|
|
ESP.wdtEnable(WATCHDOG_TIMEOUT_MS);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
if (sensor_apds9930_begin()) {
|
|
|
|
|
sensors[SENSOR_LIGHT] = &apds9930_light;
|
2019-03-31 17:01:04 +02:00
|
|
|
|
}
|
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
|
|
|
|
|
if (sensor_apds9960_begin()) {
|
2020-02-18 20:06:03 +01:00
|
|
|
|
sensors[SENSOR_LIGHT] = &apds9960_light;
|
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
|
2019-01-27 18:49:28 +01:00
|
|
|
|
//Temperature + pressure
|
2019-08-04 11:56:18 +02:00
|
|
|
|
if (sensor_bme280_begin(BME_ADDRESS)) {
|
|
|
|
|
sensors[SENSOR_TEMPERATURE] = &bme280_temperature;
|
2022-09-13 09:59:35 +02:00
|
|
|
|
sensors[SENSOR_HUMIDITY] = &bme280_humidity;
|
|
|
|
|
sensors[SENSOR_PRESSURE] = &bme280_pressure;
|
2018-06-23 20:13:33 +02:00
|
|
|
|
}
|
2019-08-04 11:56:18 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_WIND
|
|
|
|
|
sensors[SENSOR_WINDSPEED] = &wind_speed;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_BATTERY
|
2022-09-13 09:59:35 +02:00
|
|
|
|
sensors[SENSOR_BAT_VOLTAGE] = &battery_voltage;
|
2019-08-04 11:56:18 +02:00
|
|
|
|
sensors[SENSOR_BATCHARGESTATE] = &battery_charging;
|
2022-09-13 09:59:35 +02:00
|
|
|
|
sensors[SENSOR_ESAVEMODE] = &isEnergySavingMode;
|
2019-08-04 11:56:18 +02:00
|
|
|
|
#endif
|
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-09-12 15:26:30 +02:00
|
|
|
|
void wifiConnectionCheck()
|
|
|
|
|
{
|
2018-07-12 22:30:30 +02:00
|
|
|
|
|
2022-09-12 23:07:05 +02: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
|
|
|
|
|
2022-09-12 23:07:05 +02:00
|
|
|
|
if (WiFi.status() == WL_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
// if we are connected
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 20:27:42 +02:00
|
|
|
|
debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
|
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
wifiConnect();
|
2022-06-22 13:52:55 +02:00
|
|
|
|
|
2017-10-20 00:13:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
void wifiConnect()
|
2022-07-27 10:29:01 +02:00
|
|
|
|
{
|
2022-09-12 15:26:30 +02:00
|
|
|
|
|
|
|
|
|
// Establish WiFi connection if not already applied
|
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);
|
|
|
|
|
|
|
|
|
|
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
|
2022-09-13 09:59:35 +02:00
|
|
|
|
|
2022-07-27 10:29:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
|
#ifdef BATTERY_POWERED
|
2022-05-16 10:25:07 +02:00
|
|
|
|
void criticalBatCheck()
|
|
|
|
|
{
|
2019-08-04 11:56:18 +02:00
|
|
|
|
float volt = battery_voltage();
|
2022-09-13 09:59:35 +02: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
|
|
|
|
|
ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // 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-05-16 10:25:07 +02:00
|
|
|
|
void loop()
|
|
|
|
|
{
|
2022-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_WATCHDOG
|
|
|
|
|
ESP.wdtFeed();
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-27 18:49:28 +01:00
|
|
|
|
#ifdef BATTERY_POWERED
|
2018-07-12 22:30:30 +02:00
|
|
|
|
delay(50);
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-05-23 12:09:45 +02:00
|
|
|
|
// call sub loop function
|
2018-07-12 22:30:30 +02:00
|
|
|
|
_loop();
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
2018-06-23 20:13:33 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
2022-05-25 21:45:09 +02:00
|
|
|
|
void _loop()
|
|
|
|
|
{
|
2018-07-12 22:30:30 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
switch (fsm_state)
|
2022-05-23 12:09:45 +02:00
|
|
|
|
{
|
2022-09-12 15:26:30 +02:00
|
|
|
|
|
|
|
|
|
case FSM_STATE_WU:
|
2022-09-12 21:44:10 +02:00
|
|
|
|
//debug("web updater call if required");
|
2022-09-12 15:26:30 +02:00
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
2022-09-12 21:44:10 +02:00
|
|
|
|
if ((update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000)) <= millis())
|
2022-09-12 15:26:30 +02:00
|
|
|
|
{
|
2022-09-12 21:44:10 +02:00
|
|
|
|
//debug("web updater call");
|
2022-09-12 20:27:42 +02:00
|
|
|
|
update_webserver_cnt = millis();
|
2022-09-12 15:26:30 +02:00
|
|
|
|
doWebUpdater();
|
|
|
|
|
}
|
2022-05-23 12:09:45 +02:00
|
|
|
|
#endif
|
2022-09-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_WSE;
|
|
|
|
|
break;
|
2022-05-23 12:09:45 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_WSE:
|
2022-09-12 19:09:43 +02:00
|
|
|
|
debug("wind speed exceeded check if required");
|
2022-05-22 08:51:24 +02:00
|
|
|
|
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
|
2022-09-12 21:44:10 +02:00
|
|
|
|
if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
|
2022-05-22 08:51:24 +02:00
|
|
|
|
{
|
2022-09-12 21:44:10 +02:00
|
|
|
|
debug("reading wind sensor exceed");
|
|
|
|
|
|
2022-09-12 20:27:42 +02:00
|
|
|
|
update_windspeed_exceed_cnt = millis();
|
2022-09-12 21:44:10 +02:00
|
|
|
|
|
|
|
|
|
readWindSpeedExceed();
|
2022-05-22 08:51:24 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-09-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_RS;
|
|
|
|
|
break;
|
2022-05-22 08:51:24 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_RS:
|
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
|
|
|
|
|
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-13 09:59:35 +02:00
|
|
|
|
fsm_state = FSM_STATE_WC;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FSM_STATE_WC:
|
|
|
|
|
wifiConnectionCheck();
|
2022-09-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_WS;
|
|
|
|
|
break;
|
2019-08-03 23:17:17 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_WS:
|
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-09-12 15:26:30 +02:00
|
|
|
|
if (energySavingMode() == 1) {
|
|
|
|
|
// Disable expensive tasks
|
|
|
|
|
sensors[SENSOR_WINDSPEED] = 0;
|
2022-09-12 21:44:10 +02:00
|
|
|
|
//debug("read of wind sensor because of low battery disabled");
|
2022-09-12 15:26:30 +02:00
|
|
|
|
} else {
|
|
|
|
|
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-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-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_US;
|
|
|
|
|
break;
|
2019-08-04 11:56:18 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_US:
|
2022-09-12 21:44:10 +02:00
|
|
|
|
//debug("read sensor data check");
|
|
|
|
|
if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis())
|
2022-09-12 15:26:30 +02:00
|
|
|
|
{
|
2022-09-12 19:31:16 +02:00
|
|
|
|
debug("read sensor data " + String(sensor_cnt));
|
2022-09-13 09:59:35 +02:00
|
|
|
|
if (sensors[sensor_cnt])
|
|
|
|
|
{
|
2022-09-13 10:48:58 +02:00
|
|
|
|
debug("sensors[sensor_value[" + String(sensor_cnt) + " ]]=" + String((int)sensors[sensor_value[sensor_cnt]]) + ":" + String(sensor_value[sensor_cnt]));
|
|
|
|
|
currentSensorData[sensor_cnt] = sensors[sensor_value[sensor_cnt]]();
|
|
|
|
|
//currentSensorData[sensor_cnt] = sensor_cnt;
|
2022-09-13 09:59:35 +02:00
|
|
|
|
|
2022-09-12 19:09:43 +02:00
|
|
|
|
} else {
|
2022-09-13 09:59:35 +02:00
|
|
|
|
|
2022-09-13 10:48:58 +02:00
|
|
|
|
debug("sensors[sensor_value[" + String(sensor_cnt) + "]]=nan");
|
2022-09-12 19:09:43 +02:00
|
|
|
|
currentSensorData[sensor_cnt] = nan("no value");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sensor_cnt < VALUES)
|
|
|
|
|
{
|
|
|
|
|
sensor_cnt++;
|
2022-09-12 19:31:16 +02:00
|
|
|
|
fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read
|
2022-09-12 15:26:30 +02:00
|
|
|
|
} else {
|
2022-09-12 20:27:42 +02:00
|
|
|
|
update_sensor_cnt = millis();
|
2022-09-12 19:09:43 +02:00
|
|
|
|
sensor_cnt = 0;
|
2022-09-12 19:31:16 +02:00
|
|
|
|
fsm_state = FSM_STATE_SC; // next state
|
2022-09-12 15:26:30 +02:00
|
|
|
|
}
|
2022-09-12 19:09:43 +02:00
|
|
|
|
} else {
|
|
|
|
|
debug("skip read sensor data");
|
|
|
|
|
fsm_state = FSM_STATE_WU; // no new data, reset FSM
|
2022-09-12 15:26:30 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2019-08-03 23:17:17 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_SC:
|
2022-09-12 19:09:43 +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-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_ID;
|
|
|
|
|
break;
|
2017-10-20 00:13:40 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_ID:
|
2022-09-12 19:31:16 +02:00
|
|
|
|
debug("send data to influxdb if required");
|
2019-08-04 12:08:31 +02:00
|
|
|
|
#ifdef INFLUXDB_FEATURE
|
2022-09-12 15:26:30 +02:00
|
|
|
|
for (uint8_t i = 0; i < 5 and validData == false; i++)
|
|
|
|
|
{
|
|
|
|
|
if (currentSensorData[i] != 0)
|
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
|
if (validData == true)
|
|
|
|
|
{
|
|
|
|
|
// send data only if valid data is available
|
|
|
|
|
pushToInfluxDB(DEVICE_NAME, currentSensorData);
|
|
|
|
|
}
|
2019-08-04 12:08:31 +02:00
|
|
|
|
#endif
|
2022-09-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_SD;
|
|
|
|
|
break;
|
2019-01-27 18:49:28 +01:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
case FSM_STATE_SD:
|
2022-09-12 19:31:16 +02:00
|
|
|
|
debug("set sensor data in webupdater if required");
|
2018-12-02 17:06:06 +01:00
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
2022-09-12 15:26:30 +02:00
|
|
|
|
setSensorData(currentSensorData);
|
2018-07-12 22:30:30 +02:00
|
|
|
|
#endif
|
2022-09-12 15:26:30 +02:00
|
|
|
|
fsm_state = FSM_STATE_WU;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
fsm_state = FSM_STATE_WU;
|
|
|
|
|
break;
|
2022-05-09 09:42:42 +02:00
|
|
|
|
|
2022-09-12 15:26:30 +02:00
|
|
|
|
} // close of switch
|
2022-09-12 19:09:43 +02:00
|
|
|
|
|
|
|
|
|
//delay(100); // TODO warum hier ein delay?
|
|
|
|
|
|
2022-09-12 19:31:16 +02:00
|
|
|
|
//debug("FSM state = " + String(fsm_state));
|
2022-09-12 21:44:10 +02:00
|
|
|
|
/*if (fsm_state == FSM_STATE_WU)
|
2022-09-12 19:31:16 +02:00
|
|
|
|
{
|
|
|
|
|
debug("----------");
|
2022-09-12 21:44:10 +02:00
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
|
|
|
|
void readWindSpeedExceed()
|
|
|
|
|
{
|
|
|
|
|
if (sensors[SENSOR_WINDSPEED])
|
|
|
|
|
{
|
|
|
|
|
// read from windspeed sensorSTATUS_LED_PIN
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = sensors[SENSOR_WINDSPEED]();
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
|
|
|
|
|
|
|
|
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, HIGH);
|
|
|
|
|
|
|
|
|
|
// call the url HTTP_CALL_ON_WINDSPEED_URL
|
|
|
|
|
WiFiClient client;
|
|
|
|
|
HTTPClient http;
|
|
|
|
|
|
|
|
|
|
http.begin(client, String(HTTP_CALL_ON_WINDSPEED_URL).c_str());
|
|
|
|
|
// Send HTTP GET request
|
|
|
|
|
int httpResponseCode = http.GET();
|
|
|
|
|
|
|
|
|
|
if (httpResponseCode > 0) {
|
|
|
|
|
String response = http.getString();
|
|
|
|
|
debug("http response code: " + String(httpResponseCode) + " = " + response);
|
|
|
|
|
// TODO handle response
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.end();
|
|
|
|
|
debug("Called windspeed exceed callout");
|
|
|
|
|
digitalWrite(STATUS_LED_PIN, LOW);
|
|
|
|
|
}
|
2022-09-13 10:48:58 +02:00
|
|
|
|
|
|
|
|
|
#ifdef WEBUPDATER_FEATURE
|
|
|
|
|
setSensorData(currentSensorData);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-12 21:44:10 +02:00
|
|
|
|
} else {
|
|
|
|
|
currentSensorData[SENSOR_WINDSPEED] = nan("no value");
|
2022-09-12 19:31:16 +02:00
|
|
|
|
}
|
2018-06-24 12:00:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 21:44:10 +02:00
|
|
|
|
//*************************************************************************//
|
|
|
|
|
|
2022-05-25 21:45:09 +02: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-12 21:44:10 +02:00
|
|
|
|
|
|
|
|
|
//*************************************************************************//
|