Browse Source

Powersaving implemented. The web update mechanism is disabled.

klaute 2 years ago
parent
commit
10977e14b7
4 changed files with 70 additions and 18 deletions
  1. 7
    4
      firmware/config.h
  2. 46
    11
      firmware/firmware.ino
  3. 16
    2
      firmware/influxdb.ino
  4. 1
    1
      firmware/sensors.ino

+ 7
- 4
firmware/config.h View File

@@ -1,5 +1,7 @@
1 1
 
2
-#define DEBUG 1
2
+//#define DEBUG 1
3
+
4
+#define POWERSAVING 1
3 5
 
4 6
 #define SENSOR_TEMPERATURE    0
5 7
 #define SENSOR_HUMIDITY       1
@@ -8,9 +10,10 @@
8 10
 #define SENSOR_PRESSURE       4
9 11
 #define SENSOR_BAT_VOLTAGE    5
10 12
 
11
-#define UPDATE_SENSOR_INTERVAL_S     10
12
-#define UPDATE_WEBSERVER_INTVERVAL_S  1
13
-#define DELAY_LOOP_MS                50
13
+#define UPDATE_SENSOR_INTERVAL_S      10
14
+#define UPDATE_WEBSERVER_INTVERVAL_S   1
15
+#define DELAY_LOOP_MS                 50
16
+#define POWERSAVING_SLEEP_S          10
14 17
 
15 18
 #define STATUS_LED_PIN        BUILTIN_LED
16 19
 #define ANEMOMETER_PIN        D7

+ 46
- 11
firmware/firmware.ino View File

@@ -60,6 +60,8 @@ void setup() {
60 60
   pinMode(ANEMOMETER_PIN, INPUT_PULLUP);
61 61
   pinMode(A0, INPUT);
62 62
 
63
+  digitalWrite(STATUS_LED_PIN, HIGH);
64
+
63 65
   // Establish WiFi connection
64 66
   String wifiName = "oko-weather-" + String(ESP.getChipId());
65 67
   wifiManager.setMinimumSignalQuality(15);
@@ -85,34 +87,72 @@ void setup() {
85 87
 
86 88
   bool status = bme.begin(0x76);
87 89
   if (!status) {
90
+#ifdef DEBUG
88 91
     Serial.println("Could not find a valid BME280 sensor, check wiring!");
92
+#endif
89 93
     while (1);
90 94
   }
91 95
 
96
+#ifndef POWERSAVING
92 97
   setupWebUpdater();
98
+#endif
93 99
 
94 100
   localIP = WiFi.localIP().toString();
95 101
 
96 102
   delay(100);
97 103
 
104
+#ifdef POWERSAVING
105
+  _loop();
106
+
98 107
   digitalWrite(STATUS_LED_PIN, LOW);
108
+
109
+  WiFi.mode(WIFI_OFF);
110
+  WiFi.forceSleepBegin();
111
+
112
+  // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
113
+  ESP.deepSleep(POWERSAVING_SLEEP_S * 1000000, WAKE_RF_DEFAULT);
114
+
115
+#endif
116
+
99 117
 }
100 118
 
101 119
 //*************************************************************************//
102 120
 
103 121
 void loop() {
104 122
 
123
+#ifdef POWERSAVING
124
+  delay(50);
125
+  return;
126
+#endif
127
+
105 128
   if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
106 129
     update_webserver_cnt = 0;
107 130
     doWebUpdater();
108 131
   }
109 132
 
133
+  _loop();
134
+
135
+  delay(DELAY_LOOP_MS);
136
+
137
+  if (currentSensorData[SENSOR_BAT_VOLTAGE] <= 3.4)
138
+  {
110 139
 #ifdef DEBUG
111
-  digitalWrite(STATUS_LED_PIN, HIGH);
140
+    Serial.println("Low battery, going into deep sleep.");
112 141
 #endif
142
+    ESP.deepSleep(4294967295); // battery low, shutting down
143
+  }
144
+
145
+  update_sensor_cnt++;
146
+  update_webserver_cnt++;
147
+}
113 148
 
