Browse Source

manual merge

klaute 1 year ago
parent
commit
845e637c59
2 changed files with 53 additions and 19 deletions
  1. 6
    4
      firmware/config.h
  2. 47
    15
      firmware/firmware.ino

+ 6
- 4
firmware/config.h View File

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

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

Loading…
Cancel
Save