Initial working example
This commit is contained in:
@@ -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
|
||||
|
||||
+37
-13
@@ -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 buttonState = digitalRead(BUTTON_PIN);
|
||||
if (buttonState == LOW) {
|
||||
Serial.println("Button pressed!");
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -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
|
||||
|
||||
+63
-19
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user