/* *************************************************************************** ** Program : networkStuff.ino ** ** Copyright (c) 2021 Willem Aandewiel ** ** TERMS OF USE: MIT License. See bottom of file. *************************************************************************** ** Usage: ** ** #define HOSTNAME thisProject ** ** setup() ** { ** startWiFi(_HOSTNAME, 240); // timeout 4 minuten ** startMDNS(_HOSTNAME); ** httpServer.on("/index", ); ** httpServer.on("/index.html",); ** httpServer.begin(); ** } ** ** loop() ** { ** handleWiFi(); ** MDNS.update(); ** httpServer.handleClient(); ** . ** . ** } */ #include //ESP8266 Core WiFi Library #include // Version 1.0.0 - part of ESP8266 Core https://github.com/esp8266/Arduino #include // part of ESP8266 Core https://github.com/esp8266/Arduino #include // part of ESP8266 Core https://github.com/esp8266/Arduino #include // https://github.com/mrWheel/ModUpdateServer #include "updateServerHtml.h" #include // part of ESP8266 Core ESP8266HTTPUpdateServer httpUpdater(true); bool isConnected = false; /** void configModeCallback (WiFiManager *myWiFiManager) { Debugln(F("Entered config mode\r")); Debugln(WiFi.softAPIP().toString()); Debugln(" ----> brows to 192.168.4.1"); Debugln(myWiFiManager->getConfigPortalSSID()); } // configModeCallback() **/ //=========================================================================================== void startWiFi(const char* hostname, int timeOut) { WiFiManager manageWiFi; uint32_t lTime = millis(); String thisAP = String(hostname) + "-" + WiFi.macAddress(); DebugT("start WiFi ..."); manageWiFi.setDebugOutput(true); //-- reset settings - wipe stored credentials for testing //-- these are stored by the esp library //manageWiFi.resetSettings(); //-- set callback that gets called when connecting to previous //-- WiFi fails, and enters Access Point mode //manageWiFi.setAPCallback(configModeCallback); //-- sets timeout until configuration portal gets turned off //-- useful to make it all retry or go to sleep in seconds manageWiFi.setTimeout(timeOut); // in seconden ... //-- fetches ssid and pass and tries to connect //-- if it does not connect it starts an access point with the specified name //-- here "lichtKrant-" //-- and goes into a blocking loop awaiting configuration if (!manageWiFi.autoConnect(thisAP.c_str())) { DebugTln(F("failed to connect and hit timeout")); //reset and try again, or maybe put it to deep sleep //delay(3000); //ESP.reset(); //delay(2000); DebugTf(" took [%d] seconds ==> ERROR!\r\n", (millis() - lTime) / 1000); return; } Debugln(); DebugT(F("Connected to " )); Debugln (WiFi.SSID()); DebugT(F("IP address: " )); Debugln (WiFi.localIP()); DebugT(F("IP gateway: " )); Debugln (WiFi.gatewayIP()); Debugln(); httpUpdater.setup(&httpServer); httpUpdater.setIndexPage(UpdateServerIndex); httpUpdater.setSuccessPage(UpdateServerSuccess); DebugTf(" took [%d] seconds => OK!\r\n", (millis() - lTime) / 1000); } // startWiFi() //======================================================================= void startMDNS(const char *Hostname) { DebugTf("[1] mDNS setup as [%s.local]\r\n", Hostname); if (MDNS.begin(Hostname)) // Start the mDNS responder for Hostname.local { DebugTf("[2] mDNS responder started as [%s.local]\r\n", Hostname); } else { DebugTln(F("[3] Error setting up MDNS responder!\r\n")); } MDNS.addService("http", "tcp", 80); } // startMDNS() /*************************************************************************** * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **************************************************************************** */