report and send average (over 1 min) pm25 for aqi
authorjweigele <jweigele@local>
Thu, 13 Jul 2023 15:06:53 +0000 (08:06 -0700)
committerjweigele <jweigele@local>
Thu, 13 Jul 2023 15:06:53 +0000 (08:06 -0700)
aqi/main/esp_zb_light.c
aqi/main/esp_zb_light.h

index 4585347781c68c520742b0ffbfe36a6798741a1e..3a14d0670e634ce791fe7568d3f1ad294fdb8320 100644 (file)
@@ -35,11 +35,13 @@ typedef struct zdo_info_ctx_s {
 } zdo_info_user_ctx_t;
 
 static short temp_list[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-static const char *TAG = "ESP_ZB_COLOR_DIMM_LIGHT";
+static const char *TAG = "aqi";
 static OW ow;
 static bool zig_connected = false;
 static uint16_t cur_pm25 = 0;
 
+static float pm25buffer[PM25BUFFERSIZE];
+static int pm25bufferindex = 0;
 
 /********************* Define functions **************************/
 static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
@@ -63,6 +65,34 @@ bool init_temp(){
 }
 
 
+// add new value to ringbuffer and return average of all, downcast to uint16_t
+uint16_t new_pm25_average(float new_val){
+    pm25buffer[pm25bufferindex++] = new_val;
+    // rollover
+    if (pm25bufferindex == PM25BUFFERSIZE){
+        pm25bufferindex = 0;
+    }
+
+    float total = 0;
+    int count = 0;
+    for (int i = 0; i < PM25BUFFERSIZE; i++ ){
+        // ignore initial values
+        if ( pm25buffer[i] != -1.0 ){
+            total += pm25buffer[i];
+            count++;
+        }
+    }
+    uint16_t retval = (uint16_t)(total*100/count);
+    ESP_LOGI(TAG, "returning average pm25 to send: %d", retval);
+    return retval;
+
+}
+
+void init_pm25buffer(){
+    for (int i = 0; i < PM25BUFFERSIZE; i++){
+        pm25buffer[i] = -1.0;
+    }
+}
 void init_uart(void) {
     const uart_config_t uart_config = {
         .baud_rate = 9600,
@@ -227,10 +257,10 @@ static void monitoring_task(void* discard)
                 float cur_avg = get_average_pm25(data, rxBytes);
                 ESP_LOGI(TAG, "average from last 10 seconds pm25: %f", cur_avg);
                 ESP_LOGI(TAG, "setting attrib value and sending, I guess");
-                cur_pm25 = cur_avg*100;
+                cur_pm25 = new_pm25_average(cur_avg);
                 esp_zb_zcl_set_attribute_val(HA_ESP_TEMP_ENDPOINT, PM25CLUSTER, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
                                 PM25MEASURED, &cur_pm25, false);
-                //send_report(PM25_REPORT);
+                send_report(PM25_REPORT);
 
             } else {
                 ESP_LOGI(TAG, "Nothing seen from uart");
@@ -422,6 +452,7 @@ void app_main(void)
     };
     ESP_ERROR_CHECK(nvs_flash_init());
     init_uart();
+    init_pm25buffer();
     /* load Zigbee light_bulb platform config to initialization */
     ESP_ERROR_CHECK(esp_zb_platform_config(&config));
     /* hardware related and device init */
index 885995a250d2ceb153d178e2ac165277447dd30b..62b336d420faa74507f412590c7f0fa5f02fd37e 100644 (file)
@@ -33,6 +33,9 @@
 #define TEMP_REPORT 0x1
 #define PM25_REPORT 0x2
 
+
+#define PM25BUFFERSIZE 6
+
 static const int RX_BUF_SIZE = 1024;
 
 //#define ZB_DISTRIBUTED_GLOBAL_KEY {0x1, 0x3, 0x5, 0x7, 0x9, 0xb, 0xd, 0xf, 0x0, 0x2, 0x4, 0x6, 0x8, 0xa, 0xc, 0xd};