Browse Source

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

Florian Eitel 1 year ago
parent
commit
2f9bae5622
No account linked to committer's email address
2 changed files with 36 additions and 42 deletions
  1. 36
    40
      firmware/firmware.ino
  2. 0
    2
      firmware/sensors.ino

+ 36
- 40
firmware/firmware.ino View File

@@ -1,5 +1,3 @@
1
-
2
-
3 1
 // Standard ESP8266 libs
4 2
 #include <ESP8266mDNS.h>
5 3
 #include <ESP8266HTTPUpdateServer.h>
@@ -28,7 +26,7 @@
28 26
 #include "config.h"
29 27
 
30 28
 // IMPORTANT: If you compile the sourcecode and it can't find this file, this is
31
-// indended :) You need to create a config_user.h with your own settings. Use the
29
+// intentional :) You need to create a config_user.h with your own settings. Use the
32 30
 // config_user.h.example as a starting point.
33 31
 #include "config_user.h"
34 32
 
@@ -70,6 +68,13 @@ String localIP = "127.0.0.1";
70 68
 #endif
71 69
 //*************************************************************************//
72 70
 
71
+void debug(String x) {
72
+#ifdef DEBUG
73
+  Serial.println(x);
74
+#endif
75
+}
76
+
77
+
73 78
 void setup() {
74 79
 
75 80
   // Erase WiFi Credentials (maybe this will work ...)
@@ -101,17 +106,13 @@ void setup() {
101 106
   wifiManager.setTimeout(WIFI_CONFIG_PORTAL_TIMEOUT_S); // the time in seconds to wait for the user to configure the device
102 107
 
103 108
   if (!wifiManager.autoConnect(wifiName.c_str(), "DEADBEEF")) {
104
-#ifdef DEBUG
105
-    Serial.println("WiFi connection failed, going into deep sleep ...");
106
-#endif
109
+    debug("WiFi connection failed, going into deep sleep ...");
107 110
     // If autoconnect to WLAN failed and no client connected, go to deep sleep
108 111
     ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
109 112
     delay(100);
110 113
   }
111 114
 
112
-#ifdef DEBUG
113
-  Serial.println("Connected!");
114
-#endif
115
+  debug("Connected!");
115 116
 
116 117
   // Init variables to influxdb config - doesn't talk to database
117 118
   influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
@@ -122,17 +123,16 @@ void setup() {
122 123
     apds_connected = true;
123 124
     apds.enableColor(true);
124 125
   }
125
-#ifdef DEBUG2
126
-  Serial.println("Connected!");
127
-#endif
126
+
127
+  debug("APDS Connected!");
128
+
128 129
   //Temperature + pressure
129 130
   bool status = bme.begin(BME_ADDRESS);
130 131
   if (!status) {
131
-#ifdef DEBUG
132
-    Serial.println("Could not find a valid BME280 sensor, check wiring!");
133
-#endif
132
+    debug("Could not find a valid BME280 sensor, check wiring!");
134 133
    //#warning TODO: FIXME while (1);
135 134
   }
135
+  debug("BME Connected!");
136 136
 
137 137
 #ifdef WEBUPDATER_FEATURE
138 138
 #ifndef BATTERY_POWERED
@@ -145,6 +145,7 @@ void setup() {
145 145
   delay(100);
146 146
 
147 147
 #ifdef BATTERY_POWERED
148
+  debug("battery powered");
148 149
   _loop();
149 150
 
150 151
   digitalWrite(STATUS_LED_PIN, LOW);
@@ -154,9 +155,8 @@ void setup() {
154 155
   WiFi.mode(WIFI_OFF);
155 156
   WiFi.forceSleepBegin();
156 157
 
157
-#ifdef DEBUG
158
-  Serial.println("deep sleep");
159
-#endif
158
+  debug("deep sleep");
159
+
160 160
   // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
161 161
   ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
162 162
   delay(100);
@@ -169,10 +169,8 @@ void setup() {
169 169
 void criticalBatCheck() {
170 170
   float volt = getBatteryVoltage();
171 171
   if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
172
-#ifdef DEBUG
173
-    Serial.println("Bat Voltage: " + String(volt) + " V");
174
-    Serial.println("Low battery, going into deep sleep.");
175
-#endif
172
+    debug("Bat Voltage: " + String(volt) + " V");
173
+    debug("Low battery, going into deep sleep.");
176 174
     // Casting to an unsigned int, so it fits into the integer range
177 175
     ESP.deepSleep(1U * EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
178 176
     delay(100);
@@ -230,9 +228,12 @@ void loop() {
230 228
 void _loop() {
231 229
 
232 230
 #ifndef BATTERY_POWERED
233
-  if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
234
-    update_sensor_cnt = 0;
231
+  if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS > update_sensor_cnt) {
232
+    return;
233
+  }
235 234
 #endif
235
+
236
+    update_sensor_cnt = 0;
236 237
     currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
237 238
     currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
238 239
     if (apds_connected) {
@@ -258,18 +259,17 @@ void _loop() {
258 259
       currentSensorData[SENSOR_ESAVEMODE] = ENERGY_SAVE_MODE_ENABLED;
259 260
     }
260 261
 #endif
261
-#ifdef DEBUG
262
-    Serial.println("");
263
-    //Serial.println("Current readings:");
264
-    Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
265
-    Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
266
-    Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
267
-    Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
268
-    Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
269
-    Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
270
-    Serial.println("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE]));
271
-    Serial.println("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE]));
272
-#endif
262
+
263
+    debug("");
264
+    debug("Current readings:");
265
+    debug("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
266
+    debug("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
267
+    debug("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
268
+    debug("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
269
+    debug("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
270
+    debug("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
271
+    debug("Bat charge state: " + String(currentSensorData[SENSOR_BATCHARGESTATE]));
272
+    debug("Energy saving: " + String(currentSensorData[SENSOR_ESAVEMODE]));
273 273
 
274 274
     delay(100);
275 275
 
@@ -278,10 +278,6 @@ void _loop() {
278 278
 #ifdef WEBUPDATER_FEATURE
279 279
     setSensorData(DEVICE_NAME, localIP, currentSensorData);
280 280
 #endif
281
-#ifndef BATTERY_POWERED
282
-  }
283
-#endif
284
-
285 281
 }
286 282
 
287 283
 //*************************************************************************//

+ 0
- 2
firmware/sensors.ino View File

@@ -4,12 +4,10 @@ int anemometerRotations = 0;
4 4
 unsigned long currentTime = 0;
5 5
 
6 6
 float fetchTemperature() {
7
-  //return 10;
8 7
   return bme.readTemperature();
9 8
 }
10 9
 
11 10
 float fetchPressure() {
12
-  //return 10;
13 11
   return bme.readPressure() / 100.0F;
14 12
 }
15 13
 

Loading…
Cancel
Save