Skip to content

Commit 2bbf0ff

Browse files
committed
Add ESP8266 support
Should fully work except for IPv6 address printing. Requires platformio/platform-espressif8266#267 to build. Updating using OTA on ESP8266 causes an exception, but that seems to be a general problem with mDNS. It still works, it just spams the Serial console.
1 parent c0dd078 commit 2bbf0ff

File tree

8 files changed

+87
-20
lines changed

8 files changed

+87
-20
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ESP Wake on Lan
2-
ESP Wake on Lan is a simple web interface on an ESP32 to send Wake on Lan Magic Packets.
2+
ESP Wake on Lan is a simple web interface on an ESP32/ESP8266 to send Wake on Lan Magic Packets.
3+
34
The MAC address of the device to wake up can be selected from a list of specified devices, or manually entered.
45
The default list of devices to wake up is an external file that is compiled into the project.
56
Manually entered MAC addresses are added to the dropdown, however they are lost when the ESP restarts.
@@ -12,7 +13,7 @@ This is how the web interface looks:
1213

1314
## Requirements
1415
1. A system with a working [platform io](https://platformio.org/) installation.
15-
2. A ESP32 board, tested only with the ESP32-devkitc, but should work with others.
16+
2. A ESP32 or ESP8266 board, tested only with the ESP32-DevKitC and ESP8266-DevKitC but should work with others.
1617

1718
## Installation
1819
* Create a file called `wifissid.txt` in the root folder of this project, containing your wifi ssid.
@@ -24,5 +25,5 @@ This is how the web interface looks:
2425
* An example can be seen in `devices.example`.
2526
* Empty lines in this file are ignored.
2627
* Attach your ESP to your PC.
27-
* Run `pio run -t upload -e esp32dev` to compile this project and flash it to your ESP.
28-
* To later update it over WiFi run `pio ron -t upload -e ota`.
28+
* Run `pio run -t upload -e esp32dev` to compile this project and flash it to your ESP32 or `pio run -t upload -e esp_wroom_02` for ESP8266.
29+
* To later update it over WiFi run `pio ron -t upload -e esp32dev_ota` for ESP32 or `pio run -t upload -e esp_wroom_02_ota` for ESP8266.

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
* Add basic unit test(s)
44
* Add persistent config
55
* Ping device to check successful start?
6+
* Make ESP8266 print ipv6 addr if supported

lib/WakeOnLanGenerator/WakeOnLanGenerator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
#define LIB_WAKEONLANGENERATOR_H_
1313

1414
#include <regex>
15+
#ifdef ESP32
1516
#include <WiFi.h>
1617
#include <AsyncUDP.h>
18+
#elif defined(ESP8266)
19+
#include <ESP8266WiFi.h>
20+
#include <ESPAsyncUDP.h>
21+
#endif
1722

1823
static const std::regex MAC_REGEX(
1924
"[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}");

platformio.ini

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88
; Please visit documentation for the other options and examples
99
; https://docs.platformio.org/page/projectconf.html
1010

11-
[env:esp32dev]
12-
platform = espressif32
13-
board = esp32dev
11+
[env]
1412
framework = arduino
1513
monitor_speed = 115200
1614
upload_speed = 921600
17-
lib_deps =
18-
me-no-dev/ESP Async WebServer@^1.2.3
1915
board_build.embed_txtfiles =
2016
wifissid.txt
2117
wifipass.txt
@@ -26,8 +22,27 @@ board_build.embed_txtfiles =
2622
src/html/main.css
2723
src/html/not_found.html
2824

29-
[env:ota]
25+
[env:esp32dev]
26+
platform = espressif32
27+
board = esp32dev
28+
lib_deps =
29+
me-no-dev/ESP Async WebServer@^1.2.3
30+
31+
[env:esp32dev_ota]
3032
extends = env:esp32dev
3133
upload_protocol = espota
3234
upload_port = esp-wol.local
3335
extra_scripts = post:read_ota_pass.py
36+
37+
[env:esp_wroom_02]
38+
platform = espressif8266
39+
board = esp_wroom_02
40+
lib_deps =
41+
me-no-dev/ESPAsyncUDP
42+
me-no-dev/ESP Async WebServer@^1.2.3
43+
44+
[env:esp_wroom_02_ota]
45+
extends = env:esp_wroom_02
46+
upload_protocol = espota
47+
upload_port = esp-wol.local
48+
extra_scripts = post:read_ota_pass.py

read_ota_pass.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def main():
1414
with open(input) as f:
1515
password = f.readline()
1616

17+
password = password.strip('\00')
1718
env.Append(UPLOADERFLAGS=["-a", password])
1819

1920
main()

src/NetworkHandler.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
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;
2023
AsyncWebServer NetworkHandler::webServer(WEBSERVER_PORT);
21-
std::string NetworkHandler::indexHtml = INDEX_HTML;
2224
std::vector<std::string> NetworkHandler::deviceMacs;
2325
IPAddress NetworkHandler::targetBroadcast = DEFAULT_TARGET_BROADCAST;
2426
AsyncUDP 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

4551
void NetworkHandler::loop() {
4652
ArduinoOTA.handle();
4753
}
4854

55+
#ifdef ESP32
4956
void 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

78107
void NetworkHandler::setupWebServer() {
79108
std::string deviceMacs = DEVICE_MACS;
@@ -217,7 +246,7 @@ void NetworkHandler::onIndexPost(AsyncWebServerRequest *request) {
217246
}
218247

219248
std::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 += "\">";

src/NetworkHandler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
#include <string>
1515
#include <ESPAsyncWebServer.h>
16+
#ifdef ESP32
1617
#include <AsyncUDP.h>
18+
#elif defined(ESP8266)
19+
#include <ESPAsyncUDP.h>
20+
#endif
1721

1822
// Includes the content of the file "wifissid.txt" in the project root.
1923
// Make sure this file doesn't end with an empty line.
@@ -36,8 +40,8 @@ extern const char NOT_FOUND_HTML[] asm("_binary_src_html_not_found_html_start");
3640

3741
// WiFi constants
3842
static const char HOSTNAME[] = "esp-wol";
39-
static const IPAddress GATEWAY(192, 168, 2, 1);
40-
static const IPAddress SUBNET = IPADDR_NONE;
43+
static const IPAddress GATEWAY = IPADDR_ANY;
44+
static const IPAddress SUBNET = IPADDR_ANY;
4145

4246
// Web Server constants
4347
static const uint16_t WEBSERVER_PORT = 80;
@@ -57,11 +61,9 @@ class NetworkHandler {
5761

5862
// WiFi variables
5963
static IPAddress localhost;
60-
static IPv6Address localhost_ipv6;
6164

6265
// Web Server variables
6366
static AsyncWebServer webServer;
64-
static std::string indexHtml;
6567
static std::vector<std::string> deviceMacs;
6668
static IPAddress targetBroadcast;
6769

src/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#ifndef SRC_MAIN_H_
1212
#define SRC_MAIN_H_
1313

14+
#ifdef ESP32
1415
void setup();
1516
void loop();
17+
#endif
1618

1719
class main {
1820
public:

0 commit comments

Comments
 (0)