diff --git a/ESP/mqtt_test/mqtt_test.ino b/ESP/mqtt_test/mqtt_test.ino index be528c2..4cf13aa 100644 --- a/ESP/mqtt_test/mqtt_test.ino +++ b/ESP/mqtt_test/mqtt_test.ino @@ -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 } \ No newline at end of file