1313#include < regex>
1414#include < sstream>
1515#include < ArduinoOTA.h>
16+ #ifdef ESP32
1617#include < ESPmDNS.h>
18+ #elif defined(ESP8266)
19+ #include < ESP8266mDNS.h>
20+ #endif
1721
18- IPAddress NetworkHandler::localhost = IPADDR_NONE;
19- IPv6Address NetworkHandler::localhost_ipv6;
22+ IPAddress NetworkHandler::localhost = IPADDR_ANY;
2023AsyncWebServer NetworkHandler::webServer (WEBSERVER_PORT);
21- std::string NetworkHandler::indexHtml = INDEX_HTML;
2224std::vector<std::string> NetworkHandler::deviceMacs;
2325IPAddress NetworkHandler::targetBroadcast = DEFAULT_TARGET_BROADCAST;
2426AsyncUDP NetworkHandler::udp;
@@ -39,13 +41,18 @@ void NetworkHandler::setupWiFi() {
3941 return ;
4042 }
4143
44+ #ifdef ESP8266
45+ WiFi.setHostname (HOSTNAME);
46+ #endif
47+
4248 WiFi.begin (WIFI_SSID, WIFI_PASS);
4349}
4450
4551void NetworkHandler::loop () {
4652 ArduinoOTA.handle ();
4753}
4854
55+ #ifdef ESP32
4956void NetworkHandler::onWiFiEvent (WiFiEvent_t event) {
5057 switch (event) {
5158 case SYSTEM_EVENT_STA_START:
@@ -57,7 +64,7 @@ void NetworkHandler::onWiFiEvent(WiFiEvent_t event) {
5764 break ;
5865 case SYSTEM_EVENT_GOT_IP6:
5966 Serial.print (" STA IPv6: " );
60- Serial.println (localhost_ipv6 = WiFi.localIPv6 ());
67+ Serial.println (WiFi.localIPv6 ());
6168 break ;
6269 case SYSTEM_EVENT_STA_GOT_IP:
6370 Serial.print (" STA IP: " );
@@ -74,6 +81,28 @@ void NetworkHandler::onWiFiEvent(WiFiEvent_t event) {
7481 break ;
7582 }
7683}
84+ #elif defined(ESP8266)
85+ void NetworkHandler::onWiFiEvent (WiFiEvent_t event) {
86+ switch (event) {
87+ case WIFI_EVENT_STAMODE_CONNECTED:
88+ Serial.println (" WiFi connected." );
89+ break ;
90+ case WIFI_EVENT_STAMODE_GOT_IP:
91+ Serial.print (" STA IP: " );
92+ Serial.println (localhost = WiFi.localIP ());
93+
94+ if (DEFAULT_TARGET_BROADCAST == IPAddress ((uint32_t ) 0 )) {
95+ targetBroadcast = WiFi.broadcastIP ();
96+ }
97+ break ;
98+ case WIFI_EVENT_STAMODE_DISCONNECTED:
99+ WiFi.reconnect ();
100+ break ;
101+ default :
102+ break ;
103+ }
104+ }
105+ #endif
77106
78107void NetworkHandler::setupWebServer () {
79108 std::string deviceMacs = DEVICE_MACS;
@@ -217,7 +246,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
217246}
218247
219248std::string NetworkHandler::prepareIndexResponse (const String device, const String target) {
220- std::string response = indexHtml ;
249+ std::string response = INDEX_HTML ;
221250
222251 std::string devices = " \n " ;
223252 for (std::string device : deviceMacs) {
@@ -233,7 +262,12 @@ std::string NetworkHandler::prepareIndexResponse(const String device, const Stri
233262 std::vector<std::string> targetIPs;
234263 targetIPs.reserve (4 );
235264 if (target.length () > 6 ) {
236- targetIPs.push_back (std::string (target.c_str ()));
265+ #ifdef ESP8266
266+ if (target == " 255.255.255.255" ) {
267+ targetIPs.push_back (" (IP unset)" );
268+ } else
269+ #endif
270+ targetIPs.push_back (std::string (target.c_str ()));
237271 }
238272 std::string ip (targetBroadcast.toString ().c_str ());
239273 if (std::count (targetIPs.begin (), targetIPs.end (), ip) == 0 ) {
@@ -259,6 +293,12 @@ std::string NetworkHandler::prepareIndexResponse(const String device, const Stri
259293
260294 std::string targets = " \n " ;
261295 for (std::string target : targetIPs) {
296+ #ifdef ESP8266
297+ if (target == " (IP unset)" ) {
298+ target = " 255.255.255.255" ;
299+ }
300+ #endif
301+
262302 targets += " <option value=\" " ;
263303 targets += target;
264304 targets += " \" >" ;
0 commit comments