Browse Source

More energy saving features

Aaron Fischer 2 years ago
parent
commit
82e450708a
2 changed files with 53 additions and 21 deletions
  1. 6
    4
      firmware/config.h
  2. 47
    17
      firmware/firmware.ino

+ 6
- 4
firmware/config.h View File

@@ -16,8 +16,11 @@
16 16
 #define UPDATE_WEBSERVER_INTVERVAL_S   1
17 17
 #define DELAY_LOOP_MS                 50
18 18
 #define POWERSAVING_SLEEP_S          600
19
+#define EMERGENCY_SLEEP_S            172800 // Sleep for 2 days to recover
20
+#define ENERGY_SAVING_ITERATIONS     30
19 21
 
20
-#define BAT_LOW_VOLTAGE              3.4
22
+#define BAT_LOW_VOLTAGE                 3.6
23
+#define BAT_EMERGENCY_DEEPSLEEP_VOLTAGE 3.5
21 24
 
22 25
 #define STATUS_LED_PIN        BUILTIN_LED
23 26
 #define ANEMOMETER_PIN        D7
@@ -28,6 +31,7 @@
28 31
 #define BME_CS   10
29 32
 
30 33
 #define INITIAL_WEBSERVER_TIME 20
34
+#define WEBUPDATER_FEATURE 0
31 35
 
32 36
 #define SEALEVELPRESSURE_HPA (1013.25)
33 37
 
@@ -40,6 +44,4 @@ const char     *INFLUXDB_DB   = "weatherstation";
40 44
 const char     *INFLUXDB_USER = "oko";
41 45
 const char     *INFLUXDB_PASS = "de1873a0d2f8f21f17cf4d8db4f65c59";
42 46
 
43
-String DEVICE_NAME  = "klaute";
44
-
45
-
47
+String DEVICE_NAME  = "aaron";

+ 47
- 17
firmware/firmware.ino View File

@@ -37,6 +37,7 @@ float currentSensorData[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
37 37
 
38 38
 uint16_t update_sensor_cnt    = 0;
39 39
 uint16_t update_webserver_cnt = 0;
40
+uint16_t energySavingIterations = 0;
40 41
 
41 42
 WiFiManager wifiManager;
42 43
 Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
@@ -48,7 +49,6 @@ String localIP = "127.0.0.1";
48 49
 //*************************************************************************//
49 50
 
50 51
 void setup() {
51
-
52 52
   //WiFi.disconnect(); // erase the wifi credentials
53 53
 
54 54
 #ifdef DEBUG
@@ -60,13 +60,12 @@ void setup() {
60 60
   pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
61 61
   pinMode(A0, INPUT);
62 62
 
63
-  digitalWrite(STATUS_LED_PIN, HIGH);
63
+  digitalWrite(STATUS_LED_PIN, LOW);
64 64
 
65 65
   // Establish WiFi connection
66 66
   String wifiName = "oko-weather-" + String(ESP.getChipId());
67 67
 
68 68
   wifiManager.setMinimumSignalQuality(15);
69
-
70 69
   wifiManager.setConnectTimeout(60); // the time in seconds to wait for the known wifi connection
71 70
   wifiManager.setTimeout(60); // the time in seconds to wait for the user to configure the device
72 71
 
@@ -98,11 +97,12 @@ void setup() {
98 97
     while (1);
99 98
   }
100 99
 
100
+#ifdef WEBUPDATER_FEATURE
101 101
 #ifndef POWERSAVING
102 102
   setupWebUpdater();
103
-#endif
104
-
105 103
   localIP = WiFi.localIP().toString();
104
+#endif
105
+#endif
106 106
 
107 107
   delay(100);
108 108
 
@@ -111,7 +111,7 @@ void setup() {
111 111
 
112 112
   digitalWrite(STATUS_LED_PIN, LOW);
113 113
 
114
-  lowBatCheck();
114
+  criticalBatCheck();
115 115
 
116 116
   WiFi.mode(WIFI_OFF);
117 117
   WiFi.forceSleepBegin();
@@ -121,43 +121,66 @@ void setup() {
121 121
   delay(100);
122 122
 
123 123
 #endif
124
-
125 124
 }
126 125
 
127 126
 //*************************************************************************//
128 127
 
129
-void lowBatCheck()
130
-{
131
-  if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_LOW_VOLTAGE)
132
-  {
128
+void criticalBatCheck() {
129
+  if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_EMERGENCY_DEEPSLEEP_VOLTAGE) {
133 130
 #ifdef DEBUG
134 131
     Serial.println("Low battery, going into deep sleep.");
135 132
 #endif
136
-    ESP.deepSleep(4294967295); // battery low, shutting down
133
+    ESP.deepSleep(EMERGENCY_SLEEP_S * 1000000); // battery low, shutting down
137 134
     delay(100);
138 135
   }
139 136
 }
140 137
 
141
-void loop() {
138
+int energySavingMode() {
139
+  // Give the solar panel some time to load the cell to prevent
140
+  // flapping.
141
+  if (energySavingIterations > 0) {
142
+    energySavingIterations--;
143
+    return 1;
144
+  }
145
+
146
+  // Is the battery low?
147
+  if (currentSensorData[SENSOR_BAT_VOLTAGE] <= BAT_LOW_VOLTAGE) {
148
+    // Entering energy saving
149
+    if (energySavingIterations == 0) {
150
+      energySavingIterations = ENERGY_SAVING_ITERATIONS;
151
+    }
142 152
 
143
-  lowBatCheck();
153
+    return 1;
154
+  }
155
+    
156
+  return 0;
157
+}
158
+
159
+void loop() {
160
+  criticalBatCheck();
144 161
 
145 162
 #ifdef POWERSAVING
146 163
   delay(50);
147 164
   return;
148 165
 #endif
149 166
 
167
+#ifdef WEBUPDATER_FEATURE
150 168
   if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
151 169
     update_webserver_cnt = 0;
152 170
     doWebUpdater();
153 171
   }
172
+#endif
154 173
 
155 174
   _loop();
156 175
 
157 176
   delay(DELAY_LOOP_MS);
158 177
 
178
+
159 179
   update_sensor_cnt++;
180
+
181
+#ifdef WEBUPDATER_FEATURE
160 182
   update_webserver_cnt++;
183
+#endif
161 184
 }
162 185
 
163 186
 void _loop() {
@@ -170,7 +193,13 @@ void _loop() {
170 193
     currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
171 194
     currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
172 195
     currentSensorData[SENSOR_LIGHT]       = fetchLight();
173
-    currentSensorData[SENSOR_WINDSPEED]   = fetchWindspeed();
196
+
197
+    // Disable expensive tasks
198
+    if (energySavingMode() == 0) {
199
+      currentSensorData[SENSOR_WINDSPEED]   = fetchWindspeed();
200
+    } else {
201
+      currentSensorData[SENSOR_WINDSPEED]   = -1;
202
+    }
174 203
     currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
175 204
     currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
176 205
 
@@ -188,13 +217,14 @@ void _loop() {
188 217
     delay(100);
189 218
 
190 219
     pushToInfluxDB(DEVICE_NAME, currentSensorData);
191
-
220
+    
221
+#ifdef WEBUPDATER_FEATURE
192 222
 #ifndef POWERSAVING
193 223
     setSensorData(DEVICE_NAME, localIP, currentSensorData);
194 224
   }
195 225
 #endif
226
+#endif
196 227
 
197 228
 }
198 229
 
199 230
 //*************************************************************************//
200
-

Loading…
Cancel
Save