expanded the esp script to send messages whenever a button is pushed OR released.

This commit is contained in:
CiscoTheWolf 2023-05-31 18:31:16 +02:00
parent 834bc01610
commit da9965cb41

View file

@ -10,7 +10,7 @@ const char* mqttTopic = "test";
const int gpioPins[] = {25, 26, 27, 14}; // GPIO pins to check for shorting
const unsigned int numPins = sizeof(gpioPins) / sizeof(gpioPins[0]);
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
const unsigned long debounceDelay = 40; // Debounce delay in milliseconds
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
@ -55,7 +55,14 @@ void mqttTask(void* parameter) {
if (mqttClient.connected()) {
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].fell()) {
String message = "Pin shorted: " + String(gpioPins[i]);
String message = "Pin fell: " + String(gpioPins[i]);
mqttClient.publish(mqttTopic, message.c_str());
Serial.println("Message sent to MQTT server");
}
}
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].rose()) {
String message = "Pin rose: " + String(gpioPins[i]);
mqttClient.publish(mqttTopic, message.c_str());
Serial.println("Message sent to MQTT server");
}
@ -99,6 +106,7 @@ void loop() {
}
bool anyPinFell = false;
bool anyPinRose = false;
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].fell()) {
@ -107,6 +115,13 @@ void loop() {
}
}
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].rose()) {
anyPinRose = true;
break;
}
}
if (anyPinFell) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(mqttTaskHandle, &xHigherPriorityTaskWoken);
@ -115,5 +130,13 @@ void loop() {
}
}
if (anyPinRose) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(mqttTaskHandle, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}
delay(10); // Adjust the delay as per your requirements
}