From 820430a8b85e447f4b1be25716d22d4ee629047c Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:14:42 +0300 Subject: [PATCH 01/11] Use UDP to support WiFiUDP & EthernetUdp --- src/WakeOnLan.cpp | 2 +- src/WakeOnLan.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/WakeOnLan.cpp b/src/WakeOnLan.cpp index f325349..b33c202 100644 --- a/src/WakeOnLan.cpp +++ b/src/WakeOnLan.cpp @@ -1,6 +1,6 @@ #include "WakeOnLan.h" -WakeOnLan::WakeOnLan(WiFiUDP _udpSock) { +WakeOnLan::WakeOnLan(UDP _udpSock) { udpSock = _udpSock; } diff --git a/src/WakeOnLan.h b/src/WakeOnLan.h index 9711016..0767c82 100644 --- a/src/WakeOnLan.h +++ b/src/WakeOnLan.h @@ -1,19 +1,19 @@ #ifndef WakeOnLan_h #define WakeOnLan_h -#include +#include #include class WakeOnLan { private: - WiFiUDP udpSock; + UDP udpSock; IPAddress broadcastAddress = IPAddress(255, 255, 255, 255); uint8_t repeatPacket = 1; unsigned long delayPacket = 0; public: - WakeOnLan(WiFiUDP _udpSock); + WakeOnLan(UDP _udpSock); void setBroadcastAddress(IPAddress _broadcastAddress); void setRepeat(uint8_t _repeatPacket, unsigned long _delayPacket); From 6cfdaa6bd6c7dab832ebcac0bfeb8db74319ecca Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:19:36 +0300 Subject: [PATCH 02/11] Add Ethernet example --- .../WakeOnLan-ESP32-Ethernet.ino | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino diff --git a/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino b/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino new file mode 100644 index 0000000..4095ffe --- /dev/null +++ b/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino @@ -0,0 +1,48 @@ +#include +#include + +#include + +EthernetUdp UDP; +WakeOnLan WOL(UDP); + +const char* ssid = "your-ssid"; +const char* password = "your-password"; + +void wakeMyPC() { + const char *MACAddress = "01:23:45:67:89:AB"; + + WOL.sendMagicPacket(MACAddress); // Send Wake On Lan packet with the above MAC address. Default to port 9. + // WOL.sendMagicPacket(MACAddress, 7); // Change the port number +} + +void wakeOfficePC() { + const char *MACAddress = "01:23:45:67:89:AB"; + const char *secureOn = "FE:DC:BA:98:76:54"; + + WOL.sendSecureMagicPacket(MACAddress, secureOn); // Send Wake On Lan packet with the above MAC address and SecureOn feature. Default to port 9. + // WOL.sendSecureMagicPacket(MACAddress, secureOn, 7); // Change the port number +} + +void setup() +{ + WOL.setRepeat(3, 100); // Optional, repeat the packet three times with 100ms between. WARNING delay() is used between send packet function. + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); // Optional => To calculate the broadcast address, otherwise 255.255.255.255 is used (which is denied in some networks). + + wakeMyPC(); + wakeOfficePC(); +} + + +void loop() +{ +} From a4d9490ba85c29cd2588c649a138bfb7d27393e1 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:25:41 +0300 Subject: [PATCH 03/11] README include EthernetUdp example --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bf3d22b..4f45963 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,12 @@ To install the library in the PlatformIO IDE, use the library name like so: WiFiUDP UDP; ``` +#### (or) Include and initialize EthernetUdp +```cpp +#include +EthernetUdp UDP; +``` + #### Include and initialize WakeOnLan class ```cpp #include @@ -24,13 +30,19 @@ WakeOnLan WOL(UDP); // Pass WiFiUDP class #### Add this line in void setup() (optional) -`WOL.setRepeat(3, 100); // Repeat the packet three times with 100ms delay between` +```cpp +WOL.setRepeat(3, 100); // Repeat the packet three times with 100ms delay between +``` #### After connecting to WiFi successfully, Calculate and set the broadcast address (optional) -`WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());` +```cpp +WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); +``` -#### Manually set the broadcast address (optional) -`WOL.setBroadcastAddress("192.168.1.255");` +#### Manually set the broadcast address (optional; default to 255.255.255.255) +```cpp +WOL.setBroadcastAddress("192.168.1.255"); +``` ## **Usage** @@ -42,10 +54,14 @@ const char *MACAddress = "01:23:45:67:89:AB"; ``` ##### Send WOL UDP packet (Using the default port - 9) -`WOL.sendMagicPacket(MACAddress);` +```cpp +WOL.sendMagicPacket(MACAddress); +``` ##### Send WOL UDP packet (Use port 7) -`WOL.sendMagicPacket(MACAddress, 7);` +```cpp +WOL.sendMagicPacket(MACAddress, 7); +``` #### Set MAC address and SecureOn variables @@ -55,10 +71,14 @@ const char *secureOn = "FE:DC:BA:98:76:54"; ``` ##### Send WOL UDP packet with password (Using the default port - 9) -`WOL.sendSecureMagicPacket(MACAddress, secureOn);` +```cpp +WOL.sendSecureMagicPacket(MACAddress, secureOn); +``` ##### Send WOL UDP packet with password (Use port 7) -`WOL.sendSecureMagicPacket(MACAddress, secureOn, 7);` +```cpp +WOL.sendSecureMagicPacket(MACAddress, secureOn, 7); +``` ### **Send WOL from byte array MAC Address** @@ -68,10 +88,14 @@ uint8_t MAC[6] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; // 01:23:45:67:89:AB ``` ##### Send WOL UDP packet (Using the default port - 9) -`WOL.sendMagicPacket(MAC, sizeof(MAC));` +```cpp +WOL.sendMagicPacket(MAC, sizeof(MAC)); +``` ##### Send WOL UDP packet (Use port 7) -`WOL.sendMagicPacket(MAC, sizeof(MAC), 7);` +```cpp +WOL.sendMagicPacket(MAC, sizeof(MAC), 7); +``` #### Set MAC address and SecureOn in variable @@ -81,10 +105,14 @@ uint8_t SECURE_ON[6] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54}; // FE:DC:BA:98:76:5 ``` ##### Send WOL UDP packet with password (By default port 9) -`WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON));` +```cpp +WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON)); +``` ##### Send WOL UDP packet with password (Use port 7) -`WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON), 7);` +```cpp +WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON), 7); +``` ### **Generate magic packet** From 0b949ec67bee027b3fba8f90ba685bde9e7cfafb Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 19:43:37 +0300 Subject: [PATCH 04/11] Travis add Ethernet example --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 0462981..40efc93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,7 @@ cache: env: - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino PLATFORMIO_CI_EXTRA_ARGS="--board=esp32dev" - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino PLATFORMIO_CI_EXTRA_ARGS="--board=nodemcuv2" + - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino PLATFORMIO_CI_EXTRA_ARGS="--board=esp32dev" install: - pip install -U platformio From b5a86a7263ea5a44618b67e5940a6fdcdc914689 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 19:44:56 +0300 Subject: [PATCH 05/11] Pump version --- library.json | 2 +- library.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 8115d7e..f6fd479 100644 --- a/library.json +++ b/library.json @@ -14,7 +14,7 @@ "maintainer": true } ], - "version": "1.1.7", + "version": "1.2.8", "frameworks": "arduino", "platforms": "espressif32, espressif8266" } diff --git a/library.properties b/library.properties index e5efaa6..f193b46 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WakeOnLan -version=1.1.7 +version=1.2.8 author=a7md0 maintainer=a7md0 sentence=Generate and send Wake On Lan (WOL) packet over UDP protocol. From 23af597188ef4cd26c1e62b77470915b1d5dca1a Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 20:55:53 +0300 Subject: [PATCH 06/11] Create PlatformIO.yml Create CI --- .github/workflows/PlatformIO.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/PlatformIO.yml diff --git a/.github/workflows/PlatformIO.yml b/.github/workflows/PlatformIO.yml new file mode 100644 index 0000000..00ec6ad --- /dev/null +++ b/.github/workflows/PlatformIO.yml @@ -0,0 +1,29 @@ +name: PlatformIO CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + example: [examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino, examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino, examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino] + + steps: + - uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - uses: actions/setup-python@v4 + with: + python-version: '3.9' + - name: Install PlatformIO Core + run: pip install --upgrade platformio + + - name: Build examples + run: pio ci --board=esp32dev --board=nodemcuv2 --board=esp32dev + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} From dc5248d205ff9c7231127378a2c5b881ed2dad16 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:04:13 +0300 Subject: [PATCH 07/11] Fix CI --- .github/workflows/PlatformIO.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/PlatformIO.yml b/.github/workflows/PlatformIO.yml index 00ec6ad..ded4276 100644 --- a/.github/workflows/PlatformIO.yml +++ b/.github/workflows/PlatformIO.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - example: [examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino, examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino, examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino] + example: [[esp32dev, examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino], [nodemcuv2, examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino], [esp32dev, examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino]] steps: - uses: actions/checkout@v3 @@ -24,6 +24,6 @@ jobs: run: pip install --upgrade platformio - name: Build examples - run: pio ci --board=esp32dev --board=nodemcuv2 --board=esp32dev + run: pio ci --lib="." --board=${{ matrix.example[0] }} env: - PLATFORMIO_CI_SRC: ${{ matrix.example }} + PLATFORMIO_CI_SRC: ${{ matrix.example[1] }} From 3c21766e88f53c28f2cbf351e1f6549bcd460eae Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:04:23 +0300 Subject: [PATCH 08/11] Remove Travis --- .travis.yml | 45 --------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 40efc93..0000000 --- a/.travis.yml +++ /dev/null @@ -1,45 +0,0 @@ -# Continuous Integration (CI) is the practice, in software -# engineering, of merging all developer working copies with a shared mainline -# several times a day < https://docs.platformio.org/page/ci/index.html > -# -# Documentation: -# -# * Travis CI Embedded Builds with PlatformIO -# < https://docs.travis-ci.com/user/integration/platformio/ > -# -# * PlatformIO integration with Travis CI -# < https://docs.platformio.org/page/ci/travis.html > -# -# * User Guide for `platformio ci` command -# < https://docs.platformio.org/page/userguide/cmd_ci.html > -# -# -# Please choose one of the following templates (proposed below) and uncomment -# it (remove "# " before each line) or use own configuration according to the -# Travis CI documentation (see above). -# - -# -# Template #2: The project is intended to be used as a library with examples. -# - -language: python -python: - - "2.7" - -sudo: false -cache: - directories: - - "~/.platformio" - -env: - - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino PLATFORMIO_CI_EXTRA_ARGS="--board=esp32dev" - - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino PLATFORMIO_CI_EXTRA_ARGS="--board=nodemcuv2" - - PLATFORMIO_CI_SRC=examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino PLATFORMIO_CI_EXTRA_ARGS="--board=esp32dev" - -install: - - pip install -U platformio - - platformio update - -script: - - platformio ci --lib="." $PLATFORMIO_CI_EXTRA_ARGS \ No newline at end of file From bac9a81c03bbf9c11f71e136b9adf3a266b29ab5 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:30:23 +0300 Subject: [PATCH 09/11] Possible fix for abstract class issue --- src/WakeOnLan.cpp | 314 +++++++++++++++++++++++----------------------- src/WakeOnLan.h | 76 +++++------ 2 files changed, 195 insertions(+), 195 deletions(-) diff --git a/src/WakeOnLan.cpp b/src/WakeOnLan.cpp index b33c202..ff32ae7 100644 --- a/src/WakeOnLan.cpp +++ b/src/WakeOnLan.cpp @@ -1,157 +1,157 @@ -#include "WakeOnLan.h" - -WakeOnLan::WakeOnLan(UDP _udpSock) { - udpSock = _udpSock; -} - -void WakeOnLan::setBroadcastAddress(IPAddress _broadcastAddress) { - broadcastAddress = _broadcastAddress; -} - -void WakeOnLan::setRepeat(uint8_t _repeatPacket, unsigned long _delayPacket) { - repeatPacket = _repeatPacket; - delayPacket = _delayPacket; -} - -IPAddress WakeOnLan::calculateBroadcastAddress(IPAddress _ipAddress, IPAddress _subnetMask) { - for (int i = 0; i < 4; i++) - broadcastAddress[i] = ~_subnetMask[i] | _ipAddress[i]; - - return broadcastAddress; -} - -bool WakeOnLan::stringToArray(uint8_t* _macAddress, const char* _macString) { - char macFormat[23 + 1]; - unsigned int tempMACAddress[6]; - - if (strlen(_macString) == 12) // FFFFFFFFFFFF - sprintf(macFormat, "%%2x%%2x%%2x%%2x%%2x%%2x"); - else if (strlen(_macString) == 14) // FFFF-FFFF-FFFF - sprintf(macFormat, "%%2x%%2x%c%%2x%%2x%c%%2x%%2x", _macString[4], _macString[9]); - else if (strlen(_macString) == 17) // FF-FF-FF-FF-FF-FF - sprintf(macFormat, "%%2x%c%%2x%c%%2x%c%%2x%c%%2x%c%%2x", _macString[2], _macString[5], _macString[8], _macString[11], _macString[14]); - else - return false; - - int j = sscanf(_macString, (const char*)macFormat, &tempMACAddress[0], &tempMACAddress[1], &tempMACAddress[2], &tempMACAddress[3], &tempMACAddress[4], &tempMACAddress[5]); - if (j == 6) { - for (uint8_t i = 0; i < sizeof(tempMACAddress) / sizeof(*tempMACAddress); i++) - _macAddress[i] = (uint8_t)tempMACAddress[i]; - - return true; - } - - return false; -} - -bool WakeOnLan::sendMagicPacket(String _macString, uint16_t _portNum) { - return sendMagicPacket(_macString.c_str(), _portNum); -} - -bool WakeOnLan::sendSecureMagicPacket(String _macString, String _secureOn, uint16_t _portNum) { - return sendSecureMagicPacket(_macString.c_str(), _secureOn.c_str(), _portNum); -} - -bool WakeOnLan::sendMagicPacket(const char* _macAddress, uint16_t _portNum) { - uint8_t macAddress[6]; - - bool res = stringToArray(macAddress, _macAddress); - if (!res) - return false; - - return sendMagicPacket(macAddress, sizeof(macAddress), _portNum); -} - -bool WakeOnLan::sendSecureMagicPacket(const char* _macAddress, const char* _secureOn, uint16_t _portNum) { - uint8_t macAddress[6]; - uint8_t secureOn[6]; - - bool res = stringToArray(macAddress, _macAddress); - if (!res) - return false; - - bool res2 = stringToArray(secureOn, _secureOn); - if (!res2) - return false; - - return sendSecureMagicPacket(macAddress, sizeof(macAddress), secureOn, sizeof(secureOn), _portNum); -} - -bool WakeOnLan::sendMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint16_t portNum) { - size_t magicPacketSize = 6 + (6 * 16); // FF*6 + MAC*16 - uint8_t* magicPacket = new uint8_t[magicPacketSize]; - - int sucessNum = 0; - - generateMagicPacket(magicPacket, magicPacketSize, pMacAddress, sizeOfMacAddress); - - for (uint8_t i = 0; i < repeatPacket; i++) { - udpSock.beginPacket(broadcastAddress, portNum); - udpSock.write(magicPacket, magicPacketSize); - sucessNum += udpSock.endPacket(); - - if (delayPacket > 0) - delay(delayPacket); - } - - delete[] magicPacket; - - if (sucessNum == repeatPacket) - return true; - - return false; -} - -bool WakeOnLan::sendSecureMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn, uint16_t portNum) { - size_t magicPacketSize = 6 + (6 * 16) + 6; // FF*6 + MAC*16 + SecureOn - uint8_t* magicPacket = new uint8_t[magicPacketSize]; - - int sucessNum = 0; - - generateSecureMagicPacket(magicPacket, magicPacketSize, pMacAddress, sizeOfMacAddress, pSecureOn, sizeOfSecureOn); - - for (uint8_t i = 0; i < repeatPacket; i++) { - udpSock.beginPacket(broadcastAddress, portNum); - udpSock.write(magicPacket, magicPacketSize); - sucessNum += udpSock.endPacket(); - - if (delayPacket > 0) - delay(delayPacket); - } - - delete[] magicPacket; - - if (sucessNum == repeatPacket) - return true; - - return false; -} - -void WakeOnLan::generateMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress) { - uint8_t macAddress[6]; - - memcpy(macAddress, pMacAddress, sizeOfMacAddress); - memset(pMagicPacket, 0xFF, 6); - - for (uint8_t i = 0; i < 16; i++) { - uint8_t indx = (i + 1) * sizeOfMacAddress; - memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress); - } -} - -void WakeOnLan::generateSecureMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn) { - uint8_t macAddress[6]; - uint8_t secureOn[6]; - - memcpy(macAddress, pMacAddress, sizeOfMacAddress); - memcpy(secureOn, pSecureOn, sizeOfSecureOn); - - memset(pMagicPacket, 0xFF, 6); - - for (uint8_t i = 0; i < 16; i++) { - uint8_t indx = (i + 1) * sizeOfMacAddress; - memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress); - } - - memcpy(&pMagicPacket[17 * sizeOfSecureOn], &secureOn, sizeOfSecureOn); -} +#include "WakeOnLan.h" + +WakeOnLan::WakeOnLan(UDP& _udpSock) { + udpSock = &_udpSock; +} + +void WakeOnLan::setBroadcastAddress(IPAddress _broadcastAddress) { + broadcastAddress = _broadcastAddress; +} + +void WakeOnLan::setRepeat(uint8_t _repeatPacket, unsigned long _delayPacket) { + repeatPacket = _repeatPacket; + delayPacket = _delayPacket; +} + +IPAddress WakeOnLan::calculateBroadcastAddress(IPAddress _ipAddress, IPAddress _subnetMask) { + for (int i = 0; i < 4; i++) + broadcastAddress[i] = ~_subnetMask[i] | _ipAddress[i]; + + return broadcastAddress; +} + +bool WakeOnLan::stringToArray(uint8_t* _macAddress, const char* _macString) { + char macFormat[23 + 1]; + unsigned int tempMACAddress[6]; + + if (strlen(_macString) == 12) // FFFFFFFFFFFF + sprintf(macFormat, "%%2x%%2x%%2x%%2x%%2x%%2x"); + else if (strlen(_macString) == 14) // FFFF-FFFF-FFFF + sprintf(macFormat, "%%2x%%2x%c%%2x%%2x%c%%2x%%2x", _macString[4], _macString[9]); + else if (strlen(_macString) == 17) // FF-FF-FF-FF-FF-FF + sprintf(macFormat, "%%2x%c%%2x%c%%2x%c%%2x%c%%2x%c%%2x", _macString[2], _macString[5], _macString[8], _macString[11], _macString[14]); + else + return false; + + int j = sscanf(_macString, (const char*)macFormat, &tempMACAddress[0], &tempMACAddress[1], &tempMACAddress[2], &tempMACAddress[3], &tempMACAddress[4], &tempMACAddress[5]); + if (j == 6) { + for (uint8_t i = 0; i < sizeof(tempMACAddress) / sizeof(*tempMACAddress); i++) + _macAddress[i] = (uint8_t)tempMACAddress[i]; + + return true; + } + + return false; +} + +bool WakeOnLan::sendMagicPacket(String _macString, uint16_t _portNum) { + return sendMagicPacket(_macString.c_str(), _portNum); +} + +bool WakeOnLan::sendSecureMagicPacket(String _macString, String _secureOn, uint16_t _portNum) { + return sendSecureMagicPacket(_macString.c_str(), _secureOn.c_str(), _portNum); +} + +bool WakeOnLan::sendMagicPacket(const char* _macAddress, uint16_t _portNum) { + uint8_t macAddress[6]; + + bool res = stringToArray(macAddress, _macAddress); + if (!res) + return false; + + return sendMagicPacket(macAddress, sizeof(macAddress), _portNum); +} + +bool WakeOnLan::sendSecureMagicPacket(const char* _macAddress, const char* _secureOn, uint16_t _portNum) { + uint8_t macAddress[6]; + uint8_t secureOn[6]; + + bool res = stringToArray(macAddress, _macAddress); + if (!res) + return false; + + bool res2 = stringToArray(secureOn, _secureOn); + if (!res2) + return false; + + return sendSecureMagicPacket(macAddress, sizeof(macAddress), secureOn, sizeof(secureOn), _portNum); +} + +bool WakeOnLan::sendMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint16_t portNum) { + size_t magicPacketSize = 6 + (6 * 16); // FF*6 + MAC*16 + uint8_t* magicPacket = new uint8_t[magicPacketSize]; + + int sucessNum = 0; + + generateMagicPacket(magicPacket, magicPacketSize, pMacAddress, sizeOfMacAddress); + + for (uint8_t i = 0; i < repeatPacket; i++) { + udpSock->beginPacket(broadcastAddress, portNum); + udpSock->write(magicPacket, magicPacketSize); + sucessNum += udpSock->endPacket(); + + if (delayPacket > 0) + delay(delayPacket); + } + + delete[] magicPacket; + + if (sucessNum == repeatPacket) + return true; + + return false; +} + +bool WakeOnLan::sendSecureMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn, uint16_t portNum) { + size_t magicPacketSize = 6 + (6 * 16) + 6; // FF*6 + MAC*16 + SecureOn + uint8_t* magicPacket = new uint8_t[magicPacketSize]; + + int sucessNum = 0; + + generateSecureMagicPacket(magicPacket, magicPacketSize, pMacAddress, sizeOfMacAddress, pSecureOn, sizeOfSecureOn); + + for (uint8_t i = 0; i < repeatPacket; i++) { + udpSock->beginPacket(broadcastAddress, portNum); + udpSock->write(magicPacket, magicPacketSize); + sucessNum += udpSock->endPacket(); + + if (delayPacket > 0) + delay(delayPacket); + } + + delete[] magicPacket; + + if (sucessNum == repeatPacket) + return true; + + return false; +} + +void WakeOnLan::generateMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress) { + uint8_t macAddress[6]; + + memcpy(macAddress, pMacAddress, sizeOfMacAddress); + memset(pMagicPacket, 0xFF, 6); + + for (uint8_t i = 0; i < 16; i++) { + uint8_t indx = (i + 1) * sizeOfMacAddress; + memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress); + } +} + +void WakeOnLan::generateSecureMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn) { + uint8_t macAddress[6]; + uint8_t secureOn[6]; + + memcpy(macAddress, pMacAddress, sizeOfMacAddress); + memcpy(secureOn, pSecureOn, sizeOfSecureOn); + + memset(pMagicPacket, 0xFF, 6); + + for (uint8_t i = 0; i < 16; i++) { + uint8_t indx = (i + 1) * sizeOfMacAddress; + memcpy(&pMagicPacket[indx], &macAddress, sizeOfMacAddress); + } + + memcpy(&pMagicPacket[17 * sizeOfSecureOn], &secureOn, sizeOfSecureOn); +} diff --git a/src/WakeOnLan.h b/src/WakeOnLan.h index 0767c82..0de4d85 100644 --- a/src/WakeOnLan.h +++ b/src/WakeOnLan.h @@ -1,38 +1,38 @@ -#ifndef WakeOnLan_h -#define WakeOnLan_h - -#include -#include - -class WakeOnLan { - private: - UDP udpSock; - IPAddress broadcastAddress = IPAddress(255, 255, 255, 255); - - uint8_t repeatPacket = 1; - unsigned long delayPacket = 0; - - public: - WakeOnLan(UDP _udpSock); - - void setBroadcastAddress(IPAddress _broadcastAddress); - void setRepeat(uint8_t _repeatPacket, unsigned long _delayPacket); - - IPAddress calculateBroadcastAddress(IPAddress _ipAddress, IPAddress _subnetMask); - - bool stringToArray(uint8_t* _macAddress, const char* _macString); - - bool sendMagicPacket(String _macString, uint16_t _portNum = 9); - bool sendSecureMagicPacket(String _macString, String _secureOn, uint16_t _portNum = 9); - - bool sendMagicPacket(const char* _macAddress, uint16_t _portNum = 9); - bool sendSecureMagicPacket(const char* _macAddress, const char* _secureOn, uint16_t _portNum = 9); - - bool sendMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint16_t portNum = 9); - bool sendSecureMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn, uint16_t portNum = 9); - - void generateMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress); - void generateSecureMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn); -}; - -#endif +#ifndef WakeOnLan_h +#define WakeOnLan_h + +#include +#include + +class WakeOnLan { + private: + UDP* udpSock; + IPAddress broadcastAddress = IPAddress(255, 255, 255, 255); + + uint8_t repeatPacket = 1; + unsigned long delayPacket = 0; + + public: + WakeOnLan(UDP& _udpSock); + + void setBroadcastAddress(IPAddress _broadcastAddress); + void setRepeat(uint8_t _repeatPacket, unsigned long _delayPacket); + + IPAddress calculateBroadcastAddress(IPAddress _ipAddress, IPAddress _subnetMask); + + bool stringToArray(uint8_t* _macAddress, const char* _macString); + + bool sendMagicPacket(String _macString, uint16_t _portNum = 9); + bool sendSecureMagicPacket(String _macString, String _secureOn, uint16_t _portNum = 9); + + bool sendMagicPacket(const char* _macAddress, uint16_t _portNum = 9); + bool sendSecureMagicPacket(const char* _macAddress, const char* _secureOn, uint16_t _portNum = 9); + + bool sendMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint16_t portNum = 9); + bool sendSecureMagicPacket(uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn, uint16_t portNum = 9); + + void generateMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress); + void generateSecureMagicPacket(uint8_t*& pMagicPacket, size_t& sizeOfMagicPacket, uint8_t* pMacAddress, size_t sizeOfMacAddress, uint8_t* pSecureOn, size_t sizeOfSecureOn); +}; + +#endif From ecf5e37b1bef06bba722f0fb734145c4ce7b9ad4 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:45:36 +0300 Subject: [PATCH 10/11] Ethernet experiment --- .github/workflows/PlatformIO.yml | 2 +- .../WakeOnLan-ESP32-Ethernet.ino | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.github/workflows/PlatformIO.yml b/.github/workflows/PlatformIO.yml index ded4276..cb86c1f 100644 --- a/.github/workflows/PlatformIO.yml +++ b/.github/workflows/PlatformIO.yml @@ -24,6 +24,6 @@ jobs: run: pip install --upgrade platformio - name: Build examples - run: pio ci --lib="." --board=${{ matrix.example[0] }} + run: pio ci --lib="." --lib="Ethernet" --board=${{ matrix.example[0] }} env: PLATFORMIO_CI_SRC: ${{ matrix.example[1] }} diff --git a/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino b/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino index 4095ffe..354d76f 100644 --- a/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino +++ b/examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino @@ -1,4 +1,5 @@ -#include +#include +#include #include #include @@ -6,9 +7,6 @@ EthernetUdp UDP; WakeOnLan WOL(UDP); -const char* ssid = "your-ssid"; -const char* password = "your-password"; - void wakeMyPC() { const char *MACAddress = "01:23:45:67:89:AB"; @@ -24,19 +22,19 @@ void wakeOfficePC() { // WOL.sendSecureMagicPacket(MACAddress, secureOn, 7); // Change the port number } -void setup() -{ +void setup() { WOL.setRepeat(3, 100); // Optional, repeat the packet three times with 100ms between. WARNING delay() is used between send packet function. - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); + // the media access control (ethernet hardware) address for the shield: + byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; + Ethernet.begin(mac); - while (WiFi.status() != WL_CONNECTED) { + while (Ethernet.linkStatus() != LinkON) { delay(500); Serial.print("."); } - WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); // Optional => To calculate the broadcast address, otherwise 255.255.255.255 is used (which is denied in some networks). + WOL.calculateBroadcastAddress(Ethernet.localIP(), Ethernet.subnetMask()); // Optional => To calculate the broadcast address, otherwise 255.255.255.255 is used (which is denied in some networks). wakeMyPC(); wakeOfficePC(); @@ -45,4 +43,5 @@ void setup() void loop() { + Ethernet.maintain(); } From e6b6d00a0af6afa978aebfeb0e90c59c04dac6d1 Mon Sep 17 00:00:00 2001 From: Ahmed Naser <10885603+a7md0@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:55:15 +0300 Subject: [PATCH 11/11] Disable Ethernet example --- .github/workflows/PlatformIO.yml | 58 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/.github/workflows/PlatformIO.yml b/.github/workflows/PlatformIO.yml index cb86c1f..b822e24 100644 --- a/.github/workflows/PlatformIO.yml +++ b/.github/workflows/PlatformIO.yml @@ -1,29 +1,29 @@ -name: PlatformIO CI - -on: [push] - -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - example: [[esp32dev, examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino], [nodemcuv2, examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino], [esp32dev, examples/WakeOnLan-ESP32-Ethernet/WakeOnLan-ESP32-Ethernet.ino]] - - steps: - - uses: actions/checkout@v3 - - uses: actions/cache@v3 - with: - path: | - ~/.cache/pip - ~/.platformio/.cache - key: ${{ runner.os }}-pio - - uses: actions/setup-python@v4 - with: - python-version: '3.9' - - name: Install PlatformIO Core - run: pip install --upgrade platformio - - - name: Build examples - run: pio ci --lib="." --lib="Ethernet" --board=${{ matrix.example[0] }} - env: - PLATFORMIO_CI_SRC: ${{ matrix.example[1] }} +name: Build CI + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + example: [[esp32dev, examples/WakeOnLan-ESP32/WakeOnLan-ESP32.ino], [nodemcuv2, examples/WakeOnLan-ESP8266/WakeOnLan-ESP8266.ino]] + + steps: + - uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - uses: actions/setup-python@v4 + with: + python-version: '3.9' + - name: Install PlatformIO Core + run: pip install --upgrade platformio + + - name: Build examples + run: pio ci --lib="." --board=${{ matrix.example[0] }} + env: + PLATFORMIO_CI_SRC: ${{ matrix.example[1] }}