updated
This commit is contained in:
@@ -3,9 +3,10 @@
|
||||
#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
|
||||
#include <HTTPClient.h> // Include the HTTPClient library for sending logs
|
||||
|
||||
// Version of the code
|
||||
//ver 0.0.4
|
||||
//ver 0.0.11
|
||||
|
||||
// Constants for Access Point mode
|
||||
const char* ap_ssid = "ESP32-AP"; // SSID for the Access Point mode
|
||||
@@ -22,10 +23,30 @@ WebServer server(80);
|
||||
|
||||
// Variables to store WiFi settings
|
||||
String ssid, password, static_ip, netmask, gateway, hostname;
|
||||
String logServerIP, logServerPort;
|
||||
bool isAPMode = false; // Flag to indicate if the board is in AP mode
|
||||
|
||||
// Variable to keep track of the last status print time
|
||||
unsigned long lastStatusPrintTime = 0;
|
||||
unsigned long lastLogTime = 0; // Variable to keep track of the last log time
|
||||
|
||||
// Function to send logs to the log server
|
||||
void sendLog(String message) {
|
||||
if (WiFi.status() == WL_CONNECTED && logServerIP.length() > 0 && logServerPort.length() > 0) {
|
||||
HTTPClient http;
|
||||
String url = "http://" + logServerIP + ":" + logServerPort + "/log";
|
||||
http.begin(url);
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
String payload = "{\"hostname\": \"" + hostname + "\", \"message\": \"" + message + "\"}";
|
||||
int httpResponseCode = http.POST(payload);
|
||||
http.end();
|
||||
if (httpResponseCode > 0) {
|
||||
Serial.println("Log sent successfully: " + message);
|
||||
} else {
|
||||
Serial.println("Error sending log: " + message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the root URL ("/") and serve the configuration page
|
||||
void handleRoot() {
|
||||
@@ -63,6 +84,10 @@ void handleRoot() {
|
||||
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='log_server_ip'>Log Server IP:</label>";
|
||||
html += "<input type='text' id='log_server_ip' name='log_server_ip' value='" + logServerIP + "'><br>";
|
||||
html += "<label for='log_server_port'>Log Server Port:</label>";
|
||||
html += "<input type='text' id='log_server_port' name='log_server_port' value='" + logServerPort + "'><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'>";
|
||||
@@ -119,6 +144,8 @@ void handleSave() {
|
||||
netmask = server.arg("netmask");
|
||||
gateway = server.arg("gateway");
|
||||
hostname = server.arg("hostname");
|
||||
logServerIP = server.arg("log_server_ip");
|
||||
logServerPort = server.arg("log_server_port");
|
||||
|
||||
// Save the WiFi settings to EEPROM
|
||||
saveSettings();
|
||||
@@ -139,8 +166,10 @@ void handleRelay() {
|
||||
if (relay >= 0 && relay < 4) {
|
||||
if (action == "on") {
|
||||
digitalWrite(relayPins[relay], HIGH);
|
||||
sendLog("Relay " + String(relay + 1) + " turned ON");
|
||||
} else if (action == "off") {
|
||||
digitalWrite(relayPins[relay], LOW);
|
||||
sendLog("Relay " + String(relay + 1) + " turned OFF");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +186,8 @@ void saveSettings() {
|
||||
EEPROM.writeString(96, netmask);
|
||||
EEPROM.writeString(128, gateway);
|
||||
EEPROM.writeString(160, hostname);
|
||||
EEPROM.writeString(192, logServerIP);
|
||||
EEPROM.writeString(224, logServerPort);
|
||||
EEPROM.commit();
|
||||
}
|
||||
|
||||
@@ -168,12 +199,14 @@ void loadSettings() {
|
||||
netmask = EEPROM.readString(96);
|
||||
gateway = EEPROM.readString(128);
|
||||
hostname = EEPROM.readString(160);
|
||||
logServerIP = EEPROM.readString(192);
|
||||
logServerPort = EEPROM.readString(224);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Start the serial communication
|
||||
Serial.println("Setup started");
|
||||
EEPROM.begin(192); // Initialize EEPROM with a size of 192 bytes
|
||||
EEPROM.begin(256); // Initialize EEPROM with a size of 256 bytes
|
||||
|
||||
pinMode(userLedPin, OUTPUT); // Set the user LED pin as output
|
||||
digitalWrite(userLedPin, LOW); // Turn off the user LED
|
||||
@@ -248,6 +281,12 @@ void loop() {
|
||||
server.handleClient(); // Handle client requests
|
||||
blinkLed(); // Blink the LED to indicate the mode
|
||||
printStatus(); // Print the status of the board
|
||||
|
||||
// Send a log message every 20 seconds
|
||||
if (millis() - lastLogTime >= 20000) {
|
||||
sendLog("Board is functioning");
|
||||
lastLogTime = millis();
|
||||
}
|
||||
}
|
||||
|
||||
// Blink the LED to indicate the mode (AP mode or client mode)
|
||||
@@ -344,4 +383,4 @@ String getInterfaceMacAddress(esp_mac_type_t interface) {
|
||||
}
|
||||
|
||||
return mac;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user