Read data every 5 second and send to server every 60 seconds

Hi, im new to IoT, i just started a new project (followed tutorial from several web/videos) where i read data from dht 11 and send it to my database using the esp8266 board, everything works fine, now i want to improve it, instead sending all data, i want to read values from dht 11 every 5 second, and get the highest, lowest and average in 60 second span, and send it to the server for every 60 seconds. Do you guys know how can i achieve it ? Thanks :D

Do you guys know how can i achieve it ?

More than likely :)

Use millis() based timing. Have three variables (for min, max, sum) Read DHT11, check if value less than min and store in min, greater than max and store in max and add to sum.

12 times and send the data, avg equals sum / 12

Post your code (using code tags as described in https://forum.arduino.cc/index.php?topic=148850.0) if you need advise how to incorporate in your existing code.

At the start of every 60 seconds set total and highest to 0 and lowest to 999. Add each reading to the total once every 5 seconds. If the program is not doing anything else then use delay() for the 5 second timer. Each time you take a reading test whether it is higher than previously highest read in this 60 second session and if so replace it. Do the same with the lowest value but set it to 999 initially. After 12 readings divide the total by 12 and now you have highest, lowest and average values for the 60 second period

sterretje: More than likely :)

Use millis() based timing. Have three variables (for min, max, sum) Read DHT11, check if value less than min and store in min, greater than max and store in max and add to sum.

12 times and send the data, avg equals sum / 12

Post your code (using code tags as described in https://forum.arduino.cc/index.php?topic=148850.0) if you need advise how to incorporate in your existing code.

Wow didnt expect such fast replies, anyway this is my current setup and loop which work sending both temprature and humidity every 5 second to the server, how do incorporate with the millis method like you said ? i've tried some research with the millies but i still cant understand it nor implement it..

void setup() {
  Serial.begin(115200);
  Serial.println();
  for(uint8_t t = 4; t > 0; t--){
    Serial.printf("[SETUP] WAIT #d...\n", t);
    Serial.flush();
    delay(1000);
  }
  wifiMulti.addAP("ZTE-b15dd9", "5422f8b1");
  Serial.print("network is connected !");
  dht.begin();

}

void loop() {

  float temp = dht.readTemperature();
  float hum = dht.readHumidity();
  
  if((wifiMulti.run() == WL_CONNECTED)){
    HTTPClient http;
    Serial.print("[HTTP] begin... \n");


    String urlsend="http://192.168.1.3/iot/server.php?temp="+(String)temp+"&hum="+(String)hum+"";
    Serial.print("URL to send: "+urlsend);

    http.begin(urlsend);

    Serial.print("[HTTP] GET... \n");

    int httpCode = http.GET();

    if(httpCode > 0){
      Serial.printf("[HTTP] GET ... code: %d \n", httpCode);

      if(httpCode == HTTP_CODE_OK){

        String payload = http.getString();
        Serial.println(payload);
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s \n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
  delay(5000);
}

UKHeliBob: At the start of every 60 seconds set total and highest to 0 and lowest to 999. Add each reading to the total once every 5 seconds. If the program is not doing anything else then use delay() for the 5 second timer. Each time you take a reading test whether it is higher than previously highest read in this 60 second session and if so replace it. Do the same with the lowest value but set it to 999 initially. After 12 readings divide the total by 12 and now you have highest, lowest and average values for the 60 second period

how do i implement it to my current code above ? thanks

Technically you do not need millis() timing for this. If you are doing something that happens every 5 seconds, just make a counter that increments every 'that something'. When it reaches 60/5 or in other words reaches 12, do the thing that you want to do every 60 seconds, and reset the counter to zero.

This general approach works as long as your 'every' is evenly divisible by your 'something'.

or use a trigger like this if (!(millis() % 5000)) { // do itNo need for complex elapsed time calculations or keeping track of the previous time. The millis() for timing example actually need an update.

how do i implement it to my current code

Some pseudo code for you to consider

count = 0
total = 0
min = 999
max = 0
average = 0

start of loop()
  delay 5 seconds
  read value from sensor
  total = total + value
  if value > max
    max = value
  end if

  if value < min
    min = value
  end if

  count = count + 1
  if count = 12
    average = total / 12
    save max, min, average to database
    total = 0
    min = 999
    max = 0
    average = 0
    count = 0
  end if
end of loop