saved
This commit is contained in:
@@ -13,6 +13,14 @@ class Log(db.Model):
|
||||
hostname = db.Column(db.String(100), nullable=False)
|
||||
message = db.Column(db.String(500), nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
relay1_status = db.Column(db.String(10), default='off')
|
||||
relay2_status = db.Column(db.String(10), default='off')
|
||||
relay3_status = db.Column(db.String(10), default='off')
|
||||
relay4_status = db.Column(db.String(10), default='off')
|
||||
input1_status = db.Column(db.String(10), default='off')
|
||||
input2_status = db.Column(db.String(10), default='off')
|
||||
input3_status = db.Column(db.String(10), default='off')
|
||||
input4_status = db.Column(db.String(10), default='off')
|
||||
|
||||
# Create the database if it does not exist
|
||||
if not os.path.exists('instance/logs.db'):
|
||||
@@ -35,7 +43,63 @@ def log_message():
|
||||
hostname = data.get('hostname')
|
||||
message = data.get('message')
|
||||
if hostname and message:
|
||||
new_log = Log(hostname=hostname, message=message)
|
||||
# Get the latest log for the hostname to determine the current status
|
||||
latest_log = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).first()
|
||||
relay1_status = latest_log.relay1_status if latest_log else 'off'
|
||||
relay2_status = latest_log.relay2_status if latest_log else 'off'
|
||||
relay3_status = latest_log.relay3_status if latest_log else 'off'
|
||||
relay4_status = latest_log.relay4_status if latest_log else 'off'
|
||||
input1_status = latest_log.input1_status if latest_log else 'off'
|
||||
input2_status = latest_log.input2_status if latest_log else 'off'
|
||||
input3_status = latest_log.input3_status if latest_log else 'off'
|
||||
input4_status = latest_log.input4_status if latest_log else 'off'
|
||||
|
||||
# Update the status based on the log message
|
||||
if "Relay 1 turned ON" in message:
|
||||
relay1_status = 'on'
|
||||
elif "Relay 1 turned OFF" in message:
|
||||
relay1_status = 'off'
|
||||
if "Relay 2 turned ON" in message:
|
||||
relay2_status = 'on'
|
||||
elif "Relay 2 turned OFF" in message:
|
||||
relay2_status = 'off'
|
||||
if "Relay 3 turned ON" in message:
|
||||
relay3_status = 'on'
|
||||
elif "Relay 3 turned OFF" in message:
|
||||
relay3_status = 'off'
|
||||
if "Relay 4 turned ON" in message:
|
||||
relay4_status = 'on'
|
||||
elif "Relay 4 turned OFF" in message:
|
||||
relay4_status = 'off'
|
||||
if "Input 1 pressed" in message:
|
||||
input1_status = 'on'
|
||||
elif "Input 1 released" in message:
|
||||
input1_status = 'off'
|
||||
if "Input 2 pressed" in message:
|
||||
input2_status = 'on'
|
||||
elif "Input 2 released" in message:
|
||||
input2_status = 'off'
|
||||
if "Input 3 pressed" in message:
|
||||
input3_status = 'on'
|
||||
elif "Input 3 released" in message:
|
||||
input3_status = 'off'
|
||||
if "Input 4 pressed" in message:
|
||||
input4_status = 'on'
|
||||
elif "Input 4 released" in message:
|
||||
input4_status = 'off'
|
||||
|
||||
new_log = Log(
|
||||
hostname=hostname,
|
||||
message=message,
|
||||
relay1_status=relay1_status,
|
||||
relay2_status=relay2_status,
|
||||
relay3_status=relay3_status,
|
||||
relay4_status=relay4_status,
|
||||
input1_status=input1_status,
|
||||
input2_status=input2_status,
|
||||
input3_status=input3_status,
|
||||
input4_status=input4_status
|
||||
)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'success', 'message': 'Log saved'}), 201
|
||||
@@ -73,8 +137,29 @@ def index():
|
||||
|
||||
@app.route('/board/<hostname>')
|
||||
def view_board(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return render_template('board.html', hostname=hostname, logs=logs)
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.asc()).all() # Order by ascending timestamp
|
||||
relay_status = ['off', 'off', 'off', 'off']
|
||||
input_status = ['off', 'off', 'off', 'off']
|
||||
relay_messages = ['', '', '', '']
|
||||
input_messages = ['', '', '', '']
|
||||
|
||||
# Determine the status of each relay and input based on the latest log messages
|
||||
for log in logs:
|
||||
for i in range(4):
|
||||
if f"Relay {i + 1} turned ON" in log.message:
|
||||
relay_status[i] = 'on'
|
||||
relay_messages[i] = log.message
|
||||
elif f"Relay {i + 1} turned OFF" in log.message:
|
||||
relay_status[i] = 'off'
|
||||
relay_messages[i] = log.message
|
||||
if f"Input {i + 1} pressed" in log.message:
|
||||
input_status[i] = 'on'
|
||||
input_messages[i] = log.message
|
||||
elif f"Input {i + 1} released" in log.message:
|
||||
input_status[i] = 'off'
|
||||
input_messages[i] = log.message
|
||||
|
||||
return render_template('board.html', hostname=hostname, logs=logs, relay_status=relay_status, input_status=input_status, relay_messages=relay_messages, input_messages=input_messages)
|
||||
|
||||
@app.route('/delete_board/<hostname>', methods=['POST'])
|
||||
def delete_board(hostname):
|
||||
@@ -85,7 +170,22 @@ def delete_board(hostname):
|
||||
@app.route('/board/<hostname>/logs', methods=['GET'])
|
||||
def get_board_logs(hostname):
|
||||
logs = Log.query.filter_by(hostname=hostname).order_by(Log.timestamp.desc()).all()
|
||||
return jsonify({'logs': [{'timestamp': log.timestamp, 'message': log.message} for log in logs]})
|
||||
latest_log = logs[0] if logs else None
|
||||
return jsonify({
|
||||
'logs': [{'timestamp': log.timestamp, 'message': log.message} for log in logs],
|
||||
'relay_status': {
|
||||
'relay1': latest_log.relay1_status if latest_log else 'off',
|
||||
'relay2': latest_log.relay2_status if latest_log else 'off',
|
||||
'relay3': latest_log.relay3_status if latest_log else 'off',
|
||||
'relay4': latest_log.relay4_status if latest_log else 'off'
|
||||
},
|
||||
'input_status': {
|
||||
'input1': latest_log.input1_status if latest_log else 'off',
|
||||
'input2': latest_log.input2_status if latest_log else 'off',
|
||||
'input3': latest_log.input3_status if latest_log else 'off',
|
||||
'input4': latest_log.input4_status if latest_log else 'off'
|
||||
}
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
@@ -27,6 +27,18 @@
|
||||
color: #6c757d;
|
||||
text-align: right;
|
||||
}
|
||||
.status-indicator {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
display: inline-block;
|
||||
border-radius: 50%;
|
||||
}
|
||||
.status-on {
|
||||
background-color: #28a745;
|
||||
}
|
||||
.status-off {
|
||||
background-color: #dc3545;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
function updateLogs() {
|
||||
@@ -41,6 +53,22 @@
|
||||
logItem.innerHTML = `<strong>${log.timestamp}</strong>: ${log.message}`;
|
||||
logsContainer.appendChild(logItem);
|
||||
});
|
||||
|
||||
// Update relay status indicators and messages
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const relayIndicator = document.getElementById(`relay-status-${i}`);
|
||||
relayIndicator.className = `status-indicator ${data.relay_status[`relay${i}`] === 'on' ? 'status-on' : 'status-off'}`;
|
||||
const relayMessage = document.getElementById(`relay-message-${i}`);
|
||||
relayMessage.innerText = data.logs.find(log => log.message.includes(`Relay ${i} turned`)).message;
|
||||
}
|
||||
|
||||
// Update input status indicators and messages
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
const inputIndicator = document.getElementById(`input-status-${i}`);
|
||||
inputIndicator.className = `status-indicator ${data.input_status[`input${i}`] === 'on' ? 'status-on' : 'status-off'}`;
|
||||
const inputMessage = document.getElementById(`input-message-${i}`);
|
||||
inputMessage.innerText = data.logs.find(log => log.message.includes(`Input ${i}`)).message;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -73,7 +101,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="col-md-6">
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Logs for {{ hostname }}</h5>
|
||||
@@ -87,6 +115,38 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="row">
|
||||
{% for i in range(4) %}
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Relay {{ i + 1 }}</h5>
|
||||
<p class="card-text">Status:
|
||||
<span id="relay-status-{{ i + 1 }}" class="status-indicator {{ 'status-on' if relay_status[i] == 'on' else 'status-off' }}"></span>
|
||||
</p>
|
||||
<p class="card-text" id="relay-message-{{ i + 1 }}">{{ relay_messages[i] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="row">
|
||||
{% for i in range(4) %}
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Input {{ i + 1 }}</h5>
|
||||
<p class="card-text">Status:
|
||||
<span id="input-status-{{ i + 1 }}" class="status-indicator {{ 'status-on' if input_status[i] == 'on' else 'status-off' }}"></span>
|
||||
</p>
|
||||
<p class="card-text" id="input-message-{{ i + 1 }}">{{ input_messages[i] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
BIN
instance/logs.db
BIN
instance/logs.db
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
#include <HTTPClient.h> // Include the HTTPClient library for sending logs
|
||||
|
||||
// Version of the code
|
||||
//ver 0.0.11
|
||||
//ver 0.0.10
|
||||
|
||||
// Constants for Access Point mode
|
||||
const char* ap_ssid = "ESP32-AP"; // SSID for the Access Point mode
|
||||
@@ -37,7 +37,7 @@ void sendLog(String message) {
|
||||
String url = "http://" + logServerIP + ":" + logServerPort + "/log";
|
||||
http.begin(url);
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
String payload = "{\"hostname\": \"" + hostname + "\", \"message\": \"" + message + "\"}";
|
||||
String payload = "{\"hostname\": \"" + hostname + "\", \"status\": \"online\", \"message\": \"" + message + "\"}";
|
||||
int httpResponseCode = http.POST(payload);
|
||||
http.end();
|
||||
if (httpResponseCode > 0) {
|
||||
@@ -48,6 +48,16 @@ void sendLog(String message) {
|
||||
}
|
||||
}
|
||||
|
||||
// Log the status of the board and the status of each input and relay
|
||||
void logBoardStatus() {
|
||||
String message = "Board started. Status of inputs and relays:\n";
|
||||
for (int i = 0; i < 4; i++) {
|
||||
message += "Relay " + String(i + 1) + ": " + (digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "\n";
|
||||
message += "Input " + String(i + 1) + ": " + (digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "\n";
|
||||
}
|
||||
sendLog(message);
|
||||
}
|
||||
|
||||
// Handle the root URL ("/") and serve the configuration page
|
||||
void handleRoot() {
|
||||
// Get the default MAC address of the ESP32
|
||||
@@ -275,6 +285,7 @@ void setup() {
|
||||
}
|
||||
}
|
||||
Serial.println("Setup completed");
|
||||
logBoardStatus(); // Log the status of the board at startup
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -371,16 +382,3 @@ String getDefaultMacAddress() {
|
||||
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;
|
||||
}
|
||||
386
wifi_set_and_server/wifiset 1
Normal file
386
wifi_set_and_server/wifiset 1
Normal file
@@ -0,0 +1,386 @@
|
||||
#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
|
||||
#include <HTTPClient.h> // Include the HTTPClient library for sending logs
|
||||
|
||||
// Version of the code
|
||||
//ver 0.0.11
|
||||
|
||||
// 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
|
||||
|
||||
// Create a web server on port 80
|
||||
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() {
|
||||
// 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 += "<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 in AP mode, show the configuration form
|
||||
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='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'>";
|
||||
html += "</form></div>";
|
||||
}
|
||||
|
||||
// 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>";
|
||||
html += "<p>IP Address: " + WiFi.localIP().toString() + "</p>";
|
||||
html += "<p>Hostname: " + hostname + "</p>";
|
||||
html += "</div>";
|
||||
|
||||
// Add relay status with control buttons
|
||||
html += "<div class='container'><h2>Relay Status</h2>";
|
||||
for (int i = 0; i < 4; i++) {
|
||||
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></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>";
|
||||
}
|
||||
} 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");
|
||||
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();
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// 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);
|
||||
sendLog("Relay " + String(relay + 1) + " turned ON");
|
||||
} else if (action == "off") {
|
||||
digitalWrite(relayPins[relay], LOW);
|
||||
sendLog("Relay " + String(relay + 1) + " turned OFF");
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect the client back to the root URL
|
||||
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.writeString(192, logServerIP);
|
||||
EEPROM.writeString(224, logServerPort);
|
||||
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);
|
||||
logServerIP = EEPROM.readString(192);
|
||||
logServerPort = EEPROM.readString(224);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Start the serial communication
|
||||
Serial.println("Setup started");
|
||||
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
|
||||
|
||||
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);
|
||||
pinMode(inputPins[i], INPUT_PULLUP); // Set input pins as input with internal pull-up resistors
|
||||
}
|
||||
|
||||
loadSettings(); // Load the WiFi settings from EEPROM
|
||||
|
||||
// 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 {
|
||||
// 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: ");
|
||||
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(); // 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)
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user