Attempt to deal with rapidly oscillating true/false motion GPIOs
authorjweigele <jweigele@local>
Mon, 11 Mar 2024 16:33:17 +0000 (09:33 -0700)
committerjweigele <jweigele@local>
Mon, 11 Mar 2024 16:33:17 +0000 (09:33 -0700)
aqi/main/aqi.c

index 35a88a1b995852b4354a89f65ad5df62c5941d25..22949ae42e94d851204ba2c1f58c41b24a9c479a 100644 (file)
@@ -75,6 +75,9 @@ static char ext_string[16 + 1];
 #if defined(CONFIG_MOTION_FIRST_ENABLED) || defined(CONFIG_MOTION_SECOND_ENABLED)
 static QueueHandle_t motion_queue;
 static QueueHandle_t check_queue;
+static int64_t last_motion_true_time = 0;
+static int64_t last_motion_false_time = 0;
+int64_t MOTION_SEND_BACKOFF_MS = 5000;
 
 static int prev_motion[2] = {0, 0};
 static int motion_pins[2] = {0, 0};
@@ -692,10 +695,35 @@ static void send_task(void* discard){
 #if defined(CONFIG_WIFI_ENABLED) || defined(CONFIG_OT_ENABLED)
 #if defined(CONFIG_MOTION_FIRST_ENABLED) || defined(CONFIG_MOTION_SECOND_ENABLED)
 static void send_report_motion(bool motion){
+    // set the current motion variable regardless of what we do subsequently
     xSemaphoreTake(report_data.mutex, portMAX_DELAY);
     report_data.motion = motion;
     xSemaphoreGive(report_data.mutex);
 
+    // this section is meant to squelch rapidly oscilating GPIOs so that we don't
+    // spam mqtt. separated into true and false times just so you don't have a false stamp out
+    // a subsequent true (and have to wait for the summary report to realize you moved)
+
+
+    // microseconds
+    int64_t cur_uptime = esp_timer_get_time();
+    if ( motion == true ){
+        if (cur_uptime < last_motion_true_time + MOTION_SEND_BACKOFF_MS*1000){
+            // we recently sent out a motion true event on mqtt, ignore and return
+            return;
+        } else {
+            last_motion_true_time = cur_uptime;
+        }
+    } else {
+        if (cur_uptime < last_motion_false_time + MOTION_SEND_BACKOFF_MS*1000){
+            // we recently sent out a motion false event on mqtt, ignore and return
+            return;
+        } else {
+            last_motion_false_time = cur_uptime;
+        }
+    }
+
+    // if we got here we actually do want to send the data
     ESP_LOGI(TAG, "send_report_motion was called!");
     cJSON *root;
     root = cJSON_CreateObject();