espwol.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * RESTful Wake On Lan
  3. * Author: Toby Chui
  4. *
  5. * This firmware provide a web interface / RESTFUL API
  6. * request for remote power-on / off or reset your computer
  7. * by emulating button press on the front-panel headers
  8. *
  9. * The following firmware config are recommended
  10. * Board: Wemos D1 Mini
  11. * CPU clockspeed: 160Mhz
  12. * IwIP Varient: v2 Higher Bandwidth
  13. *
  14. */
  15. #include <ESP8266WiFi.h>
  16. #include <WiFiManager.h>
  17. #include <ESP8266mDNS.h>
  18. #include <ArduinoJson.h>
  19. #include <LittleFS.h>
  20. #include <WiFiUdp.h>
  21. #include <WakeOnLan.h>
  22. /* Pin Definations */
  23. //On-board Programmable Button
  24. #define BTN_INPUT D0 //LOW = Pressed, HIGH = Released
  25. #define BTN_LED D7 //LED active low
  26. //ATX
  27. #define PWR_BTN D6
  28. #define RST_BTN D5
  29. #define HDD_LED D2
  30. #define PWR_LED D1
  31. /* WiFi & Web Server Related */
  32. WiFiManager wifiManager;
  33. ESP8266WebServer server(80);
  34. WiFiUDP UDP;
  35. WakeOnLan WOL(UDP);
  36. /* Discovery */
  37. #include <ESP8266mDNS.h>
  38. //To prevent collision, the device MAC will be appended to this name
  39. String MDNS_NAME = "espwol";
  40. const char* DB_PATH = "/conf.txt"; // File path for storing the string data
  41. /* Global Variables */
  42. bool hddLedState = 0;
  43. bool pwrLedState = 0;
  44. bool customBtnPressed = true;
  45. int val = 0;
  46. /* Function Prototypes */
  47. void handleRoot();
  48. void handleNotFound();
  49. void registerServeEndpoints();
  50. void handleCustomButtonEvents();
  51. void setup() {
  52. Serial.begin(115200);
  53. delay(100);
  54. /* Setup WiFi */
  55. wifiManager.setClass("invert");
  56. wifiManager.autoConnect("ESP-WakeOnLan");
  57. Serial.println("[INFO] Connected to WiFi!");
  58. /* Setup Pins */
  59. pinMode(PWR_LED, INPUT);
  60. pinMode(HDD_LED, INPUT);
  61. pinMode(PWR_BTN, OUTPUT);
  62. pinMode(RST_BTN, OUTPUT);
  63. pinMode(BTN_INPUT, INPUT);
  64. pinMode(BTN_LED, OUTPUT);
  65. /* Start LittleFS */
  66. if(!LittleFS.begin()){
  67. Serial.println("[ERROR] An Error has occurred while mounting LittleFS");
  68. return;
  69. }
  70. Serial.println("[INFO] LittleFS started");
  71. /* Start mDNS Discovery Service */
  72. String deviceMAC = WiFi.macAddress();
  73. deviceMAC.replace(":", "");
  74. MDNS_NAME = MDNS_NAME + "-" + deviceMAC;
  75. if (!MDNS.begin(MDNS_NAME)){
  76. Serial.println("[ERROR] mDNS start failed. Skipping.");
  77. }else{
  78. Serial.println("[INFO] mDNS started. Connect to your device using http://" + MDNS_NAME + ".local");
  79. MDNS.addService("http", "tcp", 80);
  80. }
  81. /* Wake On Lan Settings */
  82. WOL.setRepeat(3, 100);
  83. WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
  84. /* Bind web listener */
  85. registerServeEndpoints();
  86. server.begin();
  87. Serial.println("[INFO] HTTP server started");
  88. }
  89. void loop() {
  90. server.handleClient();
  91. handleCustomButtonEvents();
  92. updateFrontPanelLEDStatus();
  93. MDNS.update();
  94. }