From 830a4e21378fbfb368d8629d7b5d659a83dc6b1f Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 10 May 2025 22:29:52 +0200 Subject: [PATCH] Initial working example --- client/platformio.ini | 3 ++ client/src/main.cpp | 50 +++++++++++++++++++------- server/platformio.ini | 3 ++ server/src/main.cpp | 82 +++++++++++++++++++++++++++++++++---------- 4 files changed, 106 insertions(+), 32 deletions(-) diff --git a/client/platformio.ini b/client/platformio.ini index b31839a..bf4a1dd 100644 --- a/client/platformio.ini +++ b/client/platformio.ini @@ -25,3 +25,6 @@ lib_deps = monitor_filters = esp32_exception_decoder time ; add timestamp with milliseconds for each new line + +upload_port = /dev/ttyACM1 +monitor_port = /dev/ttyACM1 diff --git a/client/src/main.cpp b/client/src/main.cpp index 722099f..85ebc06 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -9,10 +9,14 @@ #define HOST "192.168.4.1" #define PORT 1234 -#define BUTTON_PIN 2 +#define POWER_BUTTON_PIN 2 +#define RESET_BUTTON_PIN 3 WiFiClient client; +int previousPowerButtonState = HIGH; +int previousResetButtonState = HIGH; + void ack(); void setup() @@ -20,7 +24,8 @@ void setup() Serial.begin(9600); Serial.setDebugOutput(true); - pinMode(BUTTON_PIN, INPUT_PULLUP); + pinMode(POWER_BUTTON_PIN, INPUT_PULLUP); + pinMode(RESET_BUTTON_PIN, INPUT_PULLUP); // Wait for a USB connection to be established while (!Serial) @@ -40,33 +45,52 @@ void setup() void loop() { - delay(25); - Serial.println("ESP32 loopy!"); + delay(20); + + int powerButtonState = digitalRead(POWER_BUTTON_PIN); + int resetButtonState = digitalRead(RESET_BUTTON_PIN); + + // Unsupported usecase + if (powerButtonState == LOW && resetButtonState == LOW) { + previousPowerButtonState = HIGH; + previousResetButtonState = HIGH; + if (client.connected()) { + client.stop(); + } + return; + } - int buttonState = digitalRead(BUTTON_PIN); - if (buttonState == LOW) { - Serial.println("Button pressed!"); + bool isButtonPressed = (previousPowerButtonState == LOW && powerButtonState == LOW) + || (previousResetButtonState == LOW && resetButtonState == LOW); + previousPowerButtonState = powerButtonState; + previousResetButtonState = resetButtonState; + + if (isButtonPressed) { + String button = (powerButtonState == LOW) ? "power" : "reset"; + + Serial.println("Pressed " + button + " button!"); if (!client.connected() && !client.connect(HOST, PORT)) { Serial.println("Connection failed"); return; } - client.print("button_pressed\n"); - Serial.println("Sent button press"); + client.print(button + "_pressed\n"); + Serial.println("Sent " + button + " button press"); ack(); } else { - Serial.println("Button not pressed!"); - if (!client.connected()) { return; } - client.print("button_released\n"); - Serial.println("Sent button release"); + client.print("power_released\n"); + Serial.println("Sent power button release"); + ack(); + client.print("reset_released\n"); + Serial.println("Sent reset button release"); ack(); client.stop(); diff --git a/server/platformio.ini b/server/platformio.ini index b31839a..26c5dcb 100644 --- a/server/platformio.ini +++ b/server/platformio.ini @@ -25,3 +25,6 @@ lib_deps = monitor_filters = esp32_exception_decoder time ; add timestamp with milliseconds for each new line + +upload_port = /dev/ttyACM0 +monitor_port = /dev/ttyACM0 diff --git a/server/src/main.cpp b/server/src/main.cpp index 74418c4..d166ce4 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -12,16 +12,25 @@ #define HIDDEN true #define MAX_CONNECTION 1 -#define SWITCH_PIN 2 +#define POWER_PIN 2 +#define RESET_PIN 3 WiFiServer server(PORT); +bool powerPressed = false; +bool resetPressed = false; + +void processButtons(); +void setPowerPin(bool enable); +void setResetPin(bool enable); + void setup() { Serial.begin(9600); Serial.setDebugOutput(true); - pinMode(SWITCH_PIN, OUTPUT); + pinMode(POWER_PIN, OUTPUT); + pinMode(RESET_PIN, OUTPUT); // Wait for a USB connection to be established while (!Serial) @@ -37,40 +46,75 @@ void setup() void loop() { - digitalWrite(2, HIGH); - delay(500); - digitalWrite(2, LOW); - delay(500); - - Serial.println("ESP32 loopy!"); - - // ------------------------------------- - WiFiClient client = server.available(); if (client) { Serial.println("Client connected"); - // unsigned long start = 0; + unsigned long start = 0; while (client.connected()) { if (client.available()) { String msg = client.readStringUntil('\n'); Serial.print("Received: "); Serial.println(msg); - // Do something with the message, like toggling an LED + if (msg == "power_pressed") { + powerPressed = true; + } + else if (msg == "power_released") { + powerPressed = false; + } + else if (msg == "reset_pressed") { + resetPressed = true; + } + else if (msg == "reset_released") { + resetPressed = false; + } client.print("ACK\n"); Serial.println("Sent ACK"); - // start = millis(); + start = millis(); } - // // Kill lingering connections - // if (start != 0 && millis() - start < 1000) { - // Serial.println("Client kill.."); - // break; - // } + // Kill lingering connections + if (start != 0 && millis() - start > 200) { + Serial.println("Client kill.."); + break; + } + + processButtons(); } + + setPowerPin(false); + setResetPin(false); + client.stop(); Serial.println("Client disconnected"); } } + +// ----------------------------------------- + +void processButtons() +{ + if (powerPressed && resetPressed) { + Serial.println("Double input.."); + setPowerPin(false); + setResetPin(false); + return; + } + + setPowerPin(powerPressed); + setResetPin(resetPressed); +} + +void setPowerPin(bool enable) +{ + powerPressed = enable; + digitalWrite(POWER_PIN, (enable) ? HIGH : LOW); +} + +void setResetPin(bool enable) +{ + resetPressed = enable; + digitalWrite(RESET_PIN, (enable) ? HIGH : LOW); +}