Ugh..
This commit is contained in:
+46
-22
@@ -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
|
|
||||||
String response = client.readStringUntil('\n');
|
|
||||||
if (response == "ACK") {
|
|
||||||
Serial.println("Received ACK");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(2000);
|
|
||||||
client.stop();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.println("Connection failed");
|
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");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 ..
|
||||||
|
|||||||
+3
-1
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user