diff --git a/wifi_set_and_server/wifi_set_and_server.ino b/wifi_set_and_server/wifi_set_and_server.ino index 76ddb2e..1afc796 100644 --- a/wifi_set_and_server/wifi_set_and_server.ino +++ b/wifi_set_and_server/wifi_set_and_server.ino @@ -1,29 +1,39 @@ -#include -#include -#include -#include // https://github.com/tzapu/WiFiManager -#include "esp_mac.h" // required - exposes esp_mac_type_t values -//ver 0.0.2 +#include // Include the WiFi library for ESP32 +#include // Include the WebServer library for ESP32 +#include // Include the EEPROM library for storing WiFi settings +#include // Include the WiFiManager library for managing WiFi connections +#include "esp_mac.h" // Include the esp_mac library for MAC address functions + +// Version of the code +//ver 0.0.4 + +// Constants for Access Point mode const char* ap_ssid = "ESP32-AP"; // SSID for the Access Point mode const char* ap_password = "12345678"; // Password for the Access Point mode + +// Pin definitions const int userLedPin = 8; // Define the pin for the user LED const int buttonPin = 0; // Define the pin for the button - const int relayPins[4] = {10, 11, 22, 23}; // Define the pins for the relays const int inputPins[4] = {1, 2, 3, 15}; // Define the pins for the inputs -WebServer server(80); // Create a web server on port 80 +// Create a web server on port 80 +WebServer server(80); -String ssid, password, static_ip, netmask, gateway, hostname; // Variables to store WiFi settings +// Variables to store WiFi settings +String ssid, password, static_ip, netmask, gateway, hostname; bool isAPMode = false; // Flag to indicate if the board is in AP mode -unsigned long lastStatusPrintTime = 0; // Variable to keep track of the last status print time +// Variable to keep track of the last status print time +unsigned long lastStatusPrintTime = 0; // Handle the root URL ("/") and serve the configuration page void handleRoot() { + // Get the default MAC address of the ESP32 String macAddress = getDefaultMacAddress(); + + // Start building the HTML response String html = "Configuration"; - html += ""; // Refresh the page every second html += ""; + // If in AP mode, show the configuration form if (isAPMode) { html += "

Board Configuration

"; html += "
"; @@ -58,7 +69,7 @@ void handleRoot() { html += "
"; } - // Add WiFi connection information if connected + // If connected to WiFi, show connection information and relay/input status if (WiFi.status() == WL_CONNECTED) { html += "

Connection Info

"; html += "

Status: Connected

"; @@ -66,31 +77,42 @@ void handleRoot() { html += "

Hostname: " + hostname + "

"; html += "
"; - // Add relay and input status with control buttons - html += "

Relay and Input Status

"; + // Add relay status with control buttons + html += "

Relay Status

"; for (int i = 0; i < 4; i++) { - html += "

Relay " + String(i + 1) + ": " + (digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "

"; + html += "

Relay " + String(i + 1) + "

"; + html += "

Status: " + String(digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "

"; html += "
"; html += ""; html += ""; html += ""; - html += "
"; - html += "

Input " + String(i + 1) + ": " + (digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "

"; + html += "
"; + } + + // Add input status + html += "

Input Status

"; + for (int i = 0; i < 4; i++) { + html += "

Input " + String(i + 1) + "

"; + html += "

Status: " + String(digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "

"; + html += "
"; } - html += "
"; } else { + // If not connected to WiFi, show not connected message html += "

Connection Info

"; html += "

Status: Not Connected

"; html += "
"; } + // End the HTML response html += ""; + // Send the HTML response to the client server.send(200, "text/html", html); } // Handle the save URL ("/save") and save the WiFi settings void handleSave() { + // Get the WiFi settings from the form ssid = server.arg("ssid"); password = server.arg("password"); static_ip = server.arg("static_ip"); @@ -98,8 +120,10 @@ void handleSave() { gateway = server.arg("gateway"); hostname = server.arg("hostname"); + // Save the WiFi settings to EEPROM saveSettings(); + // Send a response to the client and restart the device server.send(200, "text/html", "Settings saved. Device will restart in client mode."); delay(2000); ESP.restart(); @@ -107,9 +131,11 @@ void handleSave() { // Handle the relay control URL ("/relay") and control the relays void handleRelay() { + // Get the relay index and action from the form int relay = server.arg("relay").toInt(); String action = server.arg("action"); + // Control the relay based on the action if (relay >= 0 && relay < 4) { if (action == "on") { digitalWrite(relayPins[relay], HIGH); @@ -118,6 +144,7 @@ void handleRelay() { } } + // Redirect the client back to the root URL server.sendHeader("Location", "/"); server.send(303); } @@ -144,22 +171,23 @@ void loadSettings() { } void setup() { - Serial.begin(115200); + Serial.begin(115200); // Start the serial communication Serial.println("Setup started"); - EEPROM.begin(192); + EEPROM.begin(192); // Initialize EEPROM with a size of 192 bytes - pinMode(userLedPin, OUTPUT); - digitalWrite(userLedPin, LOW); + pinMode(userLedPin, OUTPUT); // Set the user LED pin as output + digitalWrite(userLedPin, LOW); // Turn off the user LED pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor + // Set the relay pins as output and turn off all relays at startup for (int i = 0; i < 4; i++) { pinMode(relayPins[i], OUTPUT); - digitalWrite(relayPins[i], LOW); // Turn off all relays at startup + digitalWrite(relayPins[i], LOW); pinMode(inputPins[i], INPUT_PULLUP); // Set input pins as input with internal pull-up resistors } - loadSettings(); + loadSettings(); // Load the WiFi settings from EEPROM // Stop AP mode if it was previously started WiFi.softAPdisconnect(true); @@ -169,6 +197,7 @@ void setup() { Serial.println("Button pressed at startup. Starting in AP mode."); startAPMode(); } else { + // Attempt to connect to WiFi with saved credentials if (ssid.length() > 0 && password.length() > 0) { Serial.println("Attempting to connect to WiFi with saved credentials:"); Serial.print("SSID: "); @@ -216,9 +245,9 @@ void setup() { } void loop() { - server.handleClient(); - blinkLed(); - printStatus(); + server.handleClient(); // Handle client requests + blinkLed(); // Blink the LED to indicate the mode + printStatus(); // Print the status of the board } // Blink the LED to indicate the mode (AP mode or client mode)