Refactor ifdef's to be less verbose and better to read

This commit is contained in:
Florian Eitel 2019-08-03 23:17:17 +02:00
parent f4139b27c8
commit 2f9bae5622
No known key found for this signature in database
GPG key ID: 9987EAFEF6F686BB
2 changed files with 36 additions and 42 deletions

View file

@ -1,5 +1,3 @@
// Standard ESP8266 libs // Standard ESP8266 libs
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
@ -28,7 +26,7 @@
#include "config.h" #include "config.h"
// IMPORTANT: If you compile the sourcecode and it can't find this file, this is // 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. // config_user.h.example as a starting point.
#include "config_user.h" #include "config_user.h"
@ -70,6 +68,13 @@ String localIP = "127.0.0.1";
#endif #endif
//*************************************************************************// //*************************************************************************//
void debug(String x) {
#ifdef DEBUG
Serial.println(x);
#endif
}
void setup() { void setup() {
// Erase WiFi Credentials (maybe this will work ...) // 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 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")) { if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
#ifdef DEBUG debug("WiFi connection failed, going into deep sleep ...");
Serial.println("WiFi connection failed, going into deep sleep ...");
#endif
// If autoconnect to WLAN failed and no client connected, go to deep sleep // If autoconnect to WLAN failed and no client connected, go to deep sleep
ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT); ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100); delay(100);
} }
#ifdef DEBUG debug("Connected!");
Serial.println("Connected!");
#endif
// Init variables to influxdb config - doesn't talk to database // Init variables to influxdb config - doesn't talk to database
influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS); influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
@ -122,17 +123,16 @@ void setup() {
apds_connected = true; apds_connected = true;
apds.enableColor(true); apds.enableColor(true);
} }
#ifdef DEBUG2
Serial.println("Connected!"); debug("APDS Connected!");
#endif
//Temperature + pressure //Temperature + pressure
bool status = bme.begin(BME_ADDRESS); bool status = bme.begin(BME_ADDRESS);
if (!status) { if (!status) {
#ifdef DEBUG debug("Could not find a valid BME280 sensor, check wiring!");
Serial.println("Could not find a valid BME280 sensor, check wiring!");
#endif
//#warning TODO: FIXME while (1); //#warning TODO: FIXME while (1);
} }
debug("BME Connected!");
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
#ifndef BATTERY_POWERED #ifndef BATTERY_POWERED
@ -145,6 +145,7 @@ void setup() {
delay(100); delay(100);
#ifdef BATTERY_POWERED #ifdef BATTERY_POWERED
debug("battery powered");
_loop(); _loop();
digitalWrite(STATUS_LED_PIN, LOW); digitalWrite(STATUS_LED_PIN, LOW);
@ -154,9 +155,8 @@ void setup() {
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin(); WiFi.forceSleepBegin();
#ifdef DEBUG debug("deep sleep");
Serial.println("deep sleep");
#endif
// the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine // 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); ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
delay(100); delay(100);
@ -169,10 +169,8 @@ void setup() {
void criticalBatCheck() { void criticalBatCheck() {
float volt = getBatteryVoltage(); float volt = getBatteryVoltage();
if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) { if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
#ifdef DEBUG debug("Bat Voltage: " + String(volt) + " V");
Serial.println("Bat Voltage: " + String(volt) + " V"); debug("Low battery, going into deep sleep.");
Serial.println("Low battery, going into deep sleep.");
#endif
// Casting to an unsigned int, so it fits into the integer range // Casting to an unsigned int, so it fits into the integer range
ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
delay(100); delay(100);
@ -230,9 +228,12 @@ void loop() {
void _loop() { void _loop() {
#ifndef BATTERY_POWERED #ifndef BATTERY_POWERED
if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) { if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt) {
update_sensor_cnt = 0; return;
}
#endif #endif
update_sensor_cnt = 0;
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
if (apds_connected) { if (apds_connected) {
@ -258,18 +259,17 @@ void _loop() {
currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_ENABLED; currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_ENABLED;
} }
#endif #endif
#ifdef DEBUG
Serial.println(""); debug("");
//Serial.println("Current readings:"); debug("Current readings:");
Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); debug("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); debug("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux"); debug("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); debug("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); debug("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V"); debug("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
Serial.println("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE])); debug("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE]));
Serial.println("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE])); debug("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE]));
#endif
delay(100); delay(100);
@ -278,10 +278,6 @@ void _loop() {
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
setSensorData(DEVICE_NAME, localIP, currentSensorData); setSensorData(DEVICE_NAME, localIP, currentSensorData);
#endif #endif
#ifndef BATTERY_POWERED
}
#endif
} }
//*************************************************************************// //*************************************************************************//

View file

@ -4,12 +4,10 @@ int anemometerRotations = 0;
unsigned long currentTime = 0; unsigned long currentTime = 0;
float fetchTemperature() { float fetchTemperature() {
//return 10;
return bme.readTemperature(); return bme.readTemperature();
} }
float fetchPressure() { float fetchPressure() {
//return 10;
return bme.readPressure() / 100.0F; return bme.readPressure() / 100.0F;
} }