Compare commits

..

No commits in common. "5fcfd8965298c9e58c459b601bc42a30edd3ae93" and "35c62fbce2fd1a71951bfc7ce4944aaa5b33b0d6" have entirely different histories.

9 changed files with 133 additions and 268 deletions

View file

@ -41,8 +41,6 @@
#define BME_CS 10 #define BME_CS 10
#define BME_ADDRESS 0x76 #define BME_ADDRESS 0x76
#define SERIAL_BAUD_RATE 115200
#define WEB_UPDATER_HTTP_PORT 8080 #define WEB_UPDATER_HTTP_PORT 8080
#endif #endif

View file

@ -12,43 +12,28 @@
// maybe have a TOO LONG SSID! // maybe have a TOO LONG SSID!
String DEVICE_NAME = "weatherstation"; String DEVICE_NAME = "weatherstation";
/********************************************************************************/ // Enable/Disable features
// Enable/Disable general features
#define BATTERY_POWERED
// retries to connect after 5 seconds or starts the wifimanager
//#define SLEEP_IF_NO_WLAN_CONNECTION
#define INFLUXDB_FEATURE #define INFLUXDB_FEATURE
#define INFLUXDB_VERSION 1 // 1 or 2 #define INFLUXDB_VERSION 1 // 1 or 2
//#define LOG_MILLIS_TO_INFLUXDB #define SERIAL_FEATURE
//#define BATTERY_POWERED
//#define SERIAL_FEATURE //#define SENSOR_WIND
#define SENSOR_WIND
#define SENSOR_APDS9960 #define SENSOR_APDS9960
//#define SENSOR_APDS9930 //#define SENSOR_APDS9930
#define SENSOR_BME280 #define SENSOR_BME280
#define SENSOR_BATTERY #define SENSOR_BATTERY
//#define BAT_PINS_D34 //#define BAT_PINS_D34
// Homebridge Webstat is only possible if webupdater is also enabled
/********************************************************************************/
// not available or recommended for battery mode
/********************************************************************************/
// Restarts the firmware every n seconds
//#define RESET_ESP_TIME_INTERVAL
//#define ENABLE_WATCHDOG
//#define WEBUPDATER_FEATURE
// only available in case that webupdater is enabled
//#define USE_LOGGER
// only possible if webupdater is also enabled
//#define HOMEBRIDGE_WEBSTAT //#define HOMEBRIDGE_WEBSTAT
// for debugging windspeed measurement only, trigger and results are handled by webupdater // retries to connect after 5 seconds or starts the wifimanager
//#define DEBUG_WINDSPEED_MEASUREMENT #define SLEEP_IF_NO_WLAN_CONNECTION
//#define HTTP_CALL_ON_WINDSPEED_EXCEED // Restarts the firmware every n seconds
//#define RESET_ESP_TIMEINTERVAL // BETA STATUS
//#define HTTP_CALL_ON_WINDSPEED_EXCEED // BETA STATUS
//#define DEBUG_WINDSPEED_MEASUREMENT // for debugging windspeed measurement only
//#define LOG_MILLIS_TO_INFLUXDB
//#define ENABLE_WATCHDOG
//#define WATCHDOG_TIMEOUT_MS 30000
const float HUMIDITY_FACTOR = 1.0; const float HUMIDITY_FACTOR = 1.0;
const float LIGHT_FACTOR = 1.0; const float LIGHT_FACTOR = 1.0;

View file

@ -18,17 +18,14 @@
#define ENERGY_SAVE_MODE_ENABLED 1.0 #define ENERGY_SAVE_MODE_ENABLED 1.0
#define ENERGY_SAVE_MODE_DISABLED 0.0 #define ENERGY_SAVE_MODE_DISABLED 0.0
#define FSM_STATE_1 0 #define FSM_STATE_WU 0
#define FSM_STATE_2 1 #define FSM_STATE_WSE 1
#define FSM_STATE_3 2 #define FSM_STATE_RS 2
#define FSM_STATE_4 3 #define FSM_STATE_WC 3
#define FSM_STATE_5 4 #define FSM_STATE_WS 4
#define FSM_STATE_6 5 #define FSM_STATE_US 5
#define FSM_STATE_7 6 #define FSM_STATE_SC 6
#define FSM_STATE_8 7 #define FSM_STATE_ID 7
#define FSM_STATE_9 8 #define FSM_STATE_SD 8
#define FSM_STATE_10 9
#define FSM_STATE_11 10
#define FSM_STATE_12 11
#endif #endif

View file

