Moved battery voltage to attributes.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-05-22 09:22:28 +08:00
parent 7f34b7ce23
commit 3d72616ed4
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
1 changed files with 13 additions and 11 deletions

View File

@ -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);
}