Moved battery measurement to attributes.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-05-21 19:02:52 +08:00
parent dfb86f939c
commit 7776d32f03
Signed by: imi415
GPG Key ID: 17F01E106F9F5E0A
4 changed files with 36 additions and 19 deletions

View File

@ -9,9 +9,6 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
/* MQTT */
#include "app_mqtt.h"
/* DHT sensor */
#include "aht10/aht10.h"

View File

@ -1,3 +1,5 @@
#include "string.h"
/* ESP drivers */
#include "esp_event.h"
#include "esp_log.h"
@ -12,7 +14,9 @@
/* App specific */
#include "app_wifi.h"
#define APP_LOG_TAG "APP_WIFI"
#define LOG_TAG "APP_WIFI"
#define APP_WIFI_AUTH_MODE WIFI_AUTH_WPA2_PSK
#define APP_WIFI_EVENT_GROUP_EVENT_CONNECTED (1 << 0U)
@ -45,20 +49,36 @@ esp_err_t app_wifi_init(void) {
ESP_ERROR_CHECK(
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_wifi_event_handler, NULL, &inst_got_ip));
wifi_config_t wifi_config = {
.sta =
{
.ssid = CONFIG_APP_WIFI_SSID,
.password = CONFIG_APP_WIFI_PASSWORD,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
},
};
wifi_config_t wifi_config;
bool has_changes = false;
ESP_ERROR_CHECK(esp_wifi_get_config(WIFI_IF_STA, &wifi_config));
if (strncmp((char *)wifi_config.sta.ssid, CONFIG_APP_WIFI_SSID, strlen(CONFIG_APP_WIFI_SSID)) != 0) {
snprintf((char *)wifi_config.sta.ssid, 32, CONFIG_APP_WIFI_SSID);
has_changes = true;
}
if (strncmp((char *)wifi_config.sta.password, CONFIG_APP_WIFI_PASSWORD, strlen(CONFIG_APP_WIFI_PASSWORD)) != 0) {
snprintf((char *)wifi_config.sta.password, 64, CONFIG_APP_WIFI_PASSWORD);
has_changes = true;
}
if (wifi_config.sta.threshold.authmode != APP_WIFI_AUTH_MODE) {
wifi_config.sta.threshold.authmode = APP_WIFI_AUTH_MODE;
has_changes = true;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
if (has_changes) {
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_LOGI(LOG_TAG, "WiFi config has changed, setting new config.");
}
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(APP_LOG_TAG, "WiFi initialized, heap free: %lu.", esp_get_free_heap_size());
ESP_LOGI(LOG_TAG, "WiFi initialized, heap free: %lu.", esp_get_free_heap_size());
return ESP_OK;
}
@ -96,7 +116,7 @@ static void app_wifi_event_handler(void *arg, esp_event_base_t event_base, int32
if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect();
ESP_LOGI(APP_LOG_TAG, "Disconnected");
ESP_LOGI(LOG_TAG, "Disconnected");
}
} else if (event_base == IP_EVENT) {
if (event_id == IP_EVENT_STA_GOT_IP) {
@ -105,7 +125,7 @@ static void app_wifi_event_handler(void *arg, esp_event_base_t event_base, int32
ip_event_got_ip_t *event = event_data;
ESP_LOGI(APP_LOG_TAG, "Connected, IP address: " IPSTR, IP2STR(&event->ip_info.ip));
ESP_LOGI(LOG_TAG, "Connected, IP address: " IPSTR, IP2STR(&event->ip_info.ip));
}
}
}

View File

@ -3,7 +3,6 @@
typedef struct {
uint64_t ts;
uint32_t voltage;
float temperature;
float humidity;
} app_report_rb_t;

View File

@ -142,7 +142,6 @@ void app_main(void) {
continue;
}
rpt.voltage = app_adc_read() * 2;
app_led_set(0);
rpt.ts = app_get_msec_timestamp();
@ -155,7 +154,6 @@ void app_main(void) {
if (current_count % 10 == 0) {
ESP_LOGI(LOG_TAG, "Last: %.2fC, %.2f%%", rpt.temperature, rpt.humidity);
ESP_LOGI(LOG_TAG, "RB level: %lu%%", current_count * 100 / rb_size);
ESP_LOGI(LOG_TAG, "Battery voltage: %lumV", rpt.voltage);
}
/**
@ -214,6 +212,9 @@ 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);
rpt_buffer = malloc(512);
while (app_report_rb_get_count() > 0) {