espwol.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /* Pin Definations */
  21. //On-board Programmable Button
  22. #define BTN_INPUT D0 //LOW = Pressed, HIGH = Released
  23. #define BTN_LED D7 //LED active low
  24. //ATX
  25. #define PWR_BTN D6
  26. #define RST_BTN D5
  27. #define HDD_LED D2
  28. #define PWR_LED D1
  29. /* WiFi & Web Server Related */
  30. WiFiManager wifiManager;
  31. ESP8266WebServer server(80);
  32. /* Discovery */
  33. #include <ESP8266mDNS.h>
  34. //To prevent collision, the device MAC will be appended to this name
  35. String MDNS_NAME = "espwol";
  36. /* Global Variables */
  37. bool hddLedState = 0;
  38. bool pwrLedState = 0;
  39. bool customBtnPressed = true;
  40. int val = 0;
  41. /* Function Prototypes */
  42. void handleRoot();
  43. void handleNotFound();
  44. void registerServeEndpoints();
  45. void handleCustomButtonEvents();
  46. void setup() {
  47. Serial.begin(115200);
  48. delay(100);
  49. /* Setup WiFi */
  50. wifiManager.setClass("invert");
  51. wifiManager.autoConnect("ESP-WakeOnLan");
  52. Serial.println("[INFO] Connected to WiFi!");
  53. /* Setup Pins */
  54. pinMode(PWR_LED, INPUT);
  55. pinMode(HDD_LED, INPUT);
  56. pinMode(PWR_BTN, OUTPUT);
  57. pinMode(RST_BTN, OUTPUT);
  58. pinMode(BTN_INPUT, INPUT);
  59. pinMode(BTN_LED, OUTPUT);
  60. /* Start LittleFS */
  61. if(!LittleFS.begin()){
  62. Serial.println("[ERROR] An Error has occurred while mounting LittleFS");
  63. return;
  64. }
  65. Serial.println("[INFO] LittleFS started");
  66. /* Start mDNS Discovery Service */
  67. String deviceMAC = WiFi.macAddress();
  68. deviceMAC.replace(":", "");
  69. MDNS_NAME = MDNS_NAME + "-" + deviceMAC;
  70. if (!MDNS.begin(MDNS_NAME)){
  71. Serial.println("[ERROR] mDNS start failed. Skipping.");
  72. }else{
  73. Serial.println("[INFO] mDNS started. Connect to your device using http://" + MDNS_NAME + ".local");
  74. MDNS.addService("http", "tcp", 80);
  75. }
  76. /* Bind web listener */
  77. registerServeEndpoints();
  78. server.begin();
  79. Serial.println("[INFO] HTTP server started");
  80. }
  81. void loop() {
  82. server.handleClient();
  83. handleCustomButtonEvents();
  84. updateFrontPanelLEDStatus();
  85. MDNS.update();
  86. }