From: jweigele Date: Mon, 11 Mar 2024 16:33:17 +0000 (-0700) Subject: Attempt to deal with rapidly oscillating true/false motion GPIOs X-Git-Url: http://git.hexthepla.net/?a=commitdiff_plain;h=b579e2ec239d5952ab4267a7a3856e98c2ea0f89;p=esp32projects Attempt to deal with rapidly oscillating true/false motion GPIOs --- diff --git a/aqi/main/aqi.c b/aqi/main/aqi.c index 35a88a1..22949ae 100644 --- a/aqi/main/aqi.c +++ b/aqi/main/aqi.c @@ -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();