Initial working example
This commit is contained in:
@@ -25,3 +25,6 @@ lib_deps =
|
|||||||
monitor_filters =
|
monitor_filters =
|
||||||
esp32_exception_decoder
|
esp32_exception_decoder
|
||||||
time ; add timestamp with milliseconds for each new line
|
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 HOST "192.168.4.1"
|
||||||
#define PORT 1234
|
#define PORT 1234
|
||||||
|
|
||||||
#define BUTTON_PIN 2
|
#define POWER_BUTTON_PIN 2
|
||||||
|
#define RESET_BUTTON_PIN 3
|
||||||
|
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
|
|
||||||
|
int previousPowerButtonState = HIGH;
|
||||||
|
int previousResetButtonState = HIGH;
|
||||||
|
|
||||||
void ack();
|
void ack();
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
@@ -20,7 +24,8 @@ void setup()
|
|||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.setDebugOutput(true);
|
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
|
// Wait for a USB connection to be established
|
||||||
while (!Serial)
|
while (!Serial)
|
||||||
@@ -40,33 +45,52 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
delay(25);
|
delay(20);
|
||||||
Serial.println("ESP32 loopy!");
|
|
||||||
|
|
||||||
int buttonState = digitalRead(BUTTON_PIN);
|
int powerButtonState = digitalRead(POWER_BUTTON_PIN);
|
||||||
if (buttonState == LOW) {
|
int resetButtonState = digitalRead(RESET_BUTTON_PIN);
|
||||||
Serial.println("Button pressed!");
|
|
||||||
|
// 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)) {
|
if (!client.connected() && !client.connect(HOST, PORT)) {
|
||||||
Serial.println("Connection failed");
|
Serial.println("Connection failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.print("button_pressed\n");
|
client.print(button + "_pressed\n");
|
||||||
Serial.println("Sent button press");
|
Serial.println("Sent " + button + " button press");
|
||||||
|
|
||||||
ack();
|
ack();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.println("Button not pressed!");
|
|
||||||
|
|
||||||
if (!client.connected()) {
|
if (!client.connected()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.print("button_released\n");
|
client.print("power_released\n");
|
||||||
Serial.println("Sent button release");
|
Serial.println("Sent power button release");
|
||||||
|
ack();
|
||||||
|
|
||||||
|
client.print("reset_released\n");
|
||||||
|
Serial.println("Sent reset button release");
|
||||||
ack();
|
ack();
|
||||||
|
|
||||||
client.stop();
|
client.stop();
|
||||||
|
|||||||
@@ -25,3 +25,6 @@ lib_deps =
|
|||||||
monitor_filters =
|
monitor_filters =
|
||||||
esp32_exception_decoder
|
esp32_exception_decoder
|
||||||
time ; add timestamp with milliseconds for each new line
|
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 HIDDEN true
|
||||||
#define MAX_CONNECTION 1
|
#define MAX_CONNECTION 1
|
||||||
|
|
||||||
#define SWITCH_PIN 2
|
#define POWER_PIN 2
|
||||||
|
#define RESET_PIN 3
|
||||||
|
|
||||||
WiFiServer server(PORT);
|
WiFiServer server(PORT);
|
||||||
|
|
||||||
|
bool powerPressed = false;
|
||||||
|
bool resetPressed = false;
|
||||||
|
|
||||||
|
void processButtons();
|
||||||
|
void setPowerPin(bool enable);
|
||||||
|
void setResetPin(bool enable);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.setDebugOutput(true);
|
Serial.setDebugOutput(true);
|
||||||
|
|
||||||
pinMode(SWITCH_PIN, OUTPUT);
|
pinMode(POWER_PIN, OUTPUT);
|
||||||
|
pinMode(RESET_PIN, OUTPUT);
|
||||||
|
|
||||||
// Wait for a USB connection to be established
|
// Wait for a USB connection to be established
|
||||||
while (!Serial)
|
while (!Serial)
|
||||||
@@ -37,40 +46,75 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
digitalWrite(2, HIGH);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(2, LOW);
|
|
||||||
delay(500);
|
|
||||||
|
|
||||||
Serial.println("ESP32 loopy!");
|
|
||||||
|
|
||||||
// -------------------------------------
|
|
||||||
|
|
||||||
WiFiClient client = server.available();
|
WiFiClient client = server.available();
|
||||||
if (client) {
|
if (client) {
|
||||||
Serial.println("Client connected");
|
Serial.println("Client connected");
|
||||||
|
|
||||||
// unsigned long start = 0;
|
unsigned long start = 0;
|
||||||
while (client.connected()) {
|
while (client.connected()) {
|
||||||
if (client.available()) {
|
if (client.available()) {
|
||||||
String msg = client.readStringUntil('\n');
|
String msg = client.readStringUntil('\n');
|
||||||
Serial.print("Received: ");
|
Serial.print("Received: ");
|
||||||
Serial.println(msg);
|
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");
|
client.print("ACK\n");
|
||||||
Serial.println("Sent ACK");
|
Serial.println("Sent ACK");
|
||||||
// start = millis();
|
start = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Kill lingering connections
|
// Kill lingering connections
|
||||||
// if (start != 0 && millis() - start < 1000) {
|
if (start != 0 && millis() - start > 200) {
|
||||||
// Serial.println("Client kill..");
|
Serial.println("Client kill..");
|
||||||
// break;
|
break;
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
processButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
setPowerPin(false);
|
||||||
|
setResetPin(false);
|
||||||
|
|
||||||
client.stop();
|
client.stop();
|
||||||
Serial.println("Client disconnected");
|
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