SystemAgent/src/tasks/user_dht_task.c

73 lines
1.8 KiB
C
Raw Normal View History

2021-07-06 17:40:58 +00:00
#include <stdint.h>
#include "impl/user_bme280_impl.h"
#include "tasks/user_task_lvgl_common.h"
2021-07-25 15:22:04 +00:00
#include "tasks/user_task_mqtt_common.h"
2021-07-06 17:40:58 +00:00
void *user_dht_task(void *arguments);
pthread_t user_dht_task_thread;
int user_dht_task_init(void) {
int ret;
ret = pthread_create(&user_dht_task_thread, NULL, user_dht_task, NULL);
if(ret) return ret;
pthread_setname_np(user_dht_task_thread, "DHT");
2021-07-25 15:22:04 +00:00
return 0;
2021-07-06 17:40:58 +00:00
}
int user_dht_task_deinit(void) {
pthread_join(user_dht_task_thread, NULL);
return 0;
}
void *user_dht_task(void *arguments) {
while(g_running && !g_lvgl_ready) {
sleep(1);
}
user_bme280_impl_t bme_impl;
user_bme280_impl_init(&bme_impl);
bme280_t bme = {
.cb =
{
.read_register_cb =
(bme280_ret_t(*)(void *, uint8_t, uint8_t *,
uint8_t))user_bme280_impl_read_register_cb,
.write_register_cb = (bme280_ret_t(*)(void *, uint8_t, uint8_t))
user_bme280_impl_write_register_cb,
2021-07-25 15:22:04 +00:00
.delay_cb = (bme280_ret_t(*)(
void *, uint32_t))user_bme280_impl_delay_cb,
2021-07-06 17:40:58 +00:00
},
2021-07-25 15:22:04 +00:00
.user_data = &bme_impl};
2021-07-06 17:40:58 +00:00
bme280_config_t cfg;
2021-07-25 15:22:04 +00:00
if(bme280_init(&bme) == BME_FAIL) {
USER_LOG(USER_LOG_FATAL, "BME280 init failed.");
}
2021-07-06 17:40:58 +00:00
bme280_preset_config(&cfg, BME_PRESET_WEATHER);
bme280_apply_config(&bme, &cfg);
bme280_result_t res;
2021-07-25 15:22:04 +00:00
mqtt_influx_measure_t meas;
char value_buf[32];
meas.measurement = "dht";
2021-07-06 17:40:58 +00:00
while(g_running) {
bme280_measure(&bme, &res);
2021-07-25 15:22:04 +00:00
meas.key = "temperature";
snprintf(value_buf, 32, "%.02f", res.temperature);
meas.value = value_buf;
mqtt_influx_publish_measurement(&g_mqtt_influx, &meas);
2021-07-06 17:40:58 +00:00
sleep(1);
}
2021-07-25 15:22:04 +00:00
return NULL;
2021-07-14 14:44:04 +00:00
}