wifisettings
This commit is contained in:
@@ -1,29 +1,39 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h> // Include the WiFi library for ESP32
|
||||||
#include <WebServer.h>
|
#include <WebServer.h> // Include the WebServer library for ESP32
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h> // Include the EEPROM library for storing WiFi settings
|
||||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
#include <WiFiManager.h> // Include the WiFiManager library for managing WiFi connections
|
||||||
#include "esp_mac.h" // required - exposes esp_mac_type_t values
|
#include "esp_mac.h" // Include the esp_mac library for MAC address functions
|
||||||
//ver 0.0.2
|
|
||||||
|
// 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_ssid = "ESP32-AP"; // SSID for the Access Point mode
|
||||||
const char* ap_password = "12345678"; // Password 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 userLedPin = 8; // Define the pin for the user LED
|
||||||
const int buttonPin = 0; // Define the pin for the button
|
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 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
|
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
|
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
|
// Handle the root URL ("/") and serve the configuration page
|
||||||
void handleRoot() {
|
void handleRoot() {
|
||||||
|
// Get the default MAC address of the ESP32
|
||||||
String macAddress = getDefaultMacAddress();
|
String macAddress = getDefaultMacAddress();
|
||||||
|
|
||||||
|
// Start building the HTML response
|
||||||
String html = "<!DOCTYPE html><html><head><title>Configuration</title>";
|
String html = "<!DOCTYPE html><html><head><title>Configuration</title>";
|
||||||
html += "<meta http-equiv='refresh' content='1'>"; // Refresh the page every second
|
|
||||||
html += "<style>";
|
html += "<style>";
|
||||||
html += "body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; }";
|
html += "body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; }";
|
||||||
html += ".container { max-width: 600px; margin: 50px auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); }";
|
html += ".container { max-width: 600px; margin: 50px auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); }";
|
||||||
@@ -37,6 +47,7 @@ void handleRoot() {
|
|||||||
html += "button:hover { background-color: #0056b3; }";
|
html += "button:hover { background-color: #0056b3; }";
|
||||||
html += "</style></head><body>";
|
html += "</style></head><body>";
|
||||||
|
|
||||||
|
// If in AP mode, show the configuration form
|
||||||
if (isAPMode) {
|
if (isAPMode) {
|
||||||
html += "<div class='container'><h2>Board Configuration</h2>";
|
html += "<div class='container'><h2>Board Configuration</h2>";
|
||||||
html += "<form action='/save' method='POST'>";
|
html += "<form action='/save' method='POST'>";
|
||||||
@@ -58,7 +69,7 @@ void handleRoot() {
|
|||||||
html += "</form></div>";
|
html += "</form></div>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add WiFi connection information if connected
|
// If connected to WiFi, show connection information and relay/input status
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
html += "<div class='container'><h2>Connection Info</h2>";
|
html += "<div class='container'><h2>Connection Info</h2>";
|
||||||
html += "<p>Status: Connected</p>";
|
html += "<p>Status: Connected</p>";
|
||||||
@@ -66,31 +77,42 @@ void handleRoot() {
|
|||||||
html += "<p>Hostname: " + hostname + "</p>";
|
html += "<p>Hostname: " + hostname + "</p>";
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
|
|
||||||
// Add relay and input status with control buttons
|
// Add relay status with control buttons
|
||||||
html += "<div class='container'><h2>Relay and Input Status</h2>";
|
html += "<div class='container'><h2>Relay Status</h2>";
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
html += "<p>Relay " + String(i + 1) + ": " + (digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "</p>";
|
html += "<div class='container'><h3>Relay " + String(i + 1) + "</h3>";
|
||||||
|
html += "<p>Status: " + String(digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "</p>";
|
||||||
html += "<form action='/relay' method='POST'>";
|
html += "<form action='/relay' method='POST'>";
|
||||||
html += "<input type='hidden' name='relay' value='" + String(i) + "'>";
|
html += "<input type='hidden' name='relay' value='" + String(i) + "'>";
|
||||||
html += "<button type='submit' name='action' value='on'>Turn ON</button>";
|
html += "<button type='submit' name='action' value='on'>Turn ON</button>";
|
||||||
html += "<button type='submit' name='action' value='off'>Turn OFF</button>";
|
html += "<button type='submit' name='action' value='off'>Turn OFF</button>";
|
||||||
html += "</form>";
|
html += "</form></div>";
|
||||||
html += "<p>Input " + String(i + 1) + ": " + (digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "</p>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add input status
|
||||||
|
html += "<div class='container'><h2>Input Status</h2>";
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
html += "<div class='container'><h3>Input " + String(i + 1) + "</h3>";
|
||||||
|
html += "<p>Status: " + String(digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "</p>";
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// If not connected to WiFi, show not connected message
|
||||||
html += "<div class='container'><h2>Connection Info</h2>";
|
html += "<div class='container'><h2>Connection Info</h2>";
|
||||||
html += "<p>Status: Not Connected</p>";
|
html += "<p>Status: Not Connected</p>";
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// End the HTML response
|
||||||
html += "</body></html>";
|
html += "</body></html>";
|
||||||
|
|
||||||
|
// Send the HTML response to the client
|
||||||
server.send(200, "text/html", html);
|
server.send(200, "text/html", html);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the save URL ("/save") and save the WiFi settings
|
// Handle the save URL ("/save") and save the WiFi settings
|
||||||
void handleSave() {
|
void handleSave() {
|
||||||
|
// Get the WiFi settings from the form
|
||||||
ssid = server.arg("ssid");
|
ssid = server.arg("ssid");
|
||||||
password = server.arg("password");
|
password = server.arg("password");
|
||||||
static_ip = server.arg("static_ip");
|
static_ip = server.arg("static_ip");
|
||||||
@@ -98,8 +120,10 @@ void handleSave() {
|
|||||||
gateway = server.arg("gateway");
|
gateway = server.arg("gateway");
|
||||||
hostname = server.arg("hostname");
|
hostname = server.arg("hostname");
|
||||||
|
|
||||||
|
// Save the WiFi settings to EEPROM
|
||||||
saveSettings();
|
saveSettings();
|
||||||
|
|
||||||
|
// Send a response to the client and restart the device
|
||||||
server.send(200, "text/html", "Settings saved. Device will restart in client mode.");
|
server.send(200, "text/html", "Settings saved. Device will restart in client mode.");
|
||||||
delay(2000);
|
delay(2000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
@@ -107,9 +131,11 @@ void handleSave() {
|
|||||||
|
|
||||||
// Handle the relay control URL ("/relay") and control the relays
|
// Handle the relay control URL ("/relay") and control the relays
|
||||||
void handleRelay() {
|
void handleRelay() {
|
||||||
|
// Get the relay index and action from the form
|
||||||
int relay = server.arg("relay").toInt();
|
int relay = server.arg("relay").toInt();
|
||||||
String action = server.arg("action");
|
String action = server.arg("action");
|
||||||
|
|
||||||
|
// Control the relay based on the action
|
||||||
if (relay >= 0 && relay < 4) {
|
if (relay >= 0 && relay < 4) {
|
||||||
if (action == "on") {
|
if (action == "on") {
|
||||||
digitalWrite(relayPins[relay], HIGH);
|
digitalWrite(relayPins[relay], HIGH);
|
||||||
@@ -118,6 +144,7 @@ void handleRelay() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redirect the client back to the root URL
|
||||||
server.sendHeader("Location", "/");
|
server.sendHeader("Location", "/");
|
||||||
server.send(303);
|
server.send(303);
|
||||||
}
|
}
|
||||||
@@ -144,22 +171,23 @@ void loadSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200); // Start the serial communication
|
||||||
Serial.println("Setup started");
|
Serial.println("Setup started");
|
||||||
EEPROM.begin(192);
|
EEPROM.begin(192); // Initialize EEPROM with a size of 192 bytes
|
||||||
|
|
||||||
pinMode(userLedPin, OUTPUT);
|
pinMode(userLedPin, OUTPUT); // Set the user LED pin as output
|
||||||
digitalWrite(userLedPin, LOW);
|
digitalWrite(userLedPin, LOW); // Turn off the user LED
|
||||||
|
|
||||||
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
|
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++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
pinMode(relayPins[i], OUTPUT);
|
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
|
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
|
// Stop AP mode if it was previously started
|
||||||
WiFi.softAPdisconnect(true);
|
WiFi.softAPdisconnect(true);
|
||||||
@@ -169,6 +197,7 @@ void setup() {
|
|||||||
Serial.println("Button pressed at startup. Starting in AP mode.");
|
Serial.println("Button pressed at startup. Starting in AP mode.");
|
||||||
startAPMode();
|
startAPMode();
|
||||||
} else {
|
} else {
|
||||||
|
// Attempt to connect to WiFi with saved credentials
|
||||||
if (ssid.length() > 0 && password.length() > 0) {
|
if (ssid.length() > 0 && password.length() > 0) {
|
||||||
Serial.println("Attempting to connect to WiFi with saved credentials:");
|
Serial.println("Attempting to connect to WiFi with saved credentials:");
|
||||||
Serial.print("SSID: ");
|
Serial.print("SSID: ");
|
||||||
@@ -216,9 +245,9 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
server.handleClient();
|
server.handleClient(); // Handle client requests
|
||||||
blinkLed();
|
blinkLed(); // Blink the LED to indicate the mode
|
||||||
printStatus();
|
printStatus(); // Print the status of the board
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blink the LED to indicate the mode (AP mode or client mode)
|
// Blink the LED to indicate the mode (AP mode or client mode)
|
||||||
|
|||||||
Reference in New Issue
Block a user