} 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)
}
+// 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,
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");
};
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 */
#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};