|
|
@ -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
|
|
|
|
|
|
|
|
if (start != 0 && millis() - start > 200) { |
|
|
|
|
|
|
|
Serial.println("Client kill.."); |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// // Kill lingering connections
|
|
|
|
processButtons(); |
|
|
|
// if (start != 0 && millis() - start < 1000) {
|
|
|
|
|
|
|
|
// Serial.println("Client kill..");
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|