first commit
This commit is contained in:
331
src/master.cpp
Normal file
331
src/master.cpp
Normal file
@@ -0,0 +1,331 @@
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "master.h"
|
||||
// Aquacross Timer - ESP32 Master (Webserver + ESP-NOW + Anlernmodus)
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <esp_now.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Preferences.h>
|
||||
#include <PrettyOTA.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <AsyncTCP.h>
|
||||
|
||||
#include <timesync.h>
|
||||
#include <licenceing.h>
|
||||
#include <debug.h>
|
||||
#include <wificlass.h>
|
||||
#include <webserverrouter.h>
|
||||
#include <communication.h>
|
||||
|
||||
|
||||
const char* firmwareversion = "1.0.0"; // Version der Firmware
|
||||
|
||||
|
||||
|
||||
void handleLearningMode(const uint8_t* mac) {
|
||||
// Prüfen ob MAC bereits einem anderen Button zugewiesen ist
|
||||
if (buttonConfigs.start1.isAssigned && memcmp(buttonConfigs.start1.mac, mac, 6) == 0) {
|
||||
Serial.println("Diese MAC ist bereits zugewiesen - wird ignoriert");
|
||||
return;
|
||||
}
|
||||
if (buttonConfigs.stop1.isAssigned && memcmp(buttonConfigs.stop1.mac, mac, 6) == 0) {
|
||||
Serial.println("Diese MAC ist bereits zugewiesen - wird ignoriert");
|
||||
return;
|
||||
}
|
||||
if (buttonConfigs.start2.isAssigned && memcmp(buttonConfigs.start2.mac, mac, 6) == 0) {
|
||||
Serial.println("Diese MAC ist bereits zugewiesen - wird ignoriert");
|
||||
return;
|
||||
}
|
||||
if (buttonConfigs.stop2.isAssigned && memcmp(buttonConfigs.stop2.mac, mac, 6) == 0) {
|
||||
Serial.println("Diese MAC ist bereits zugewiesen - wird ignoriert");
|
||||
return;
|
||||
}
|
||||
|
||||
// MAC ist noch nicht zugewiesen, normal fortfahren
|
||||
switch(learningStep) {
|
||||
case 0: // Start1
|
||||
memcpy(buttonConfigs.start1.mac, mac, 6);
|
||||
buttonConfigs.start1.isAssigned = true;
|
||||
Serial.println("Start1 Button zugewiesen");
|
||||
break;
|
||||
case 1: // Stop1
|
||||
memcpy(buttonConfigs.stop1.mac, mac, 6);
|
||||
buttonConfigs.stop1.isAssigned = true;
|
||||
Serial.println("Stop1 Button zugewiesen");
|
||||
break;
|
||||
case 2: // Start2
|
||||
memcpy(buttonConfigs.start2.mac, mac, 6);
|
||||
buttonConfigs.start2.isAssigned = true;
|
||||
Serial.println("Start2 Button zugewiesen");
|
||||
break;
|
||||
case 3: // Stop2
|
||||
memcpy(buttonConfigs.stop2.mac, mac, 6);
|
||||
buttonConfigs.stop2.isAssigned = true;
|
||||
Serial.println("Stop2 Button zugewiesen");
|
||||
break;
|
||||
}
|
||||
|
||||
learningStep++;
|
||||
if (learningStep >= 4) {
|
||||
learningMode = false;
|
||||
learningStep = 0;
|
||||
saveButtonConfig();
|
||||
Serial.println("Lernmodus beendet!");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStartLearning() {
|
||||
learningMode = true;
|
||||
|
||||
// Count assigned buttons and set appropriate learning step
|
||||
int assignedButtons = 0;
|
||||
if (buttonConfigs.start1.isAssigned) assignedButtons++;
|
||||
if (buttonConfigs.stop1.isAssigned) assignedButtons++;
|
||||
if (buttonConfigs.start2.isAssigned) assignedButtons++;
|
||||
if (buttonConfigs.stop2.isAssigned) assignedButtons++;
|
||||
|
||||
learningStep = assignedButtons;
|
||||
|
||||
Serial.printf("Learning mode started - %d buttons already assigned, continuing at step %d\n",
|
||||
assignedButtons, learningStep);
|
||||
}
|
||||
|
||||
void handleLearningStatus() {
|
||||
DynamicJsonDocument doc(256);
|
||||
doc["active"] = learningMode;
|
||||
doc["step"] = learningStep;
|
||||
|
||||
String response;
|
||||
serializeJson(doc, response);
|
||||
}
|
||||
|
||||
void unlearnButton() {
|
||||
|
||||
memset(buttonConfigs.start1.mac, 0, 6);
|
||||
buttonConfigs.start1.isAssigned = false;
|
||||
memset(buttonConfigs.stop1.mac, 0, 6);
|
||||
buttonConfigs.stop1.isAssigned = false;
|
||||
memset(buttonConfigs.start2.mac, 0, 6);
|
||||
buttonConfigs.start2.isAssigned = false;
|
||||
memset(buttonConfigs.stop2.mac, 0, 6);
|
||||
buttonConfigs.stop2.isAssigned = false;
|
||||
|
||||
saveButtonConfig();
|
||||
Serial.println("Buttons wurden verlernt.");
|
||||
}
|
||||
|
||||
void handleStart1() {
|
||||
if (!timerData.isRunning1) {
|
||||
timerData.startTime1 = millis();
|
||||
timerData.isRunning1 = true;
|
||||
timerData.endTime1 = 0;
|
||||
Serial.println("Bahn 1 gestartet");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStop1() {
|
||||
if (timerData.isRunning1) {
|
||||
timerData.endTime1 = millis();
|
||||
timerData.isRunning1 = false;
|
||||
unsigned long currentTime = timerData.endTime1 - timerData.startTime1;
|
||||
|
||||
if (timerData.bestTime1 == 0 || currentTime < timerData.bestTime1) {
|
||||
timerData.bestTime1 = currentTime;
|
||||
saveBestTimes();
|
||||
}
|
||||
timerData.finishedSince1 = millis();
|
||||
Serial.println("Bahn 1 gestoppt - Zeit: " + String(currentTime/1000.0) + "s");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStart2() {
|
||||
if (!timerData.isRunning2) {
|
||||
timerData.startTime2 = millis();
|
||||
timerData.isRunning2 = true;
|
||||
timerData.endTime2 = 0;
|
||||
Serial.println("Bahn 2 gestartet");
|
||||
}
|
||||
}
|
||||
|
||||
void handleStop2() {
|
||||
if (timerData.isRunning2) {
|
||||
timerData.endTime2 = millis();
|
||||
timerData.isRunning2 = false;
|
||||
unsigned long currentTime = timerData.endTime2 - timerData.startTime2;
|
||||
|
||||
if (timerData.bestTime2 == 0 || currentTime < timerData.bestTime2) {
|
||||
timerData.bestTime2 = currentTime;
|
||||
saveBestTimes();
|
||||
}
|
||||
timerData.finishedSince2 = millis();
|
||||
Serial.println("Bahn 2 gestoppt - Zeit: " + String(currentTime/1000.0) + "s");
|
||||
}
|
||||
}
|
||||
|
||||
void checkAutoReset() {
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
if (timerData.isRunning1 && (currentTime - timerData.startTime1 > maxTimeBeforeReset)) {
|
||||
timerData.isRunning1 = false;
|
||||
timerData.startTime1 = 0;
|
||||
Serial.println("Bahn 1 automatisch zurückgesetzt");
|
||||
}
|
||||
|
||||
if (timerData.isRunning2 && (currentTime - timerData.startTime2 > maxTimeBeforeReset)) {
|
||||
timerData.isRunning2 = false;
|
||||
timerData.startTime2 = 0;
|
||||
Serial.println("Bahn 2 automatisch zurückgesetzt");
|
||||
}
|
||||
|
||||
// Automatischer Reset nach 10 Sekunden "Beendet"
|
||||
if (!timerData.isRunning1 && timerData.endTime1 > 0 && timerData.finishedSince1 > 0) {
|
||||
if (currentTime - timerData.finishedSince1 > maxTimeDisplay) {
|
||||
timerData.startTime1 = 0;
|
||||
timerData.endTime1 = 0;
|
||||
timerData.finishedSince1 = 0;
|
||||
Serial.println("Bahn 1 automatisch auf 'Bereit' zurückgesetzt");
|
||||
}
|
||||
}
|
||||
|
||||
if (!timerData.isRunning2 && timerData.endTime2 > 0 && timerData.finishedSince2 > 0) {
|
||||
if (currentTime - timerData.finishedSince2 > maxTimeDisplay) {
|
||||
timerData.startTime2 = 0;
|
||||
timerData.endTime2 = 0;
|
||||
timerData.finishedSince2 = 0;
|
||||
Serial.println("Bahn 2 automatisch auf 'Bereit' zurückgesetzt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void saveButtonConfig() {
|
||||
preferences.begin("buttons", false);
|
||||
preferences.putBytes("config", &buttonConfigs, sizeof(buttonConfigs));
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void loadButtonConfig() {
|
||||
preferences.begin("buttons", true);
|
||||
size_t schLen = preferences.getBytesLength("config");
|
||||
if (schLen == sizeof(buttonConfigs)) {
|
||||
preferences.getBytes("config", &buttonConfigs, schLen);
|
||||
}
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void saveBestTimes() {
|
||||
preferences.begin("times", false);
|
||||
preferences.putULong("best1", timerData.bestTime1);
|
||||
preferences.putULong("best2", timerData.bestTime2);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void loadBestTimes() {
|
||||
preferences.begin("times", true);
|
||||
timerData.bestTime1 = preferences.getULong("best1", 0);
|
||||
timerData.bestTime2 = preferences.getULong("best2", 0);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void saveSettings() {
|
||||
preferences.begin("settings", false);
|
||||
preferences.putULong("maxTime", maxTimeBeforeReset);
|
||||
preferences.putULong("maxTimeDisplay", maxTimeDisplay);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
void loadSettings() {
|
||||
preferences.begin("settings", true);
|
||||
maxTimeBeforeReset = preferences.getULong("maxTime", 300000);
|
||||
maxTimeDisplay = preferences.getULong("maxTimeDisplay", 20000);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
int checkLicence() {
|
||||
loadLicenceFromPrefs();
|
||||
String id = getUniqueDeviceID();
|
||||
int tier = getLicenseTier(id, licence); // licence = stored or entered key
|
||||
return tier;
|
||||
}
|
||||
|
||||
String getTimerDataJSON() {
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
// Bahn 1
|
||||
if (timerData.isRunning1) {
|
||||
doc["time1"] = (currentTime - timerData.startTime1) / 1000.0;
|
||||
doc["status1"] = "running";
|
||||
} else if (timerData.endTime1 > 0) {
|
||||
doc["time1"] = (timerData.endTime1 - timerData.startTime1) / 1000.0;
|
||||
doc["status1"] = "finished";
|
||||
} else {
|
||||
doc["time1"] = 0;
|
||||
doc["status1"] = "ready";
|
||||
}
|
||||
|
||||
// Bahn 2
|
||||
if (timerData.isRunning2) {
|
||||
doc["time2"] = (currentTime - timerData.startTime2) / 1000.0;
|
||||
doc["status2"] = "running";
|
||||
} else if (timerData.endTime2 > 0) {
|
||||
doc["time2"] = (timerData.endTime2 - timerData.startTime2) / 1000.0;
|
||||
doc["status2"] = "finished";
|
||||
} else {
|
||||
doc["time2"] = 0;
|
||||
doc["status2"] = "ready";
|
||||
}
|
||||
|
||||
// Beste Zeiten
|
||||
doc["best1"] = timerData.bestTime1 / 1000.0;
|
||||
doc["best2"] = timerData.bestTime2 / 1000.0;
|
||||
|
||||
// Lernmodus
|
||||
doc["learningMode"] = learningMode;
|
||||
if (learningMode) {
|
||||
String buttons[] = {"Start Bahn 1", "Stop Bahn 1", "Start Bahn 2", "Stop Bahn 2"};
|
||||
doc["learningButton"] = buttons[learningStep];
|
||||
}
|
||||
|
||||
String result;
|
||||
serializeJson(doc, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
if (!SPIFFS.begin(true)) {
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
//setup external libararies
|
||||
setupTimeAPI(server);
|
||||
setupLicenceAPI(server);
|
||||
setupDebugAPI(server);
|
||||
|
||||
|
||||
// Gespeicherte Daten laden
|
||||
loadButtonConfig();
|
||||
loadBestTimes();
|
||||
loadSettings();
|
||||
|
||||
setupWifi(); // WiFi initialisieren
|
||||
setupOTA(&server);
|
||||
|
||||
setupRoutes();
|
||||
|
||||
setupMqttServer(); // MQTT Server initialisieren
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
checkAutoReset();
|
||||
loopMqttServer(); // MQTT Server in der Loop aufrufen
|
||||
delay(100);
|
||||
}
|
||||
Reference in New Issue
Block a user