ESP8266 Wake-On-LAN Button
Introduction
Are you also tired of always having to practically climb under your desk to turn on your PC? Well, i was! So i made this quick and simple Wake-On-LAN based internet of things button that turns on my PC!
In this guide i will walk you through the usage of Wake-On-LAN on an ESP8266!
Before starting this project make sure you are able to start your computer using the Wake-On-Lan protocol! Since it’s different for every computer i will not explain it in detail in this post. But the setting should be somewhere in your BIOS settings, power management, “wake on magic packet” or “WOL/Wake-On-LAN”. If u can’t figure it out google the model number of your motherboard!
Parts required
-Any ESP8266 based Board
-Button (Normally open) (With LED)
-Cables for Button
-USB Cable to power the Button
-Optional 3D-Printed case
Schematic
NodeMCU/ESP8266 | Button |
3.3v | Optional LED anode Pin |
GND | COM / Optional LED cathode Pin |
D5 | NO |
Code
We are going to program the ESP8266 NodeMCU using the Arduino IDE. You need to install the ESP8266 board in to the IDE and know how to program to it. For this you can follow the following tutorial:
https://randomnerdtutorials.com/installing-esp8266-nodemcu-arduino-ide-2-0/
In order to use WIFi and to communicate with the Wake-On-LAN protocol you need to install a couple libraries first.
Follow the next steps to install these libraries:
-ESP8266WiFi
-WiFiUdp
-WakeOnLan
Open the Arduino IDE and go to Sketch -> Include Library -> Manage Libraries. Here search for the above Libraries and install them one by one.
After installing the required libraries and making sure your Arduino IDE is set-up copy the following code to your Arduino IDE. Don’t upload it yet. You first need to make some changes to make it work!
/*
* ioTechProjects.com 2023
*
* WOL-Button
*
* u are free to edit this code
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WakeOnLan.h>
WiFiUDP UDP;
WakeOnLan WOL(UDP);
// Network Info
const char* ssid = "SSID";
const char* password = "PASSWORD";
void wakeMyPC() {
// Mac Adress
const char *MACAddress = "XX:XX:XX:XX:XX:XX";
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 setup()
{
pinMode(D5, INPUT_PULLUP);
WOL.setRepeat(3, 100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
//wakeMyPC(); // Optional wake pc at startup
}
void loop()
{
int button = digitalRead(D5);
if (button == LOW) {
Serial.print("button pressed");
wakeMyPC();
}
}
Code changes
In order for the code to work properly some parameters need to be changed before uploading.
SSID -> Your wifi Name
PASSWORD -> Your wifi Password
XX:XX:XX:XX:XX:XX -> the Mac adress of your computer.
If u know that Wake-On-LAN functions on your pc you should already have it!
After you changed these parameters you can upload the code to the ESP8266!
// Network Info
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Mac Adress
const char *MACAddress = "XX:XX:XX:XX:XX:XX";
Wrapping Up
Now plug in your ESP8266 board! After that you should be able to wake your computer by pushing the button!
To make everything connect together perfectly and to make it look clean i 3D-Printed a box for around the NodeMCU and a bracket for the push button to be able to mount it under my desk.
STL Files on thingiverse: https://www.thingiverse.com/thing:5981105