Files
ArduinoC6/wifi_set_and_server/wifi_set_and_server.ino
2025-03-05 08:11:34 +02:00

319 lines
11 KiB
C++

#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
const char* ap_ssid = "ESP32-AP"; // SSID for the Access Point mode
const char* ap_password = "12345678"; // Password for the Access Point mode
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
String ssid, password, static_ip, netmask, gateway, hostname; // Variables to store WiFi settings
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
// Handle the root URL ("/") and serve the configuration page
void handleRoot() {
String macAddress = getDefaultMacAddress();
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); }";
html += "h2 { text-align: center; color: #333; }";
html += "form { display: flex; flex-direction: column; }";
html += "label { margin-bottom: 10px; color: #555; }";
html += "input[type='text'], input[type='password'] { padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 4px; }";
html += "input[type='submit'] { padding: 10px; background-color: #28a745; color: #fff; border: none; border-radius: 4px; cursor: pointer; }";
html += "input[type='submit']:hover { background-color: #218838; }";
html += "button { padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; margin: 5px; }";
html += "button:hover { background-color: #0056b3; }";
html += "</style></head><body>";
if (isAPMode) {
html += "<div class='container'><h2>Board Configuration</h2>";
html += "<form action='/save' method='POST'>";
html += "<label for='ssid'>SSID:</label>";
html += "<input type='text' id='ssid' name='ssid' value='" + ssid + "'><br>";
html += "<label for='password'>Password:</label>";
html += "<input type='password' id='password' name='password' value='" + password + "'><br>";
html += "<label for='static_ip'>Static IP:</label>";
html += "<input type='text' id='static_ip' name='static_ip' value='" + static_ip + "'><br>";
html += "<label for='netmask'>Netmask:</label>";
html += "<input type='text' id='netmask' name='netmask' value='" + netmask + "'><br>";
html += "<label for='gateway'>Gateway:</label>";
html += "<input type='text' id='gateway' name='gateway' value='" + gateway + "'><br>";
html += "<label for='hostname'>Hostname:</label>";
html += "<input type='text' id='hostname' name='hostname' value='" + hostname + "'><br>";
html += "<label for='mac_address'>MAC Address:</label>";
html += "<input type='text' id='mac_address' name='mac_address' value='" + macAddress + "' readonly><br>";
html += "<input type='submit' value='Save'>";
html += "</form></div>";
}
// Add WiFi connection information if connected
if (WiFi.status() == WL_CONNECTED) {
html += "<div class='container'><h2>Connection Info</h2>";
html += "<p>Status: Connected</p>";
html += "<p>IP Address: " + WiFi.localIP().toString() + "</p>";
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>";
for (int i = 0; i < 4; i++) {
html += "<p>Relay " + String(i + 1) + ": " + (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 += "</div>";
} else {
html += "<div class='container'><h2>Connection Info</h2>";
html += "<p>Status: Not Connected</p>";
html += "</div>";
}
html += "</body></html>";
server.send(200, "text/html", html);
}
// Handle the save URL ("/save") and save the WiFi settings
void handleSave() {
ssid = server.arg("ssid");
password = server.arg("password");
static_ip = server.arg("static_ip");
netmask = server.arg("netmask");
gateway = server.arg("gateway");
hostname = server.arg("hostname");
saveSettings();
server.send(200, "text/html", "Settings saved. Device will restart in client mode.");
delay(2000);
ESP.restart();
}
// Handle the relay control URL ("/relay") and control the relays
void handleRelay() {
int relay = server.arg("relay").toInt();
String action = server.arg("action");
if (relay >= 0 && relay < 4) {
if (action == "on") {
digitalWrite(relayPins[relay], HIGH);
} else if (action == "off") {
digitalWrite(relayPins[relay], LOW);
}
}
server.sendHeader("Location", "/");
server.send(303);
}
// Save the WiFi settings to EEPROM
void saveSettings() {
EEPROM.writeString(0, ssid);
EEPROM.writeString(32, password);
EEPROM.writeString(64, static_ip);
EEPROM.writeString(96, netmask);
EEPROM.writeString(128, gateway);
EEPROM.writeString(160, hostname);
EEPROM.commit();
}
// Load the WiFi settings from EEPROM
void loadSettings() {
ssid = EEPROM.readString(0);
password = EEPROM.readString(32);
static_ip = EEPROM.readString(64);
netmask = EEPROM.readString(96);
gateway = EEPROM.readString(128);
hostname = EEPROM.readString(160);
}
void setup() {
Serial.begin(115200);
Serial.println("Setup started");
EEPROM.begin(192);
pinMode(userLedPin, OUTPUT);
digitalWrite(userLedPin, LOW);
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
for (int i = 0; i < 4; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Turn off all relays at startup
pinMode(inputPins[i], INPUT_PULLUP); // Set input pins as input with internal pull-up resistors
}
loadSettings();
// Stop AP mode if it was previously started
WiFi.softAPdisconnect(true);
// Check if the button is pressed at startup
if (digitalRead(buttonPin) == LOW) {
Serial.println("Button pressed at startup. Starting in AP mode.");
startAPMode();
} else {
if (ssid.length() > 0 && password.length() > 0) {
Serial.println("Attempting to connect to WiFi with saved credentials:");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
// Convert Strings to IPAddress objects
IPAddress ip, gw, nm;
if (!ip.fromString(static_ip) || !gw.fromString(gateway) || !nm.fromString(netmask)) {
Serial.println("Invalid IP configuration. Starting in AP mode.");
startAPMode();
return;
}
// Set hostname and static IP configuration
WiFi.config(ip, gw, nm);
WiFi.setHostname(hostname.c_str());
WiFi.begin(ssid.c_str(), password.c_str());
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startTime >= 10000) { // 10 seconds timeout
Serial.println("Failed to connect to WiFi. Starting in AP mode.");
startAPMode();
return;
}
delay(500);
}
Serial.println("Connected to WiFi.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Hostname: ");
Serial.println(WiFi.getHostname());
digitalWrite(userLedPin, HIGH); // Turn on the LED if connection is successful
isAPMode = false;
startWebServer();
} else {
Serial.println("No saved WiFi credentials found. Starting in AP mode.");
startAPMode();
}
}
Serial.println("Setup completed");
}
void loop() {
server.handleClient();
blinkLed();
printStatus();
}
// Blink the LED to indicate the mode (AP mode or client mode)
void blinkLed() {
static unsigned long lastBlinkTime = 0;
static bool ledState = LOW;
unsigned long interval = isAPMode ? 1000 : 3000; // 1 second interval for AP mode, 3 seconds for client mode
if (millis() - lastBlinkTime >= interval) {
ledState = !ledState;
digitalWrite(userLedPin, ledState);
lastBlinkTime = millis();
}
}
// Print the status of the board every 20 seconds
void printStatus() {
if (millis() - lastStatusPrintTime >= 20000) { // 20 seconds interval
Serial.println("Board Status:");
if (isAPMode) {
Serial.println("Mode: Access Point");
Serial.print("AP SSID: ");
Serial.println(ap_ssid);
Serial.print("AP IP Address: ");
Serial.println(WiFi.softAPIP());
} else {
Serial.println("Mode: Client");
Serial.print("Connected to SSID: ");
Serial.println(ssid);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Hostname: ");
Serial.println(WiFi.getHostname());
}
for (int i = 0; i < 4; i++) {
Serial.print("Relay ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF");
Serial.print("Input ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed");
}
Serial.println();
lastStatusPrintTime = millis();
}
}
// Start the Access Point mode
void startAPMode() {
WiFi.softAP(ap_ssid, ap_password);
Serial.println("Access Point Started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.on("/save", handleSave);
server.on("/relay", handleRelay); // Add handler for relay control
server.begin();
isAPMode = true;
}
// Start the web server in client mode
void startWebServer() {
server.on("/", handleRoot);
server.on("/save", handleSave);
server.on("/relay", handleRelay); // Add handler for relay control
server.begin();
}
// Get the default MAC address of the ESP32
String getDefaultMacAddress() {
String mac = "";
unsigned char mac_base[6] = {0};
if (esp_efuse_mac_get_default(mac_base) == ESP_OK) {
char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator
sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]);
mac = buffer;
}
return mac;
}
// Get the MAC address of a specific interface
String getInterfaceMacAddress(esp_mac_type_t interface) {
String mac = "";
unsigned char mac_base[6] = {0};
if (esp_read_mac(mac_base, interface) == ESP_OK) {
char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator
sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]);
mac = buffer;
}
return mac;
}