Browse Source

Main loop timing optimized.

klaute 2 years ago
parent
commit
c47116af89
2 changed files with 31 additions and 17 deletions
  1. 3
    1
      firmware/config.h
  2. 28
    16
      firmware/firmware.ino

+ 3
- 1
firmware/config.h View File

@@ -7,7 +7,9 @@
7 7
 #define SENSOR_WINDSPEED      3
8 8
 #define SENSOR_PRESSURE       4
9 9
 
10
-#define UPDATE_INTERVAL       4
10
+#define UPDATE_SENSOR_INTERVAL_S     10
11
+#define UPDATE_WEBSERVER_INTVERVAL_S  1
12
+#define DELAY_LOOP_MS                50
11 13
 
12 14
 #define STATUS_LED_PIN        BUILTIN_LED
13 15
 #define ANEMOMETER_PIN        D7

+ 28
- 16
firmware/firmware.ino View File

@@ -35,6 +35,9 @@
35 35
 
36 36
 float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
37 37
 
38
+uint16_t update_sensor_cnt    = 0;
39
+uint16_t update_webserver_cnt = 0;
40
+
38 41
 WiFiManager wifiManager;
39 42
 Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
40 43
 Adafruit_APDS9960 apds;
@@ -93,33 +96,42 @@ void setup() {
93 96
 
94 97
 void loop() {
95 98
 
96
-  doWebUpdater();
99
+  if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
100
+    update_webserver_cnt = 0;
101
+    doWebUpdater();
102
+  }
97 103
 
98 104
 #ifdef DEBUG
99 105
   digitalWrite(STATUS_LED_PIN, HIGH);
100 106
 #endif
101
-  currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
102
-  currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
103
-  currentSensorData[SENSOR_LIGHT]       = fetchLight();
104
-  currentSensorData[SENSOR_WINDSPEED]   = fetchWindspeed();
105
-  currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
106 107
 
108
+  if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
109
+    update_sensor_cnt = 0;
110
+    currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
111
+    currentSensorData[SENSOR_HUMIDITY]    = fetchHumidity();
112
+    currentSensorData[SENSOR_LIGHT]       = fetchLight();
113
+    currentSensorData[SENSOR_WINDSPEED]   = fetchWindspeed();
114
+    currentSensorData[SENSOR_PRESSURE]    = fetchPressure();
115
+  
107 116
 #ifdef DEBUG
108
-  Serial.println("");
109
-  //Serial.println("Current readings:");
110
-  Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
111
-  Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
112
-  Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
113
-  Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
114
-  Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
117
+    Serial.println("");
118
+    //Serial.println("Current readings:");
119
+    Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
120
+    Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
121
+    Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
122
+    Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
123
+    Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
115 124
 #endif
116 125
 
117
-  pushToInfluxDB(DEVICE_NAME, currentSensorData);
126
+    pushToInfluxDB(DEVICE_NAME, currentSensorData);
127
+  }
118 128
 
119 129
   //ESP.deepSleep(UPDATE_INTERVAL * 1000000);
120 130
   //delay(100);
121
-  delay(UPDATE_INTERVAL * 1000);
122
-  /**/
131
+  delay(DELAY_LOOP_MS);
132
+
133
+  update_sensor_cnt++;
134
+  update_webserver_cnt++;
123 135
 }
124 136
 
125 137
 //*************************************************************************//

Loading…
Cancel
Save