'How to send data every hour to Database using ESP8266?
I would like to send hourly data to a database with my ESP8266. However, I don't know how to send the data exactly every hour. I don't want to use delay(). Can someone help me?
Solution 1:[1]
this is a skeleton of a sketch for retrieving time from Internet with the esp8266 into the internal RTC, set the time into the TimeLib and use TimeLib's minute() function to execute something at hh:00.
#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <sntp.h>
#include <TZ.h>
#define TIME_ZONE TZ_Europe_London
bool doneSending = false; // flag to send only once in the first minute of the hour
void setup() {
WiFi.begin(ssid, pass);
configTime(TIME_ZONE, "pool.ntp.org");
time_t now = time(nullptr);
while (now < SECS_YR_2000) { // why until time is retrieve from Internet
delay(100);
now = time(nullptr);
}
setTime(now); // set the time into TimeLib
}
void loop() {
if (minute() == 0) { // it is hh:00
if (!doneSending) {
sendData();
doneSending= true;
}
} else if (doneSending) {
doneSending = false; // reset after the :00 minute is over
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Juraj |
