Browse Source

added arduino code

Toby Chui 1 year ago
parent
commit
0c11c85af7
5 changed files with 250 additions and 0 deletions
  1. 43 0
      espwol/README.md
  2. 17 0
      espwol/data/index.html
  3. 95 0
      espwol/espwol.ino
  4. 64 0
      espwol/sequences.ino
  5. 31 0
      espwol/webserver.ino

+ 43 - 0
espwol/README.md

@@ -0,0 +1,43 @@
+## Uploading Files to ESP8266 via Arduino IDE
+
+This guide will walk you through the process of uploading files, such as HTML, CSS, JavaScript, images, etc., to an ESP8266 microcontroller using the Arduino IDE.
+
+### Prerequisites
+
+- Arduino IDE installed on your computer.
+- ESP8266 board selected and installed in the Arduino IDE. You can follow [this guide](https://arduino-esp8266.readthedocs.io/en/latest/installing.html) for installation instructions.
+- Basic familiarity with Arduino programming.
+
+### Steps
+
+1. **Prepare your files**: Add the files you want to upload to the ESP8266. These could be HTML, CSS, JavaScript, images, or any other files you need for your project.
+
+2. **Check if the data folder exists**: In your Arduino sketch folder, there should be a folder named `data` (all lowercase). This folder will hold the files you want to upload.
+
+3. **Place your files in the data folder**: Copy or move your files into the `data` folder. You can organize them into subdirectories if needed.
+
+4. **Upload files using the Arduino IDE**:
+   
+   - Open your Arduino sketch (or create a new one).
+   - Make sure your ESP8266 board is selected in the Arduino IDE.
+   - Go to `Tools` > `ESP8266 LittleFS Data Upload`. This will compress the `data` folder and upload it to the ESP8266's SPIFFS or LittleFS filesystem.
+   
+5. **Access uploaded files in your sketch**:
+   
+   - You can access the uploaded files using functions like `LittleFS.open()` in your sketch. For example:
+     ```cpp
+     File file = LittleFS.open("/filename.txt", "r");
+     ```
+   
+6. **Write your sketch**: Now you can write your Arduino sketch as usual, add custom buttons actions and access the uploaded files from the SPIFFS or LittleFS filesystem.
+
+### Additional Notes
+
+- If you encounter any issues during the upload process, make sure you have the correct ESP8266 board selected in the Arduino IDE and that your sketch folder structure is set up correctly.
+- Keep in mind the limitations of the SPIFFS or LittleFS filesystem in terms of available space. Check the documentation for your ESP8266 board to determine the available storage capacity.
+- Remember to include error handling in your sketch to handle cases where files cannot be opened or accessed.
+
+### Resources
+
+- [LittleFS Tutorials](https://randomnerdtutorials.com/esp8266-nodemcu-write-data-littlefs-arduino/)
+- [LittleFS Documentation](https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html#littlefs)

+ 17 - 0
espwol/data/index.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1.0">
+	<meta http-equiv="X-UA-Compatible" content="ie=edge">
+	<title>ESP Wake On LAN</title>
+	<style>
+	  
+	</style>
+</head>
+<body>
+<h1>HELLO WORLD</h1>
+<p>This is a testing page for the ESPWoL system </p>
+
+</body>
+</html>

+ 95 - 0
espwol/espwol.ino

@@ -0,0 +1,95 @@
+/*
+ *  RESTful Wake On Lan 
+ *  Author: Toby Chui
+ * 
+ *  This firmware provide a web interface / RESTFUL API
+ *  request for remote power-on / off or reset your computer
+ *  by emulating button press on the front-panel headers
+ *  
+ *  The following firmware config are recommended
+ *  Board: Wemos D1 Mini
+ *  CPU clockspeed: 160Mhz
+ *  IwIP Varient: v2 Higher Bandwidth
+ * 
+ */
+
+#include <ESP8266WiFi.h>        
+#include <WiFiManager.h>
+#include <ArduinoJson.h>
+#include <LittleFS.h>
+
+/* Pin Definations */
+//On-board Programmable Button
+#define BTN_INPUT D0 //LOW = Pressed, HIGH = Released
+#define BTN_LED D7 //LED active low
+//ATX
+#define PWR_BTN D6
+#define RST_BTN D5
+#define HDD_LED D2
+#define PWR_LED D1
+
+/* WiFi & Web Server Related */
+WiFiManager wifiManager;
+ESP8266WebServer server(80);
+
+/* Discovery */
+#include <ESP8266mDNS.h>
+String MDNS_NAME = "espwol";
+
+/* Global Variables */
+bool hddLedState = 0;
+bool pwrLedState = 0;
+bool customBtnPressed = true; 
+int val = 0;
+
+/* Function Prototypes */
+void handleRoot();
+void handleNotFound();
+void registerServeEndpoints();
+void handleCustomButtonEvents();
+
+void setup() {
+  Serial.begin(115200);
+  delay(100);
+  
+  /* Setup WiFi */
+  wifiManager.setClass("invert");
+  wifiManager.autoConnect("ESP-WakeOnLan");
+  Serial.println("[INFO] Connected to WiFi!");
+
+  /* Setup Pins */
+  pinMode(PWR_LED, INPUT);
+  pinMode(HDD_LED, INPUT);
+  pinMode(PWR_BTN, OUTPUT);
+  pinMode(RST_BTN, OUTPUT);
+  pinMode(BTN_INPUT, INPUT);
+  pinMode(BTN_LED, OUTPUT);
+
+  /* Start LittleFS */
+  if(!LittleFS.begin()){
+    Serial.println("[ERROR] An Error has occurred while mounting LittleFS");
+    return;
+  }
+  Serial.println("[INFO] LittleFS started");
+
+  /* Start mDNS Discovery Service */
+  if (!MDNS.begin(MDNS_NAME)){
+      Serial.println("[ERROR] mDNS start failed. Skipping.");
+  }else{
+      Serial.println("[INFO] mDNS started. Connect to your webstick using http://" + MDNS_NAME + ".local");
+      MDNS.addService("http", "tcp", 80);
+  }
+  
+  /* Bind web listener */
+  registerServeEndpoints();
+  server.begin();                           
+  Serial.println("[INFO] HTTP server started");
+}
+
+void loop() {
+  server.handleClient();
+  handleCustomButtonEvents();
+  Serial.print(hddLedState);
+  Serial.print(",");
+  Serial.println(pwrLedState);
+}

+ 64 - 0
espwol/sequences.ino

@@ -0,0 +1,64 @@
+/*
+
+   sequences.ino
+
+   This file contains the sequences for handling the power up / down
+   reset logic as well as reading the status LEDs from front-panel headers
+*/
+
+/* 
+  Programmable Buttons Functions 
+  
+  Edit the customButtonOnPress and customButtonOnRelease function
+  to fit your needs. 
+  Leave it as default for acting like a power button.
+  
+*/
+void customButtonOnPress(){
+  digitalWrite(PWR_BTN, HIGH);
+  digitalWrite(BTN_LED, LOW);
+}
+
+void customButtonOnRelease(){
+  digitalWrite(PWR_BTN, LOW);
+  digitalWrite(BTN_LED, HIGH);
+}
+
+//Handle call to press / release function events
+void handleCustomButtonEvents(){
+
+  //Note: The button pin is pulled low when button is pressed
+  int btnPinState = digitalRead(BTN_INPUT);
+  if (btnPinState == HIGH && customBtnPressed == true){
+    //The button has just been released. Call to the onRelease event
+    customBtnPressed = false;
+    customButtonOnRelease();
+  }else if (btnPinState == LOW && customBtnPressed == false){
+    //The button has just been pressed. Call to the onPress event
+    customBtnPressed = true;
+    customButtonOnPress();
+  }
+}
+
+//Read the current front panel LED state and store to global variables
+void updateFrontPanelLEDStatus() {
+  val = digitalRead(HDD_LED);
+  hddLedState = (val == LOW); //Active low
+
+  val = digitalRead(PWR_LED);
+  pwrLedState = (val == LOW); //Active low
+}
+
+//Emulate power button press
+void pressPowerButton() {
+  digitalWrite(PWR_BTN, HIGH);
+  delay(1000);
+  digitalWrite(PWR_BTN, LOW);
+}
+
+//Emulate reset button perss
+void pressResetButton() {
+  digitalWrite(RST_BTN, HIGH);
+  delay(1000);
+  digitalWrite(RST_BTN, LOW);
+}

+ 31 - 0
espwol/webserver.ino

@@ -0,0 +1,31 @@
+/*
+ * 
+ *  Webserver.ino
+ *  
+ *  This script handle web serving and RESTFUL API features
+ */
+
+void registerServeEndpoints(){
+  server.serveStatic("/", LittleFS, "/index.html"); 
+  server.onNotFound(handleNotFound);
+}
+
+void handleRoot() {
+  //server.send(200, "text/plain", "Hello world!"); 
+}
+
+
+void handleNotFound() {
+  String message = "404 - 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);
+}