This commit is contained in:
2025-03-06 14:38:19 +02:00
parent f9818d17d4
commit bffb708035
4 changed files with 0 additions and 226 deletions

Binary file not shown.

View File

@@ -1,3 +0,0 @@

View File

@@ -1,199 +0,0 @@
#include <WiFi.h>
#include <WebServer.h>
#include <EEPROM.h>
#include "esp_mac.h" // required - exposes esp_mac_type_t values
const char* ap_ssid = "ESP32-AP";
const char* ap_password = "12345678";
const int userLedPin = 8; // Define the pin for the user LED
const int buttonPin = 0; // Define the pin for the button
WebServer server(80);
String ssid, password, static_ip, hostname;
bool isAPMode = false;
void handleRoot() {
String macAddress = getDefaultMacAddress();
String html = "<!DOCTYPE html><html><head><title>Configuration</title>";
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 += "</style></head><body>";
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'><br>";
html += "<label for='password'>Password:</label>";
html += "<input type='password' id='password' name='password'><br>";
html += "<label for='static_ip'>Static IP:</label>";
html += "<input type='text' id='static_ip' name='static_ip'><br>";
html += "<label for='hostname'>Hostname:</label>";
html += "<input type='text' id='hostname' name='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></body></html>";
server.send(200, "text/html", html);
}
void handleSave() {
ssid = server.arg("ssid");
password = server.arg("password");
static_ip = server.arg("static_ip");
hostname = server.arg("hostname");
saveSettings();
server.send(200, "text/html", "Settings saved. Device will restart in client mode.");
delay(2000);
ESP.restart();
}
void handleInfo() {
String html = "<!DOCTYPE html><html><head><title>Info</title>";
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 += "</style></head><body>";
html += "<div class='container'><h2>Connection Info</h2>";
if (WiFi.status() == WL_CONNECTED) {
html += "<p>Status: Connected</p>";
html += "<p>IP Address: " + WiFi.localIP().toString() + "</p>";
} else {
html += "<p>Status: Not Connected</p>";
}
html += "</div></body></html>";
server.send(200, "text/html", html);
}
void saveSettings() {
EEPROM.writeString(0, ssid);
EEPROM.writeString(32, password);
EEPROM.writeString(64, static_ip);
EEPROM.writeString(96, hostname);
EEPROM.commit();
}
void loadSettings() {
ssid = EEPROM.readString(0);
password = EEPROM.readString(32);
static_ip = EEPROM.readString(64);
hostname = EEPROM.readString(96);
}
void setup() {
Serial.begin(115200);
Serial.println("Setup started");
EEPROM.begin(128);
pinMode(userLedPin, OUTPUT);
digitalWrite(userLedPin, LOW);
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
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);
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("Connecting to WiFi");
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startTime >= 10000) { // 10 seconds timeout
Serial.println("Failed to connect to WiFi.");
startAPMode();
return;
}
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("Connected to WiFi.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Hostname: ");
Serial.println(hostname);
digitalWrite(userLedPin, HIGH); // Turn on the LED if connection is successful
isAPMode = false;
} else {
Serial.println("No saved WiFi credentials found. Starting in AP mode.");
startAPMode();
}
}
Serial.println("Setup completed");
}
void loop() {
server.handleClient();
blinkLed();
}
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();
}
}
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("/info", handleInfo);
server.begin();
isAPMode = true;
}
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;
}
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;
}

View File

@@ -1,24 +0,0 @@
const int buttonPin = 0; // Assuming BUT1 is connected to GPIO 0
const int ledPin = 8; // Assuming the user LED is connected to GPIO 8
//test1
void setup() {
Serial.begin(115200);
Serial.println("Setup started");
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.println("Setup completed");
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the state of the button
if (buttonState == LOW) {
Serial.println("Button is pressed");
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
Serial.println("Button is not pressed");
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(2000); // Add a 2-second delay between readings
}