Browse Source

Clean up power settings logic

Thomas Kopp 1 year ago
parent
commit
1cef0e43c8
3 changed files with 45 additions and 34 deletions
  1. 3
    4
      firmware/config_user.h.example
  2. 32
    24
      firmware/firmware.ino
  3. 10
    6
      firmware/sensors.ino

+ 3
- 4
firmware/config_user.h.example View File

@@ -1,12 +1,11 @@
1 1
 // Copy this file to config_user.h and adjust it to your needs.
2 2
 
3 3
 // Debug output on the serial console
4
-#define DEBUG 0
4
+#define DEBUG
5 5
 
6 6
 // Enable/Disable features
7
-#define WEBUPDATER_FEATURE 0
8
-#define BATTERY_POWERED 1
9
-#define POWERSAVING 1
7
+//#define WEBUPDATER_FEATURE
8
+#define BATTERY_POWERED
10 9
 
11 10
 // InfluxDB credentials
12 11
 const char     *INFLUXDB_HOST = "hostname";

+ 32
- 24
firmware/firmware.ino View File

@@ -1,4 +1,3 @@
1
-
2 1
 #include <WiFiClient.h>
3 2
 #include <ESP8266mDNS.h>
4 3
 #include <ESP8266HTTPUpdateServer.h>
@@ -22,13 +21,13 @@
22 21
  * - Show current sensor values over simple webpage
