Main loop timing optimized.

This commit is contained in:
klaute 2018-06-24 16:54:57 +02:00
parent 0e33d52af4
commit c47116af89
2 changed files with 31 additions and 17 deletions

View file

@ -7,7 +7,9 @@
#define SENSOR_WINDSPEED 3 #define SENSOR_WINDSPEED 3
#define SENSOR_PRESSURE 4 #define SENSOR_PRESSURE 4
#define UPDATE_INTERVAL 4 #define UPDATE_SENSOR_INTERVAL_S 10
#define UPDATE_WEBSERVER_INTVERVAL_S 1
#define DELAY_LOOP_MS 50
#define STATUS_LED_PIN BUILTIN_LED #define STATUS_LED_PIN BUILTIN_LED
#define ANEMOMETER_PIN D7 #define ANEMOMETER_PIN D7

View file

@ -35,6 +35,9 @@
float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
uint16_t update_sensor_cnt = 0;
uint16_t update_webserver_cnt = 0;
WiFiManager wifiManager; WiFiManager wifiManager;
Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT);
Adafruit_APDS9960 apds; Adafruit_APDS9960 apds;
@ -93,33 +96,42 @@ void setup() {
void loop() { void loop() {
doWebUpdater(); if (UPDATE_WEBSERVER_INTVERVAL_S * 1000 / DELAY_LOOP_MS <= update_webserver_cnt) {
update_webserver_cnt = 0;
doWebUpdater();
}
#ifdef DEBUG #ifdef DEBUG
digitalWrite(STATUS_LED_PIN, HIGH); digitalWrite(STATUS_LED_PIN, HIGH);
#endif #endif
currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); if (UPDATE_SENSOR_INTERVAL_S * 1000 / DELAY_LOOP_MS <= update_sensor_cnt) {
currentSensorData[SENSOR_LIGHT] = fetchLight(); update_sensor_cnt = 0;
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature();
currentSensorData[SENSOR_PRESSURE] = fetchPressure(); currentSensorData[SENSOR_HUMIDITY] = fetchHumidity();
currentSensorData[SENSOR_LIGHT] = fetchLight();
currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed();
currentSensorData[SENSOR_PRESSURE] = fetchPressure();
#ifdef DEBUG #ifdef DEBUG
Serial.println(""); Serial.println("");
//Serial.println("Current readings:"); //Serial.println("Current readings:");
Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C");
Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %");
Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen"); Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen");
Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h"); Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " km/h");
Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa");
#endif #endif
pushToInfluxDB(DEVICE_NAME, currentSensorData); pushToInfluxDB(DEVICE_NAME, currentSensorData);
}
//ESP.deepSleep(UPDATE_INTERVAL * 1000000); //ESP.deepSleep(UPDATE_INTERVAL * 1000000);
//delay(100); //delay(100);
delay(UPDATE_INTERVAL * 1000); delay(DELAY_LOOP_MS);
/**/
update_sensor_cnt++;
update_webserver_cnt++;
} }
//*************************************************************************// //*************************************************************************//