From 3d72616ed47437109a4c8802d8715cc69389b374 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Mon, 22 May 2023 09:22:28 +0800 Subject: [PATCH] Moved battery voltage to attributes. Signed-off-by: Yilin Sun --- main/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/main/main.c b/main/main.c index 817f1e5..6dc1ae5 100644 --- a/main/main.c +++ b/main/main.c @@ -54,9 +54,11 @@ #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}}" +#define APP_ATTRIBUTE_JSON_STRING \ + "{\"firmwareVersion\":\"%s\", \"macAddress\":\"" MACSTR \ + "\",\"hasHumidity\":true,\"hasPressure\":false,\"batteryPowered\":true,\"batteryType\":\"Li-ion\"," \ + "\"batteryVoltage\":%ld}" +#define APP_TELEMETRY_JSON_STRING "{\"ts\":%lld,\"values\":{\"temperature\":%.2f,\"humidity\":%.2f}}" static void app_report_attributes(void); static void app_report_task(void *pvParameters); @@ -91,6 +93,10 @@ void app_main(void) { /* NVS initialization reported failure, this should not happen. In this case, abort! */ ESP_ERROR_CHECK(ret); + /* Some indication... */ + app_led_init(); + app_adc_init(); + ESP_ERROR_CHECK(app_wifi_init()); app_wifi_event_t a_wifi_evt = app_wifi_wait_event(APP_WIFI_TIMEOUT_SECS * 1000); @@ -133,10 +139,6 @@ void app_main(void) { */ ESP_ERROR_CHECK(app_wifi_deinit()); - /* Some indication... */ - app_led_init(); - app_adc_init(); - /* Initialize DHT sensors, abort if error occurs */ ESP_ERROR_CHECK(app_dht_init()); @@ -225,15 +227,14 @@ static void app_report_task(void *pvParameters) { ESP_ERROR_CHECK(app_mqtt_init(APP_MQTT_TIMEOUT_SECS * 1000)); - uint32_t batt_voltage = app_adc_read() * 2; - ESP_LOGI(LOG_TAG, "Battery voltage: %lumV", batt_voltage); + app_report_attributes(); rpt_buffer = malloc(512); while (app_report_rb_get_count() > 0) { app_report_rb_consume(&rpt); - snprintf(rpt_buffer, 512, APP_TELEMETRY_JSON_STRING, rpt.ts, rpt.temperature, rpt.humidity, batt_voltage); + snprintf(rpt_buffer, 512, APP_TELEMETRY_JSON_STRING, rpt.ts, rpt.temperature, rpt.humidity); app_mqtt_publish(APP_TELEMETRY_TOPIC, rpt_buffer); } @@ -273,8 +274,9 @@ static void app_report_attributes(void) { const esp_app_desc_t *app_descr = esp_app_get_description(); esp_read_mac(mac_addr, ESP_MAC_WIFI_STA); + uint32_t batt_voltage = app_adc_read() * 2; - snprintf(rpt_buf, 256, APP_ATTRIBUTE_JSON_STRING, app_descr->version, MAC2STR(mac_addr)); + snprintf(rpt_buf, 256, APP_ATTRIBUTE_JSON_STRING, app_descr->version, MAC2STR(mac_addr), batt_voltage); app_mqtt_publish(APP_ATTRIBUTE_TOPIC, rpt_buf); }