This commit is contained in:
Riyyi
2025-05-10 20:57:26 +02:00
parent 364733cbba
commit 2b6ba48365
3 changed files with 51 additions and 25 deletions
+40 -16
View File
@@ -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");
ack();
}
else {
Serial.println("Button not pressed!");
if (!client.connected()) {
return;
}
client.print("button_released\n");
Serial.println("Sent button release");
ack();
client.stop();
}
}
// -----------------------------------------
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");
break;
return;
}
}
}
delay(2000);
client.stop();
}
else {
Serial.println("Connection failed");
}
}
+2 -2
View File
@@ -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 ..
+3 -1
View File
@@ -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)