From 7f34b7ce235c0ddb8a1ae0f6e094090a0aaf3da1 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Sun, 21 May 2023 20:01:34 +0800 Subject: [PATCH] Updated report format. Signed-off-by: Yilin Sun --- main/main.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/main/main.c b/main/main.c index d185a67..817f1e5 100644 --- a/main/main.c +++ b/main/main.c @@ -13,7 +13,9 @@ #include "esp_adc/adc_cali.h" #include "esp_adc/adc_cali_scheme.h" #include "esp_adc/adc_oneshot.h" +#include "esp_app_desc.h" #include "esp_log.h" +#include "esp_mac.h" #include "esp_sleep.h" #include "esp_sntp.h" @@ -40,7 +42,7 @@ #define APP_SNTP_TIMEOUT_SECS 60 #define APP_SENSOR_INTERVAL_SECS 1 -#define APP_SENSOR_REPORT_TRIGGER_POINTS 20 /* Count of the trigger points which triggers telemetry reporting */ +#define APP_SENSOR_REPORT_TRIGGER_POINTS 4 /* Count of the trigger points which triggers telemetry reporting */ #define APP_SENSOR_REPORT_WATERMARK_PERCENT 80 /* when the buffer reaches this watermark in each trigger point */ #define APP_SENSOR_REPORT_CRITICAL_PERCENT 95 /* Critical level, when this is reached, reports will be flushed */ @@ -49,6 +51,14 @@ #define APP_LED_PIN 16 #define APP_ADC_CH ADC_CHANNEL_6 +#define APP_TELEMETRY_TOPIC "v1/devices/me/telemetry" +#define APP_ATTRIBUTE_TOPIC "v1/devices/me/attributes" + +#define APP_ATTRIBUTE_JSON_STRING "{\"firmwareVersion\":\"%s\", \"macAddress\":\"" MACSTR "\"}" +#define APP_TELEMETRY_JSON_STRING \ + "{\"ts\":%lld,\"values\":{\"temperature\":%.2f,\"humidity\":%.2f,\"batteryVoltage\":%ld}}" + +static void app_report_attributes(void); static void app_report_task(void *pvParameters); static uint64_t app_get_msec_timestamp(void); @@ -112,6 +122,9 @@ void app_main(void) { sntp_stop(); ESP_ERROR_CHECK(app_mqtt_init(APP_MQTT_TIMEOUT_SECS * 1000)); + + app_report_attributes(); + ESP_ERROR_CHECK(app_mqtt_deinit()); /* @@ -220,8 +233,8 @@ static void app_report_task(void *pvParameters) { while (app_report_rb_get_count() > 0) { app_report_rb_consume(&rpt); - // TODO: Create new report - // app_mqtt_publish(INFLUX_TOPIC, rpt_buffer); + snprintf(rpt_buffer, 512, APP_TELEMETRY_JSON_STRING, rpt.ts, rpt.temperature, rpt.humidity, batt_voltage); + app_mqtt_publish(APP_TELEMETRY_TOPIC, rpt_buffer); } free(rpt_buffer); @@ -254,6 +267,18 @@ drop_data_exit: vTaskDelete(NULL); } +static void app_report_attributes(void) { + char rpt_buf[256]; + uint8_t mac_addr[6]; + + const esp_app_desc_t *app_descr = esp_app_get_description(); + esp_read_mac(mac_addr, ESP_MAC_WIFI_STA); + + snprintf(rpt_buf, 256, APP_ATTRIBUTE_JSON_STRING, app_descr->version, MAC2STR(mac_addr)); + + app_mqtt_publish(APP_ATTRIBUTE_TOPIC, rpt_buf); +} + static uint64_t app_get_msec_timestamp(void) { struct timeval tv_now; gettimeofday(&tv_now, NULL);