@ -40,7 +40,7 @@ const String wifiName = "oko-weather-" + DEVICE_NAME;
WiFiManager wifiManager; WiFiManager wifiManager;
uint8_t fsm_state = FSM_STATE_1; uint8_t fsm_state = FSM_STATE_WU;
uint8_t sensor_cnt = 0; uint8_t sensor_cnt = 0;
@ -67,7 +67,7 @@ void setup()
{ {
#if defined(DEBUG) || defined(SERIAL_FEATURE) #if defined(DEBUG) || defined(SERIAL_FEATURE)
Serial.begin(SERIAL_BARD_RATE); Serial.begin(115200);
#endif #endif
// Pin settings // Pin settings
@ -109,11 +109,7 @@ void setup()
#ifdef BATTERY_POWERED #ifdef BATTERY_POWERED
debug("battery powered"); debug("battery powered");
_loop();
do {
_battery_mode_main();
} while (fsm_state != FSM_STATE_1);
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
@ -130,12 +126,10 @@ void setup()
delay(100); delay(100);
#endif #endif
#ifndef BATTERY_POWERED
#ifdef ENABLE_WATCHDOG #ifdef ENABLE_WATCHDOG
// Enable the internal watchdog // Enable the internal watchdog
ESP.wdtEnable(WATCHDOG_TIMEOUT_MS); ESP.wdtEnable(WATCHDOG_TIMEOUT_MS);
#endif #endif
#endif
} }
@ -244,7 +238,7 @@ void wifiConnectionCheck()
return; return;
} }
debug("no wifi connection, try to reconnect"); debug("no connection or time to check " + String(WiFi.status() == WL_CONNECTED));
wifiConnect(); wifiConnect();
@ -309,23 +303,19 @@ void loop()
#ifdef BATTERY_POWERED #ifdef BATTERY_POWERED
delay(50); delay(50);
return; return;
#endif
#else // call sub loop function
_loop();
// call fsm loop function
_fsm_loop();
// Needed to give WIFI time to function properly // Needed to give WIFI time to function properly
delay(DELAY_LOOP_MS); delay(DELAY_LOOP_MS);
#endif
} }
//*************************************************************************// //*************************************************************************//
#ifndef BATTERY_POWERED void _loop()
void _fsm_loop()
{ {
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
@ -340,27 +330,37 @@ void _fsm_loop()
switch (fsm_state) switch (fsm_state)
{ {
/* -------------------------------------------------------------------------------- */ case FSM_STATE_WU:
case FSM_STATE_1: /*
//debug("web updater call if required");
#ifdef WEBUPDATER_FEATURE
if ((update_webserver_cnt + (UPDATE_WEBSERVER_INTVERVAL_S * 1000)) <= millis())
{
//debug("web updater call");
update_webserver_cnt = millis();
doWebUpdater();
}
#endif
*/
fsm_state = FSM_STATE_WSE;
break;
case FSM_STATE_WSE:
//debug("wind speed exceeded check if required"); //debug("wind speed exceeded check if required");
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED #ifdef HTTP_CALL_ON_WINDSPEED_EXCEED
if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis()) if ((update_windspeed_exceed_cnt + (HTTP_CALL_ON_WINDSPEED_INTERVAL_S * 1000)) <= millis())
{ {
debug("reading wind sensor exceed"); debug("reading wind sensor exceed");
// reset the wait timer to get a value every HTTP_CALL_ON_WINDSPEED_INTERVAL_S independently to the runtime of the measurement
update_windspeed_exceed_cnt = millis(); update_windspeed_exceed_cnt = millis();
start_measure_wind(); // start measurement of wind speed readWindSpeedExceed();
fsm_state = FSM_STATE_11; // wait untile the wind meas time exceeded
break; // abort case here to prevent read of next sensor in list
} }
#endif #endif
fsm_state = FSM_STATE_2; fsm_state = FSM_STATE_RS;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_RS:
case FSM_STATE_2:
//debug("reset time check if required"); //debug("reset time check if required");
#ifdef RESET_ESP_TIME_INTERVAL #ifdef RESET_ESP_TIME_INTERVAL
// if millis() reached interval restart ESP // if millis() reached interval restart ESP
@ -371,21 +371,18 @@ void _fsm_loop()
ESP.restart(); ESP.restart();
} }
#endif #endif
fsm_state = FSM_STATE_3; fsm_state = FSM_STATE_WC;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_WC:
case FSM_STATE_3:
wifiConnectionCheck(); wifiConnectionCheck();
fsm_state = FSM_STATE_4; fsm_state = FSM_STATE_WS;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_WS:
case FSM_STATE_4:
//debug("disable measure of wind speed if required"); //debug("disable measure of wind speed if required");
#ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND) #ifdef defined(BATTERY_POWERED) && defined(SENSOR_WIND)
if (energySavingMode() == 1) if (energySavingMode() == 1) {
{
// Disable expensive tasks // Disable expensive tasks
//sensors[SENSOR_WINDSPEED] = 0; //sensors[SENSOR_WINDSPEED] = 0;
//debug("read of wind sensor because of low battery disabled"); //debug("read of wind sensor because of low battery disabled");
@ -398,57 +395,45 @@ void _fsm_loop()
} }
#endif #endif
sensor_cnt = 0; sensor_cnt = 0;
fsm_state = FSM_STATE_5; fsm_state = FSM_STATE_US;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_US:
case FSM_STATE_5:
//debug("read sensor data check"); //debug("read sensor data check");
if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis()) if ((update_sensor_cnt + (UPDATE_SENSOR_INTERVAL_S * 1000)) <= millis())
{ {
debug("read sensor data " + String(sensor_cnt)); debug("read sensor data " + String(sensor_cnt));
if (sensor_cnt != SENSOR_WINDSPEED)
{
// read data from sensor
currentSensorData[sensor_cnt] = readSensors(sensor_cnt); currentSensorData[sensor_cnt] = readSensors(sensor_cnt);
} else {
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
}
if (sensor_cnt < VALUES) if (sensor_cnt < VALUES)
{ {
sensor_cnt++; sensor_cnt++;
fsm_state = FSM_STATE_5; // jump to same state again, more sensors to read fsm_state = FSM_STATE_US; // jump to same state again, more sensors to read
} else { } else {
update_sensor_cnt = millis(); // reset the update interval counter update_sensor_cnt = millis();
sensor_cnt = 0; sensor_cnt = 0;
fsm_state = FSM_STATE_6; // next state fsm_state = FSM_STATE_SC; // next state
} }
} else { } else {
//debug("skip read sensor data"); //debug("skip read sensor data");
fsm_state = FSM_STATE_1; // no new data, reset FSM fsm_state = FSM_STATE_WU; // no new data, reset FSM
} }
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_SC:
case FSM_STATE_6:
//debug("log to serial if required"); //debug("log to serial if required");
#ifdef SERIAL_FEATURE #ifdef SERIAL_FEATURE
logToSerial(currentSensorData); logToSerial(currentSensorData);
#endif #endif
fsm_state = FSM_STATE_7; fsm_state = FSM_STATE_ID;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_ID:
case FSM_STATE_7:
//debug("send data to influxdb if required"); //debug("send data to influxdb if required");
#ifdef INFLUXDB_FEATURE #ifdef INFLUXDB_FEATURE
for (uint8_t i = 0; i < 5 and validData == false; i++) for (uint8_t i = 0; i < 5 and validData == false; i++)
@ -465,74 +450,44 @@ void _fsm_loop()
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
} }
#endif #endif
fsm_state = FSM_STATE_8; fsm_state = FSM_STATE_SD;
break; break;
/* -------------------------------------------------------------------------------- */ case FSM_STATE_SD:
case FSM_STATE_8:
//debug("set sensor data in webupdater if required"); //debug("set sensor data in webupdater if required");
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData); setSensorData(currentSensorData);
#endif #endif
fsm_state = FSM_STATE_1; fsm_state = FSM_STATE_WU;
break; break;
/* -------------------------------------------------------------------------------- */ default:
case FSM_STATE_9: fsm_state = FSM_STATE_WU;
#ifdef SENSOR_WIND break;
if (check_measure_wind_done() == false)
} // close of switch
//delay(100); // TODO warum hier ein delay?
//debug("FSM state = " + String(fsm_state));
/*if (fsm_state == FSM_STATE_WU)
{ {
//debug("wait for wind sensor finish"); debug("----------");
fsm_state = FSM_STATE_9; // stay here until the wind measurement is done }*/
} else {
debug("wind sensor read finish");
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
currentSensorData[sensor_cnt] = measure_wind_result();
debug("wind sensor " + String(currentSensorData[sensor_cnt]));
#endif
// step into read of next sensor read
sensor_cnt++;
fsm_state = FSM_STATE_5;
break;
/* -------------------------------------------------------------------------------- */ void readWindSpeedExceed()
case FSM_STATE_11:
#ifdef SENSOR_WIND
if (check_measure_wind_done() == false)
{ {
//debug("wait for wind sensor finish");
fsm_state = FSM_STATE_11; // stay here until the wind measurement is done
} else {
debug("wind sensor read finish");
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;
/* -------------------------------------------------------------------------------- */ // read from windspeed sensorSTATUS_LED_PIN
case FSM_STATE_12: digitalWrite(STATUS_LED_PIN, HIGH);
#ifdef HTTP_CALL_ON_WINDSPEED_EXCEED currentSensorData[SENSOR_WINDSPEED] = readSensors(SENSOR_WINDSPEED);
currentSensorData[sensor_cnt] = measure_wind_result(); digitalWrite(STATUS_LED_PIN, LOW);
debug("wind sensor value " + String(currentSensorData[sensor_cnt]));
if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS) if (currentSensorData[SENSOR_WINDSPEED] >= HTTP_CALL_ON_WINDSPEED_EXCEED_MPS)
{ {
// windspeed exceeded send http call
digitalWrite(STATUS_LED_PIN, HIGH); digitalWrite(STATUS_LED_PIN, HIGH);
// call the url HTTP_CALL_ON_WINDSPEED_URL // call the url HTTP_CALL_ON_WINDSPEED_URL
@ -543,8 +498,7 @@ void _fsm_loop()
// Send HTTP GET request // Send HTTP GET request
int httpResponseCode = http.GET(); int httpResponseCode = http.GET();
if (httpResponseCode > 0) if (httpResponseCode > 0) {
{
String response = http.getString(); String response = http.getString();
debug("http response code: " + String(httpResponseCode) + " = " + response); debug("http response code: " + String(httpResponseCode) + " = " + response);
// TODO handle response // TODO handle response
@ -557,65 +511,12 @@ void _fsm_loop()
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData); setSensorData(currentSensorData);
#endif
#endif #endif
// step into read of next fsm state
fsm_state = FSM_STATE_2;
break;
/* -------------------------------------------------------------------------------- */
default:
fsm_state = FSM_STATE_1;
break;
} // close of switch
//debug("FSM state = " + String(fsm_state));
/*if (fsm_state == FSM_STATE_1)
{
debug("----------");
}*/
}
#endif
//*************************************************************************//
void _battery_mode_main()
{
if (energySavingMode() == 1)
{
// Disable expensive tasks
//debug("read of wind sensor because of low battery disabled");
do_not_read_windsensor = true;
} else {
//debug("read of wind sensor because of high battery enabled");
do_not_read_windsensor = false;
}
for (uint8_t i = 0; i < VALUES; i++)
{
currentSensorData[i] = readSensors(i);
}
#ifdef SERIAL_FEATURE
logToSerial(currentSensorData);
#endif
#ifdef INFLUXDB_FEATURE
pushToInfluxDB(DEVICE_NAME, currentSensorData);
#endif
#ifdef WEBUPDATER_FEATURE
setSensorData(currentSensorData);
#endif
} }
//*************************************************************************// //*************************************************************************//
#ifdef SERIAL_FEATURE
void logToSerial(float sensorValues[]) void logToSerial(float sensorValues[])
{ {
Serial.println(""); Serial.println("");
@ -629,6 +530,5 @@ void logToSerial(float sensorValues[])
Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE])); Serial.println("Bat charge state: " + String(sensorValues[SENSOR_BATCHARGESTATE]));
Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE])); Serial.println("Energy saving: " + String(sensorValues[SENSOR_ESAVEMODE]));
} }
#endif
//*************************************************************************// //*************************************************************************//

