Browse Source

Ugh..

master
Riyyi 4 months ago
parent
commit
2b6ba48365
  1. 64
      client/src/main.cpp
  2. 4
      makefile
  3. 4
      server/src/main.cpp

64
client/src/main.cpp

@ -9,12 +9,18 @@
#define HOST "192.168.4.1" #define HOST "192.168.4.1"
#define PORT 1234 #define PORT 1234
#define BUTTON_PIN 2
WiFiClient client;
void ack();
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
pinMode(2, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP);
// Wait for a USB connection to be established // Wait for a USB connection to be established
while (!Serial) while (!Serial)
@ -34,33 +40,51 @@ void setup()
void loop() void loop()
{ {
digitalWrite(2, HIGH); delay(25);
delay(500);
digitalWrite(2, LOW);
delay(500);
Serial.println("ESP32 loopy!"); Serial.println("ESP32 loopy!");
WiFiClient client; int buttonState = digitalRead(BUTTON_PIN);
if (client.connect(HOST, PORT)) { if (buttonState == LOW) {
Serial.println("Button pressed!");
if (!client.connected() && !client.connect(HOST, PORT)) {
Serial.println("Connection failed");
return;
}
client.print("button_pressed\n"); client.print("button_pressed\n");
Serial.println("Sent button press"); Serial.println("Sent button press");
while (client.connected()) { ack();
if (client.available()) { }
// Wait for acknowledgment from the receiver else {
String response = client.readStringUntil('\n'); Serial.println("Button not pressed!");
if (response == "ACK") {
Serial.println("Received ACK"); if (!client.connected()) {
break; return;
}
}
} }
delay(2000); client.print("button_released\n");
Serial.println("Sent button release");
ack();
client.stop(); 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;
}
}
} }
} }

4
makefile

@ -12,7 +12,7 @@ server-log:
server-run: server-run:
@-cd server ; \ @-cd server ; \
platformio run --target upload ; \ platformio run --target upload && \
platformio device monitor ; \ platformio device monitor ; \
cd .. cd ..
@ -28,6 +28,6 @@ client-log:
client-run: client-run:
@-cd client ; \ @-cd client ; \
platformio run --target upload ; \ platformio run --target upload && \
platformio device monitor ; \ platformio device monitor ; \
cd .. cd ..

4
server/src/main.cpp

@ -12,6 +12,8 @@
#define HIDDEN true #define HIDDEN true
#define MAX_CONNECTION 1 #define MAX_CONNECTION 1
#define SWITCH_PIN 2
WiFiServer server(PORT); WiFiServer server(PORT);
void setup() void setup()
@ -19,7 +21,7 @@ void setup()
Serial.begin(9600); Serial.begin(9600);
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
pinMode(2, OUTPUT); pinMode(SWITCH_PIN, OUTPUT);
// Wait for a USB connection to be established // Wait for a USB connection to be established
while (!Serial) while (!Serial)

Loading…
Cancel
Save