-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodemcu-wol.ino
88 lines (74 loc) · 1.93 KB
/
nodemcu-wol.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <WakeOnLan.h>
const char* ssid = "wifi-ssid";
const char* password = "wifi-pwd";
boolean wifiConnected = false;
WiFiUDP UDP;
/**
* The Magic Packet needs to be sent as BROADCAST in the LAN
*/
IPAddress computer_ip(255,255,255,255);
/**
* The targets MAC address to send the packet to
*/
byte mac[] = { 0xMA, 0xCA, 0xDD, 0xRE, 0xSS, 0x11 };
boolean connectWifi(); //empty methods defined here, for declaration see lower.
void sendWOL();
void setup() {
// Initialise Serial connection
Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();
UDP.begin(9); //start UDP client, not sure if really necessary.
}
void loop()
{
if (wifiConnected) {
Serial.println("Requesting...");
HTTPClient http;
http.begin("http://api.strempfer.works/blake/wol");
http.addHeader("Content-Type", "text/plain");
int httpCode = http.POST("");
int payload = http.getString().toInt();
Serial.println(payload);
http.end();
if(payload == 1) {
Serial.println("Sending WOL Packet...");
WakeOnLan::sendWOL(computer_ip, UDP, mac, sizeof mac);
}
delay(4000); //sending WOL packets every 4th second.
}
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
boolean state = true;
int i = 0;
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 20) {
state = false;
break;
}
i++;
}
if (state) {
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}