View file

@ -2,30 +2,21 @@
APDS9930 _sensor_apds9930 = APDS9930(); APDS9930 _sensor_apds9930 = APDS9930();
bool sensor_apds9930_begin() bool sensor_apds9930_begin() {
{
bool status = _sensor_apds9930.init(); bool status = _sensor_apds9930.init();
if (status) {
if (status)
{
_sensor_apds9930.enableLightSensor(false); _sensor_apds9930.enableLightSensor(false);
debug("APDS9930 Connected"); debug("APDS9930 Connected");
} else { } else {
debug("Could not find a valid APDS9930 sensor, check wiring!"); debug("Could not find a valid APDS9930 sensor, check wiring!");
} }
return status; return status;
} }
float apds9930_light() float apds9930_light() {
{
float ambient_light = 0; float ambient_light = 0;
if (_sensor_apds9930.readAmbientLightLux(ambient_light)) {
if (_sensor_apds9930.readAmbientLightLux(ambient_light))
{
return ambient_light; return ambient_light;
} else { } else {
return 0; return 0;
} }

View file

@ -2,34 +2,26 @@
Adafruit_APDS9960 _sensor_apds9960; Adafruit_APDS9960 _sensor_apds9960;
bool sensor_apds9960_begin() bool sensor_apds9960_begin() {
{
bool status = _sensor_apds9960.begin(); bool status = _sensor_apds9960.begin();
if (status) {
if (status)
{
_sensor_apds9960.enableColor(true); _sensor_apds9960.enableColor(true);
debug("APDS9960 Connected"); debug("APDS9960 Connected");
} else { } else {
debug("Could not find a valid APDS9960 sensor, check wiring!"); debug("Could not find a valid APDS9960 sensor, check wiring!");
} }
return status; return status;
} }
float apds9960_light() float apds9960_light() {
{
uint16_t red, green, blue, white, lux; uint16_t red, green, blue, white, lux;
while(!_sensor_apds9960.colorDataReady()) while(!_sensor_apds9960.colorDataReady()) {
{
delay(5); delay(5);
} }
_sensor_apds9960.getColorData(&red, &green, &blue, &white); _sensor_apds9960.getColorData(&red, &green, &blue, &white);
//calculate lux //calculate lux
lux = _sensor_apds9960.calculateLux(red, green, blue); lux = _sensor_apds9960.calculateLux(red, green, blue);
return lux * LIGHT_FACTOR; return lux * LIGHT_FACTOR;
} }

View file

@ -3,32 +3,22 @@
Adafruit_BME280 _sensor_bme280; Adafruit_BME280 _sensor_bme280;
bool sensor_bme280_begin(uint8_t addr) bool sensor_bme280_begin(uint8_t addr) {
{
bool status = _sensor_bme280.begin(addr); bool status = _sensor_bme280.begin(addr);
if (status) {
if (status)
{
debug("BME280 Connected"); debug("BME280 Connected");
} else { } else {
debug("Could not find a valid BME280 sensor, check wiring!"); debug("Could not find a valid BME280 sensor, check wiring!");
} }
return status; return status;
} }
float bme280_temperature() float bme280_temperature() {
{
return _sensor_bme280.readTemperature() * TEMP_FACTOR; return _sensor_bme280.readTemperature() * TEMP_FACTOR;
} }
float bme280_pressure() {
float bme280_pressure()
{
return _sensor_bme280.readPressure() / 100.0F; return _sensor_bme280.readPressure() / 100.0F;
} }
float bme280_humidity() {
float bme280_humidity()
{
return _sensor_bme280.readHumidity() * HUMIDITY_FACTOR; return _sensor_bme280.readHumidity() * HUMIDITY_FACTOR;
} }

View file

@ -4,7 +4,6 @@
unsigned int anemometerRotations = 0; unsigned int anemometerRotations = 0;
uint32_t start_meas_wind_time = 0; uint32_t start_meas_wind_time = 0;
int interruptNumber;
ICACHE_RAM_ATTR void _anemometerInterrupt() ICACHE_RAM_ATTR void _anemometerInterrupt()
{ {
@ -14,14 +13,28 @@ ICACHE_RAM_ATTR void _anemometerInterrupt()
#endif #endif
} }
/*
float wind_speed() float wind_speed()
{ {
start_measure_wind(); anemometerRotations = 0;
do {
delay(1000); // minimum delay of measurement time is 1 second int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN);
} while (check_measure_wind_done() == false);
return measure_wind_result(); attachInterrupt(interruptNumber, _anemometerInterrupt, RISING);
delay(1000 * WIND_SENSOR_MEAS_TIME_S); // time to measure
detachInterrupt(interruptNumber);
// calculate the speed as km/h
float tmp_speed = (float)anemometerRotations * WINDSPEED_FACTOR;
#ifdef DEBUG
Serial.print("Windspeed: " + String(tmp_speed));
#endif
return tmp_speed;
} }
*/
void start_measure_wind() void start_measure_wind()
{ {
@ -32,7 +45,7 @@ void start_measure_wind()
boolean check_measure_wind_done() boolean check_measure_wind_done()
{ {
if ((start_meas_wind_time + (WIND_SENSOR_MEAS_TIME_S * 1000)) <= millis()) if (start_meas_wind + WIND_SENSOR_MEAS_TIME_S <= millis())
{ {
detachInterrupt(interruptNumber); detachInterrupt(interruptNumber);
return true; return true;
@ -40,7 +53,7 @@ boolean check_measure_wind_done()
return false; return false;
} }
float measure_wind_result() float stop_measure_wind()
{ {
return (float)anemometerRotations * WINDSPEED_FACTOR; return (float)anemometerRotations * WINDSPEED_FACTOR;
} }

View file

@ -86,8 +86,7 @@ void setSensorData(float sensorValues[])
} }
} }
for (uint8_t i = 0; i < VALUES; i++) for (uint8_t i = 0; i < VALUES; i++) {
{
_webUpdater_sensValues[i] = sensorValues[i]; _webUpdater_sensValues[i] = sensorValues[i];
} }
} }