wifisettings
This commit is contained in:
@@ -1,29 +1,39 @@
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
#include <EEPROM.h>
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
#include "esp_mac.h" // required - exposes esp_mac_type_t values
|
||||
//ver 0.0.2
|
||||
#include <WiFi.h> // Include the WiFi library for ESP32
|
||||
#include <WebServer.h> // Include the WebServer library for ESP32
|
||||
#include <EEPROM.h> // Include the EEPROM library for storing WiFi settings
|
||||
#include <WiFiManager.h> // 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 = "<!DOCTYPE html><html><head><title>Configuration</title>";
|
||||
html += "<meta http-equiv='refresh' content='1'>"; // Refresh the page every second
|
||||
html += "<style>";
|
||||
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); }";
|
||||
@@ -37,6 +47,7 @@ void handleRoot() {
|
||||
html += "button:hover { background-color: #0056b3; }";
|
||||
html += "</style></head><body>";
|
||||
|
||||
// If in AP mode, show the configuration form
|
||||
if (isAPMode) {
|
||||
html += "<div class='container'><h2>Board Configuration</h2>";
|
||||
html += "<form action='/save' method='POST'>";
|
||||
@@ -58,7 +69,7 @@ void handleRoot() {
|
||||
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) {
|
||||
html += "<div class='container'><h2>Connection Info</h2>";
|
||||
html += "<p>Status: Connected</p>";
|
||||
@@ -66,31 +77,42 @@ void handleRoot() {
|
||||
html += "<p>Hostname: " + hostname + "</p>";
|
||||
html += "</div>";
|
||||
|
||||
// Add relay and input status with control buttons
|
||||
html += "<div class='container'><h2>Relay and Input Status</h2>";
|
||||
// Add relay status with control buttons
|
||||
html += "<div class='container'><h2>Relay Status</h2>";
|
||||
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 += "<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='off'>Turn OFF</button>";
|
||||
html += "</form>";
|
||||
html += "<p>Input " + String(i + 1) + ": " + (digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "</p>";
|
||||
html += "</form></div>";
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// If not connected to WiFi, show not connected message
|
||||
html += "<div class='container'><h2>Connection Info</h2>";
|
||||
html += "<p>Status: Not Connected</p>";
|
||||
html += "</div>";
|
||||
}
|
||||
|
||||
// End the HTML response
|
||||
html += "</body></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)
|
||||
|
||||
Reference in New Issue
Block a user