Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "ESP8266WebServer.h"
#include <math.h>
String wifiStatus = "";
ESP8266WebServer server(80);
// time related
int time_now = 10000;
int period = 0;
int periodOTA = 0;
int programLogicExecuted = 0;
String currentTime = "";
void setup() {
connectToWifi();
delay(5000);
startServer();
setupOTA();
}
void loop() {
int days = millis()/1000/60/60/24;
int hours = (millis()/1000/60/60) - (days*24);
int minutes = (millis()/1000/60) - (days*24*60) - (hours*60);
int seconds = (millis()/1000) - (days*24*60*60) - (hours*60*60) - (minutes*60);
currentTime = String(days) + "d " + String(hours) + "h " + String(minutes) + "m " + String(seconds) + "s";
if (millis() > time_now + period) {
time_now = millis();
period = 10000;
checkWifiStatus();
programLogicExecuted++;
}
ArduinoOTA.handle();
server.handleClient();
}
void programLogic() {
}
void checkWifiStatus() {
if (WiFi.status() == WL_CONNECTED) return;
else {
connectToWifi();
setupOTA();
startServer();
}
}
int connectToWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin("MYWIFISSD", "MYWIFIPASSWORD");
IPAddress ip(192, 168, 64, 52);
IPAddress gateway(192, 168, 64, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);
}
void startServer() {
server.on("/", handleRootPath);
server.on("/data.html", handleRootPath);
server.begin();
}
void setupOTA() {
ArduinoOTA.begin();
}
void handleRootPath() {
String itext =
"Running: " + String(currentTime) + "\n" +
"Program_Logic_Executed: " + String(programLogicExecuted) + "\n\n";
server.send(200, "text/plain", itext);
}