From 2f9bae56222304fe0b11c8154294cc2bd080d5bb Mon Sep 17 00:00:00 2001 From: Florian Eitel Date: Sat, 3 Aug 2019 23:17:17 +0200 Subject: [PATCH] Refactor ifdef's to be less verbose and better to read --- firmware/firmware.ino | 76 ++++++++++++++++++++----------------------- firmware/sensors.ino | 2 -- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index c55dd8c..427a902 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,5 +1,3 @@ - - // Standard ESP8266 libs #include #include @@ -28,7 +26,7 @@ #include "config.h" // IMPORTANT: If you compile the sourcecode and it can't find this file, this is -// indended :) You need to create a config_user.h with your own settings. Use the +// intentional :) You need to create a config_user.h with your own settings. Use the // config_user.h.example as a starting point. #include "config_user.h" @@ -70,6 +68,13 @@ String localIP = "127.0.0.1"; #endif //*************************************************************************// +void debug(String x) { +#ifdef DEBUG + Serial.println(x); +#endif +} + + void setup() { // Erase WiFi Credentials (maybe this will work ...) @@ -101,17 +106,13 @@ void setup() { wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) { -#ifdef DEBUG - Serial.println("WiFi connection failed, going into deep sleep ..."); -#endif + debug("WiFi connection failed, going into deep sleep ..."); // If autoconnect to WLAN failed and no client connected, go to deep sleep ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); delay(100); } -#ifdef DEBUG - Serial.println("Connected!"); -#endif + debug("Connected!"); // Init variables to influxdb config - doesn't talk to database influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); @@ -122,17 +123,16 @@ void setup() { apds_connected = true; apds.enableColor(true); } -#ifdef DEBUG2 - Serial.println("Connected!"); -#endif + + debug("APDS Connected!"); + //Temperature + pressure bool status = bme.begin(BME_ADDRESS); if (!status) { -#ifdef DEBUG - Serial.println("Could not find a valid BME280 sensor, check wiring!"); -#endif + debug("Could not find a valid BME280 sensor, check wiring!"); //#warning TODO: FIXME while (1); } + debug("BME Connected!"); #ifdef WEBUPDATER_FEATURE #ifndef BATTERY_POWERED @@ -145,6 +145,7 @@ void setup() { delay(100); #ifdef BATTERY_POWERED + debug("battery powered"); _loop(); digitalWrite(STATUS_LED_PIN, LOW); @@ -154,9 +155,8 @@ void setup() { WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); -#ifdef DEBUG - Serial.println("deep sleep"); -#endif + 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); @@ -169,10 +169,8 @@ void setup() { void criticalBatCheck() { float volt = getBatteryVoltage(); if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) { -#ifdef DEBUG - Serial.println("Bat Voltage: " + String(volt) + " V"); - Serial.println("Low battery, going into deep sleep."); -#endif + debug("Bat Voltage: " + String(volt) + " V"); + debug("Low battery, going into deep sleep."); // Casting to an unsigned int, so it fits into the integer range ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down delay(100); @@ -230,9 +228,12 @@ void loop() { void _loop() { #ifndef BATTERY_POWERED - if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) { - update_sensor_cnt = 0; + if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt) { + return; + } #endif + + update_sensor_cnt = 0; currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); if (apds_connected) { @@ -258,18 +259,17 @@ void _loop() { currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_ENABLED; } #endif -#ifdef DEBUG - Serial.println(""); - //Serial.println("Current readings:"); - Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); - Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); - Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux"); - Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); - Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); - Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); - Serial.println("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE])); - Serial.println("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE])); -#endif + + debug(""); + debug("Current readings:"); + debug("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); + debug("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); + debug("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux"); + debug("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); + debug("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); + debug("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); + debug("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE])); + debug("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE])); delay(100); @@ -278,10 +278,6 @@ void _loop() { #ifdef WEBUPDATER_FEATURE setSensorData(DEVICE_NAME, localIP, currentSensorData); #endif -#ifndef BATTERY_POWERED - } -#endif - } //*************************************************************************// diff --git a/firmware/sensors.ino b/firmware/sensors.ino index 18cea4f..8ed0291 100644 --- a/firmware/sensors.ino +++ b/firmware/sensors.ino @@ -4,12 +4,10 @@ int anemometerRotations = 0; unsigned long currentTime = 0; float fetchTemperature() { - //return 10; return bme.readTemperature(); } float fetchPressure() { - //return 10; return bme.readPressure() / 100.0F; }