23 22
  * - Push sensor values to various 3rd party services (https://openweathermap.org/)
24 23
  * - MQTT?
25
- * 
24
+ *
26 25
  * - Buffer sensor values if there is no WIFI connection
27 26
  * - Configure weather station over http webinterface
28 27
  * - OTA firmware update
29
- * 
28
+ *
30 29
  * TODO:
31
- * - https://sminghub.github.io/Sming/about/ 
30
+ * - https://sminghub.github.io/Sming/about/
32 31
  * - https://github.com/marvinroger/homie-esp8266
33 32
  */
34 33
 
@@ -45,8 +44,9 @@ Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
45 44
 Adafruit_APDS9960 apds;
46 45
 Adafruit_BME280 bme;
47 46
 
47
+#ifdef WEBUPDATER_FEATURE
48 48
 String localIP = "127.0.0.1";
49
-
49
+#endif
50 50
 //*************************************************************************//
51 51
 
52 52
 void setup() {
@@ -63,10 +63,11 @@ void setup() {
63 63
 
64 64
   digitalWrite(STATUS_LED_PIN, LOW);
65 65
 
66
+#ifdef BATTERY_POWERED
66 67
   criticalBatCheck();
67
-
68
+#endif
68 69
   // Establish WiFi connection
69
-  String wifiName = "oko-weather-" + String(ESP.getChipId());
70
+  String wifiName = "oko-weather-" + DEVICE_NAME;
70 71
 
71 72
   wifiManager.setMinimumSignalQuality(15);
72 73
   wifiManager.setConnectTimeout(60); // the time in seconds to wait for the known wifi connection
@@ -76,7 +77,7 @@ void setup() {
76 77
 #ifdef DEBUG
77 78
     Serial.println("WiFi connection failed, we reboot ...");
78 79
 #endif
79
-
80
+    // If autoconnect to WLAN failed and no client connected, go to deep sleep
80 81
     ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
81 82
     delay(100);
82 83
   }
@@ -85,31 +86,34 @@ void setup() {
85 86
   Serial.println("Connected!");
86 87
 #endif
87 88
 
88
-  // Open the InfluxDB session
89
+  // Init variables to influxdb config - doesn't talk to database
89 90
   influxdb.opendb(INFLUXDB_DB, INFLUXDB_USER, INFLUXDB_PASS);
90 91
 
91 92
   // Initialize and configure the sensors
93
+  //Light Sensor
92 94
   apds.begin();
93 95
   apds.enableColor(true);
94 96
 
97
+  //Temperature + pressure
95 98
   bool status = bme.begin(0x76);
96 99
   if (!status) {
97 100
 #ifdef DEBUG
98 101
     Serial.println("Could not find a valid BME280 sensor, check wiring!");
99 102
 #endif
100
-    while (1);
103
+   //#warning TODO: FIXME while (1);
101 104
   }
102 105
 
103 106
 #ifdef WEBUPDATER_FEATURE
104
-#ifndef POWERSAVING
107
+#ifndef BATTERY_POWERED
105 108
   setupWebUpdater();
106 109
   localIP = WiFi.localIP().toString();
107 110
 #endif
108 111
 #endif
109 112
 
113
+  //It's magic! leave in
110 114
   delay(100);
111 115
 
112
-#ifdef POWERSAVING
116
+#ifdef BATTERY_POWERED
113 117
   _loop();
114 118
 
115 119
   digitalWrite(STATUS_LED_PIN, LOW);
@@ -122,12 +126,12 @@ void setup() {
122 126
   // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
123 127
   ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
124 128
   delay(100);
125
-
126 129
 #endif
127 130
 }
128 131
 
129 132
 //*************************************************************************//
130 133
 
134
+#ifdef BATTERY_POWERED
131 135
 void criticalBatCheck() {
132 136
   float volt = getBatteryVoltage();
133 137
   if (volt <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
@@ -139,7 +143,9 @@ void criticalBatCheck() {
139 143
     delay(100);
140 144
   }
141 145
 }
146
+#endif
142 147
 
148
+#ifdef BATTERY_POWERED
143 149
 int energySavingMode() {
144 150
   // Give the solar panel some time to load the cell to prevent
145 151
   // flapping.
@@ -155,14 +161,13 @@ int energySavingMode() {
155 161
 
156 162
     return 1;
157 163
   }
158
-    
164
+
159 165
   return 0;
160 166
 }
167
+#endif
161 168
 
162 169
 void loop() {
163
-  criticalBatCheck();
164
-
165
-#ifdef POWERSAVING
170
+#ifdef BATTERY_POWERED
166 171
   delay(50);
167 172
   return;
168 173
 #endif
@@ -176,9 +181,9 @@ void loop() {
176 181
 
177 182
   _loop();
178 183
 
184
+  //Needed to give WIFI time to function properly
179 185
   delay(DELAY_LOOP_MS);
180 186
 
181
-
182 187
   update_sensor_cnt++;
183 188
 
184 189
 #ifdef WEBUPDATER_FEATURE
@@ -188,21 +193,24 @@ void loop() {
188 193
 
189 194
 void _loop() {
190 195
 
191
-#ifndef POWERSAVING
196
+#ifndef BATTERY_POWERED
192 197
   if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
193 198
     update_sensor_cnt = 0;
194 199
 #endif
195
-
196 200
     currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
197 201
     currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
198 202
     currentSensorData[SENSOR_LIGHT]       = fetchLight();
199 203
     currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
204
+#ifdef BATTERY_POWERED
200 205
     currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
206
+#else
207
+    currentSensorData[SENSOR_BAT_VOLTAGE] = 0xFFFFFFFF;
208
+#endif
201 209
     // Disable expensive tasks
202 210
     if (energySavingMode() == 0) {
203 211
       currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
204 212
     } else {
205
-      currentSensorData[SENSOR_WINDSPEED] = -1;
213
+      currentSensorData[SENSOR_WINDSPEED] = 0xFFFFFFFF;
206 214
     }
207 215
 
208 216
 #ifdef DEBUG
@@ -210,7 +218,7 @@ void _loop() {
210 218
     //Serial.println("Current readings:");
211 219
     Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
212 220
     Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
213
-    Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
221
+    Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lux");
214 222
     Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
215 223
     Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
216 224
     Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
@@ -219,9 +227,9 @@ void _loop() {
219 227
     delay(100);
220 228
 
221 229
     pushToInfluxDB(DEVICE_NAME, currentSensorData);
222
-    
230
+
223 231
 #ifdef WEBUPDATER_FEATURE
224
-#ifndef POWERSAVING
232
+#ifndef BATTERY_POWERED
225 233
     setSensorData(DEVICE_NAME, localIP, currentSensorData);
226 234
   }
227 235
 #endif

+ 10
- 6
firmware/sensors.ino View File

@@ -1,3 +1,4 @@
1
+#include "config_user.h"
1 2
 
2 3
 int anemometerRotations = 0;
3 4
 unsigned long currentTime = 0;
@@ -17,14 +18,17 @@ float fetchHumidity() {
17 18
 }
18 19
 
19 20
 float fetchLight() {
20
-  uint16_t red, green, blue, white;
21
-  
21
+  //TODO read values
22
+  uint16_t red, green, blue, white, lux;
23
+
22 24
   while(!apds.colorDataReady()) {
23 25
     delay(5);
24 26
   }
25
-
27
+  
26 28
   apds.getColorData(&red, &green, &blue, &white);
27
-  return white;
29
+  //calculate lux
30
+  lux = apds.calculateLux(red, green, blue);
31
+  return lux;
28 32
 }
29 33
 
30 34
 void _anemometerInterrupt() {
@@ -47,10 +51,10 @@ float fetchWindspeed() {
47 51
 }
48 52
 
49 53
 // Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
54
+#ifdef BATTERY_POWERED
50 55
 float getBatteryVoltage() {
51 56
   uint16_t raw = analogRead(A0);
52 57
   float volt = raw / 1023.0;
53 58
   return volt * 4.2;
54
-
55 59
 }
56
-
60
+#endif

Loading…
Cancel
Save