149
+void _loop() {
150
+
151
+#ifndef POWERSAVING
114 152
   if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
115 153
     update_sensor_cnt = 0;
154
+#endif
155
+
116 156
     currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
117 157
     currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
118 158
     currentSensorData[SENSOR_LIGHT]       = fetchLight();
@@ -120,10 +160,6 @@ void loop() {
120 160
     currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
121 161
     currentSensorData[SENSOR_BAT_VOLTAGE] = getBatteryVoltage();
122 162
 
123
-    if (currentSensorData[SENSOR_BAT_VOLTAGE] <= 3.7)
124
-    {
125
-      ESP.deepSleep(4294967295); // battery low, shutting down
126
-    }
127 163
 #ifdef DEBUG
128 164
     Serial.println("");
129 165
     //Serial.println("Current readings:");
@@ -135,16 +171,15 @@ void loop() {
135 171
     Serial.println("Bat Voltage: " + String(currentSensorData[SENSOR_BAT_VOLTAGE]) + " V");
136 172
 #endif
137 173
 
174
+    delay(100);
175
+
138 176
     pushToInfluxDB(DEVICE_NAME, currentSensorData);
177
+
178
+#ifndef POWERSAVING
139 179
     setSensorData(DEVICE_NAME, localIP, currentSensorData);
140 180
   }
181
+#endif
141 182
 
142
-  //ESP.deepSleep(DELAY_LOOP_MS * 1000); // the ESP.deepSleep requires microseconds as input, after the sleep the system will run into the setup routine
143
-  //delay(100);
144
-  delay(DELAY_LOOP_MS);
145
-
146
-  update_sensor_cnt++;
147
-  update_webserver_cnt++;
148 183
 }
149 184
 
150 185
 //*************************************************************************//

+ 16
- 2
firmware/influxdb.ino View File

@@ -1,12 +1,26 @@
1 1
 
2 2
 void pushToInfluxDB(String device, float sensorValues[]) {
3 3
 
4
-  influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE]));
4
+  /*influxdb.write("weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE]));
5 5
   influxdb.write("weather,device=" + device + " humidity=" + String(sensorValues[SENSOR_HUMIDITY]));
6 6
   influxdb.write("weather,device=" + device + " light=" + String(sensorValues[SENSOR_LIGHT]));
7 7
   influxdb.write("weather,device=" + device + " windspeed=" + String(sensorValues[SENSOR_WINDSPEED]));
8 8
   influxdb.write("weather,device=" + device + " pressure=" + String(sensorValues[SENSOR_PRESSURE]));
9
-  influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));
9
+  influxdb.write("weather,device=" + device + " batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]));*/
10
+
11
+  String msg = "weather,device=" + device + " temperature=" + String(sensorValues[SENSOR_TEMPERATURE])
12
+               + ",humidity=" + String(sensorValues[SENSOR_HUMIDITY])
13
+               + ",light=" + String(sensorValues[SENSOR_LIGHT])
14
+               + ",windspeed=" + String(sensorValues[SENSOR_WINDSPEED])
15
+               + ",pressure=" + String(sensorValues[SENSOR_PRESSURE])
16
+               + ",batvoltage=" + String(sensorValues[SENSOR_BAT_VOLTAGE]);
17
+
18
+  uint8_t tries = 0;
19
+  do {
20
+    tries++;
21
+    influxdb.write(msg);
22
+  } while (influxdb.response() != DB_SUCCESS and tries < 5);
23
+
10 24
 
11 25
 }
12 26
 

+ 1
- 1
firmware/sensors.ino View File

@@ -19,7 +19,7 @@ float fetchHumidity() {
19 19
 float fetchLight() {
20 20
   uint16_t red, green, blue, white;
21 21
   
22
-  while(!apds.colorDataReady()){
22
+  while(!apds.colorDataReady()) {
23 23
     delay(5);
24 24
   }
25 25
 

Loading…
Cancel
Save