Hab ich gemacht
funktioniert super
// Define some constants
#define OneSecond 1000 // One second
#define WiFiDelay (45 * OneSecond) // Delay between WiFi network scans
// Define some global variables
const char* WiFi_SSID = "*****";
const char* WiFi_PASS = "*****";
String OTAError, PGM = "";
bool OTA_Update, WiFiConnected, WasConnected;
unsigned long WiFiTime, WebClientTime;
// Include these to disable brownouts conditions and to get reset reason.
// This was added because of the DOIT ESP_32 Dev Kit board which is *very* sensitive to brownouts.
// Comment out or remove these lines if your board does not have brownout issues when turning on WiFi.
#include <soc/rtc_cntl_reg.h> // For brownouts
#include <rom/rtc.h> // For reset reason
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
HTTPClient http;
WiFiServer WebServer(80);
WiFiClient WebClient;
void WriteLog(String Msg) {
// Print message to serial output
Serial.println(Msg);
}
// Setup function
void setup() {
// Initialize serial port
Serial.begin(115200);
delay(200);
PGM = __FILE__;
PGM = PGM.substring(PGM.lastIndexOf("\\") + 1);
PGM = PGM.substring(0, PGM.indexOf("."));
// Init WiFi
WiFi.disconnect(); // Clear Wifi Credentials
WiFi.mode(WIFI_STA); // Make WiFi mode is Station
// Initialize OTA callbacks
InitOTA();
// Default some program variables
WasConnected = WiFiConnected = false;
}
// Main program loop
void loop() {
// Handle OTA process and do nothing else if we are in an OTA Update
ArduinoOTA.handle();
if (OTA_Update) {
return;
}
// Check if I've lost my WiFi connection
int WiFiStatus = WiFi.status();
if (WiFiStatus != WL_CONNECTED && WiFiConnected) {
WebServer.stop();
WiFi.disconnect();
WiFiConnected = false;
WasConnected = true;
}
// If not connected to WiFi, try to connect
if (WiFiStatus != WL_CONNECTED && (millis() - WiFiTime >= WiFiDelay || WiFiTime == 0)) {
uint8_t networks = WiFi.scanNetworks();
for (uint8_t i = 0; i < networks; i++) {
if (WiFi.SSID(i) == String(WiFi_SSID)) {
uint8_t Cntr = 0;
if (WasConnected) {
WiFi.reconnect();
}
else {
WiFiStatus = WiFi.begin(WiFi_SSID, WiFi_PASS);
}
while (WiFiStatus != WL_CONNECTED) {
delay(500);
WiFiStatus = WiFi.status();
if (++Cntr > 10 || WiFiStatus == WL_CONNECTED) {
break;
}
}
if (WiFiStatus != WL_CONNECTED) {
WiFi.disconnect(true);
break;
}
else {
WiFiConnected = WasConnected = true;
WebServer.begin();
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// HELPER FUNCTIONS
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialize OTA callback functions
void InitOTA(void) {
// Set the hostname and OTA update password
ArduinoOTA.setHostname(PGM.c_str());
ArduinoOTA.setPassword("admin");
// Create OTA call backs
ArduinoOTA.onStart([]() {
OTA_Update = true;
WebServer.stop();
WriteLog("OTA update starting...");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
int p = (progress / (total / 100));
static int lp;
char buf[40];
// Write to logfile every 10% done
if (p % 10 == 0 && p != lp) {
lp = p;
WriteLog(String(p) + "% done...");
}
});
ArduinoOTA.onEnd([]() {
WriteLog("OTA update done!");
});
ArduinoOTA.onError([](ota_error_t error) {
OTAError = "OTA error [" + String(error) + "] ";
if (error == OTA_AUTH_ERROR) OTAError += "Auth Failed";
else if (error == OTA_BEGIN_ERROR) OTAError += "Begin Failed";
else if (error == OTA_CONNECT_ERROR) OTAError += "Connect Failed";
else if (error == OTA_RECEIVE_ERROR) OTAError += "Receive Failed";
else if (error == OTA_END_ERROR) OTAError += "End Failed";
else OTAError += "Unknown Error";
WriteLog(OTAError);
ESP.restart();
});
// Start the OTA process
ArduinoOTA.begin();
}
// Return CPU reset reason
#ifdef _ROM_RTC_H_
String Get_Reset_Reason(RESET_REASON reason) {
switch (reason) {
case 1: return ("POWERON_RESET"); /**<1, Vbat power on reset*/
case 2: return ("UNKNOWN");
case 3: return ("SW_RESET"); /**<3, Software reset digital core*/
case 4: return ("OWDT_RESET"); /**<4, Legacy watch dog reset digital core*/
case 5: return ("DEEPSLEEP_RESET"); /**<5, Deep Sleep reset digital core*/
case 6: return ("SDIO_RESET"); /**<6, Reset by SLC module, reset digital core*/
case 7: return ("TG0WDT_SYS_RESET"); /**<7, Timer Group0 Watch dog reset digital core*/
case 8: return ("TG1WDT_SYS_RESET"); /**<8, Timer Group1 Watch dog reset digital core*/
case 9: return ("RTCWDT_SYS_RESET"); /**<9, RTC Watch dog Reset digital core*/
case 10: return ("INTRUSION_RESET"); /**<10, Instrusion tested to reset CPU*/
case 11: return ("TGWDT_CPU_RESET"); /**<11, Time Group reset CPU*/
case 12: return ("SW_CPU_RESET"); /**<12, Software reset CPU*/
case 13: return ("RTCWDT_CPU_RESET"); /**<13, RTC Watch dog Reset CPU*/
case 14: return ("EXT_CPU_RESET"); /**<14, for APP CPU, reseted by PRO CPU*/
case 15: return ("RTCWDT_BROWN_OUT_RESET"); /**<15, Reset when the vdd voltage is not stable*/
case 16: return ("RTCWDT_RTC_RESET"); /**<16, RTC Watch dog reset digital core and rtc module*/
default: return ("UNKNOWN");
}
}
#endif