I use ESP8266 read data from http like this code.
ESP8266WiFiMulti WiFiMulti;
String payload, getData;
unsigned long unix_epoch;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("NetworkWiFi", "123456789");
// Initialize a NTPClient to get time
timeClient.begin();
const long offsetTime = 25200; // 7*60*60 = 25200
timeClient.setTimeOffset(offsetTime);
}
void readData(){
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://192.168.10.80/device/?key=001")) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
JsonArray root_0 = doc[0];
JsonArray root_1 = doc[1];
bool st = root_0[1];
timeClient.update();
unix_epoch = timeClient.getEpochTime(); // get UNIX Epoch time
bool t;
t = st;
Serial.println(t);
if(t==1){
digitalWrite(LED_BUILTIN, HIGH);
}else{
digitalWrite(LED_BUILTIN, LOW);
}
}
void loop() {
if ((WiFiMulti.run() == WL_CONNECTED)) {
readData();
}
delay(3000);
}
I can turn ON/OFF LED_BUILTIN by read status from http in this line bool st = root_0[1]; . The program work for some hour. If I open ESP8266 more than 12 hour then ESP8266 will freeze I can't turn ON/OFF LED_BUILTIN. How to fix it?