Last active
January 16, 2020 08:33
-
-
Save Inobtenio/e43f51eac9d83fed5e6ea88bec453612 to your computer and use it in GitHub Desktop.
Remote turn on and off a desktop PC whose motherboard was manually hacked. This is different from Wake-on-LAN.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <ArduinoOTA.h> | |
#include <ESP8266Ping.h> | |
#include <ArduinoJson.h> | |
//const char* ssid = "..."; | |
//const char* password = "..."; | |
ESP8266WebServer server(80); | |
const int led = 13; | |
const int inputLed = 16; | |
bool pcOn = false; | |
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20; | |
DynamicJsonBuffer jsonBuffer(bufferSize); | |
IPAddress ip(192, 168, 1, 130); | |
IPAddress gateway(192, 168, 1, 1); | |
IPAddress subnet(255, 255, 255, 0); | |
IPAddress pc_ip(192, 168, 1, 125); | |
void togglePcState() { | |
blinkLED(); | |
response(200, jsonActiveMessage(pcOn)); | |
} | |
void triggerPcState(){ | |
String activeParam; | |
for (int i=0; i<server.args(); i++){ | |
if (server.argName(i) == "plain"){ | |
activeParam = jsonBuffer.parseObject(server.arg(i))["active"].as<String>(); | |
} else if (server.argName(i) == "active") { | |
activeParam = server.arg(i); | |
} | |
} | |
String message; | |
for (uint8_t i=0; i<server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
Serial.print("Message: " + message); | |
Serial.print("Param: " + activeParam); | |
if ((pcOn and activeParam == "false") or (!pcOn and activeParam == "true")){ | |
blinkLED(); | |
} | |
response(200, jsonActiveMessage(pcOn)); | |
} | |
bool pcIsOn(){ | |
return Ping.ping(pc_ip, 1); | |
} | |
void getPcState(){ | |
response(200, jsonActiveMessage(pcOn)); | |
} | |
void response(int statusCode, String message){ | |
server.send(statusCode, "application/json", message); | |
} | |
void blinkLED(){ | |
digitalWrite(led, HIGH); | |
delay(250); | |
digitalWrite(led, LOW); | |
pcOn = !pcOn; | |
} | |
String jsonActiveMessage(bool state){ | |
String message = "{\"active\": \""; | |
message += (state)?"true":"false"; | |
message += "\"}"; | |
return message; | |
} | |
void handleNotFound(){ | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET)?"GET":"POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i=0; i<server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
} | |
void setup(void){ | |
pinMode(inputLed, INPUT); | |
pinMode(LED_BUILTIN, HIGH); | |
pinMode(led, OUTPUT); | |
WiFi.config(ip, gateway, subnet); | |
// WiFi.mode(WIFI_STA); | |
WiFi.begin(); | |
// WiFi.begin(ssid, password); | |
Serial.begin(115200); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
ArduinoOTA.onEnd([]() { | |
Serial.println("\nEnd"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}); | |
ArduinoOTA.onError([](ota_error_t error) { | |
Serial.printf("Error[%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
}); | |
ArduinoOTA.begin(); | |
Serial.println(""); | |
Serial.print("Connected to "); | |
// Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/home_pc", HTTP_GET, getPcState); | |
server.on("/home_pc", HTTP_POST, triggerPcState); | |
server.on("/home_pc/toggle", HTTP_POST, togglePcState); | |
// server.on("/inline", [](){ | |
// server.send(200, "text/plain", "this works as well"); | |
// }); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void handleButton(){ | |
int buttonState = digitalRead(inputLed); | |
if (buttonState == HIGH) { | |
blinkLED(); | |
} | |
} | |
void loop(void){ | |
handleButton(); | |
ArduinoOTA.handle(); | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment