From 2b6ba483650c97a9d366846df710b5199ea5d70d Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 10 May 2025 19:59:56 +0200 Subject: [PATCH] Ugh.. --- client/src/main.cpp | 64 +++++++++++++++++++++++++++++++-------------- makefile | 4 +-- server/src/main.cpp | 4 ++- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/client/src/main.cpp b/client/src/main.cpp index 6174235..722099f 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -9,12 +9,18 @@ #define HOST "192.168.4.1" #define PORT 1234 +#define BUTTON_PIN 2 + +WiFiClient client; + +void ack(); + void setup() { Serial.begin(9600); Serial.setDebugOutput(true); - pinMode(2, OUTPUT); + pinMode(BUTTON_PIN, INPUT_PULLUP); // Wait for a USB connection to be established while (!Serial) @@ -34,33 +40,51 @@ void setup() void loop() { - digitalWrite(2, HIGH); - delay(500); - digitalWrite(2, LOW); - delay(500); - + delay(25); Serial.println("ESP32 loopy!"); - WiFiClient client; - if (client.connect(HOST, PORT)) { + int buttonState = digitalRead(BUTTON_PIN); + if (buttonState == LOW) { + Serial.println("Button pressed!"); + + if (!client.connected() && !client.connect(HOST, PORT)) { + Serial.println("Connection failed"); + return; + } + client.print("button_pressed\n"); Serial.println("Sent button press"); - while (client.connected()) { - if (client.available()) { - // Wait for acknowledgment from the receiver - String response = client.readStringUntil('\n'); - if (response == "ACK") { - Serial.println("Received ACK"); - break; - } - } + ack(); + } + else { + Serial.println("Button not pressed!"); + + if (!client.connected()) { + return; } - delay(2000); + client.print("button_released\n"); + Serial.println("Sent button release"); + + ack(); + client.stop(); } - else { - Serial.println("Connection failed"); +} + +// ----------------------------------------- + +void ack() +{ + while (client.connected()) { + if (client.available()) { + // Wait for acknowledgment from the receiver + String response = client.readStringUntil('\n'); + if (response == "ACK") { + Serial.println("Received ACK"); + return; + } + } } } diff --git a/makefile b/makefile index 4e95609..66482e3 100644 --- a/makefile +++ b/makefile @@ -12,7 +12,7 @@ server-log: server-run: @-cd server ; \ - platformio run --target upload ; \ + platformio run --target upload && \ platformio device monitor ; \ cd .. @@ -28,6 +28,6 @@ client-log: client-run: @-cd client ; \ - platformio run --target upload ; \ + platformio run --target upload && \ platformio device monitor ; \ cd .. diff --git a/server/src/main.cpp b/server/src/main.cpp index 45ef07a..74418c4 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -12,6 +12,8 @@ #define HIDDEN true #define MAX_CONNECTION 1 +#define SWITCH_PIN 2 + WiFiServer server(PORT); void setup() @@ -19,7 +21,7 @@ void setup() Serial.begin(9600); Serial.setDebugOutput(true); - pinMode(2, OUTPUT); + pinMode(SWITCH_PIN, OUTPUT); // Wait for a USB connection to be established while (!Serial)