Updated InfluxDB line protocol driver.
This commit is contained in:
parent
a132925848
commit
11a985abeb
|
@ -45,13 +45,21 @@ mqtt_influx_ret_t mqtt_influx_publish_measurement(mqtt_influx_t *influx,
|
||||||
mqtt_buf[i] = '\0';
|
mqtt_buf[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mqtt_influx_measure_item_t *item_ptr = meas->first_item;
|
||||||
|
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, meas->measurement);
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, meas->measurement);
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, ",hostname=");
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, ",hostname=");
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, influx->hostname);
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, influx->hostname);
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, " ");
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, " ");
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, meas->key);
|
do {
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, "=");
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, item_ptr->key);
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, meas->value);
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, "=");
|
||||||
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, item_ptr->value);
|
||||||
|
item_ptr = item_ptr->next;
|
||||||
|
if(item_ptr) {
|
||||||
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, ",");
|
||||||
|
}
|
||||||
|
} while(item_ptr != (void *)0);
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, " ");
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, " ");
|
||||||
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, ns_ts);
|
concat_string(mqtt_buf, MAX_MQTT_REPORT_SIZE, ns_ts);
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,14 @@ typedef struct {
|
||||||
} mqtt_influx_cb_t;
|
} mqtt_influx_cb_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *measurement;
|
|
||||||
char *key;
|
char *key;
|
||||||
char *value;
|
char *value;
|
||||||
|
void *next;
|
||||||
|
} mqtt_influx_measure_item_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *measurement;
|
||||||
|
mqtt_influx_measure_item_t *first_item;
|
||||||
} mqtt_influx_measure_t;
|
} mqtt_influx_measure_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -72,6 +72,27 @@ agent: {
|
||||||
fs_base = "lvgl_fs";
|
fs_base = "lvgl_fs";
|
||||||
logging_enabled = false;
|
logging_enabled = false;
|
||||||
};
|
};
|
||||||
|
mqtt: {
|
||||||
|
server: {
|
||||||
|
host = "127.0.0.1";
|
||||||
|
port = 1883;
|
||||||
|
keepalive = 30;
|
||||||
|
topic = "test_topic";
|
||||||
|
};
|
||||||
|
client: {
|
||||||
|
id_prefix = "SA_";
|
||||||
|
username = "test_account";
|
||||||
|
password = "test_password";
|
||||||
|
};
|
||||||
|
tls: {
|
||||||
|
enabled = true;
|
||||||
|
psk = "";
|
||||||
|
ca_file = ""; # Either ca_file or ca_path must not be null.
|
||||||
|
ca_path = "/etc/ca-certificates";
|
||||||
|
client_cert_file = "";
|
||||||
|
client_key_file = "";
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
theme: {
|
theme: {
|
||||||
|
|
|
@ -144,13 +144,11 @@ lv_fs_res_t user_lvgl_impl_fs_close_cb(lv_fs_drv_t *drv, void *file_p) {
|
||||||
int fd = *(int *)file_p;
|
int fd = *(int *)file_p;
|
||||||
|
|
||||||
if(fd > 0) {
|
if(fd > 0) {
|
||||||
|
USER_LOG(USER_LOG_DEBUG, "Free'd fd %d@%p", fd, file_p);
|
||||||
free(file_p);
|
free(file_p);
|
||||||
|
|
||||||
USER_LOG(USER_LOG_DEBUG, "Free'd fd %d@%p", fd, file_p);
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
USER_LOG(USER_LOG_DEBUG, "Called close() on fd %d", fd);
|
USER_LOG(USER_LOG_DEBUG, "Called close() on fd %d", fd);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
return LV_FS_RES_OK;
|
return LV_FS_RES_OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,8 @@ extern user_config_t g_config;
|
||||||
* http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
|
* http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
|
||||||
*/
|
*/
|
||||||
static void user_moqtt_impl_cb_on_connect(struct mosquitto *mosq, void *obj, int rc) {
|
static void user_moqtt_impl_cb_on_connect(struct mosquitto *mosq, void *obj, int rc) {
|
||||||
|
UNUSED(mosq);
|
||||||
|
|
||||||
switch(rc) {
|
switch(rc) {
|
||||||
case 0:
|
case 0:
|
||||||
USER_LOG(USER_LOG_INFO, "MQTT connection accepted.");
|
USER_LOG(USER_LOG_INFO, "MQTT connection accepted.");
|
||||||
|
@ -63,6 +65,9 @@ static void user_moqtt_impl_cb_on_connect(struct mosquitto *mosq, void *obj, int
|
||||||
* @param rc disconnect reason, 0 is expected.
|
* @param rc disconnect reason, 0 is expected.
|
||||||
*/
|
*/
|
||||||
static void user_mqtt_impl_cb_on_disconnect(struct mosquitto *mosq, void *obj, int rc) {
|
static void user_mqtt_impl_cb_on_disconnect(struct mosquitto *mosq, void *obj, int rc) {
|
||||||
|
UNUSED(mosq);
|
||||||
|
UNUSED(obj);
|
||||||
|
|
||||||
if(rc != 0) {
|
if(rc != 0) {
|
||||||
USER_LOG(USER_LOG_WARN, "MQTT received unexpected disconnect event (%d).", rc);
|
USER_LOG(USER_LOG_WARN, "MQTT received unexpected disconnect event (%d).", rc);
|
||||||
} else {
|
} else {
|
||||||
|
@ -79,6 +84,9 @@ static void user_mqtt_impl_cb_on_disconnect(struct mosquitto *mosq, void *obj, i
|
||||||
* @param str log message
|
* @param str log message
|
||||||
*/
|
*/
|
||||||
static void user_mqtt_impl_cb_log(struct mosquitto *mosq, void *obj, int level, const char *str) {
|
static void user_mqtt_impl_cb_log(struct mosquitto *mosq, void *obj, int level, const char *str) {
|
||||||
|
UNUSED(mosq);
|
||||||
|
UNUSED(obj);
|
||||||
|
|
||||||
user_log_level_t log_level;
|
user_log_level_t log_level;
|
||||||
|
|
||||||
switch(level) {
|
switch(level) {
|
||||||
|
@ -270,6 +278,7 @@ int user_mqtt_impl_init(user_mqtt_impl_t *handle) {
|
||||||
* @return 0 if success, negative value if error.
|
* @return 0 if success, negative value if error.
|
||||||
*/
|
*/
|
||||||
int user_mqtt_impl_deinit(user_mqtt_impl_t *handle) {
|
int user_mqtt_impl_deinit(user_mqtt_impl_t *handle) {
|
||||||
|
mosquitto_disconnect(handle->mosq);
|
||||||
mosquitto_destroy(handle->mosq);
|
mosquitto_destroy(handle->mosq);
|
||||||
mosquitto_lib_cleanup();
|
mosquitto_lib_cleanup();
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ int main(int argc, const char *argv[]) {
|
||||||
USER_LOG(USER_LOG_INFO, "Application started.");
|
USER_LOG(USER_LOG_INFO, "Application started.");
|
||||||
|
|
||||||
int signal_arr[] = { SIGINT, SIGTERM };
|
int signal_arr[] = { SIGINT, SIGTERM };
|
||||||
for(uint8_t i = 0; i < sizeof(signal_arr) / sizeof(int); i++) {
|
for(unsigned int i = 0; i < sizeof(signal_arr) / sizeof(int); i++) {
|
||||||
if(signal(signal_arr[i], signal_handler) == SIG_ERR) {
|
if(signal(signal_arr[i], signal_handler) == SIG_ERR) {
|
||||||
USER_LOG(USER_LOG_FATAL, "Cannot register signal handler %s.", strsignal(signal_arr[i]));
|
USER_LOG(USER_LOG_FATAL, "Cannot register signal handler %s.", strsignal(signal_arr[i]));
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -58,15 +58,31 @@ void *user_dht_task(void *arguments) {
|
||||||
|
|
||||||
bme280_result_t res;
|
bme280_result_t res;
|
||||||
mqtt_influx_measure_t meas;
|
mqtt_influx_measure_t meas;
|
||||||
char value_buf[32];
|
mqtt_influx_measure_item_t meas_item[3];
|
||||||
|
char temperature_buf[32];
|
||||||
|
char humidity_buf[32];
|
||||||
|
char pressure_buf[32];
|
||||||
|
|
||||||
meas.measurement = "dht";
|
meas.measurement = "dht";
|
||||||
|
meas.first_item = meas_item;
|
||||||
|
|
||||||
|
meas_item[0].key = "temperature";
|
||||||
|
meas_item[0].next = &meas_item[1];
|
||||||
|
|
||||||
|
meas_item[1].key = "humidity";
|
||||||
|
meas_item[1].next = &meas_item[2];
|
||||||
|
|
||||||
|
meas_item[2].key = "pressure";
|
||||||
|
meas_item[2].next = NULL;
|
||||||
|
|
||||||
while(g_running) {
|
while(g_running) {
|
||||||
bme280_measure(&bme, &res);
|
bme280_measure(&bme, &res);
|
||||||
meas.key = "temperature";
|
snprintf(temperature_buf, 32, "%.02f", res.temperature);
|
||||||
snprintf(value_buf, 32, "%.02f", res.temperature);
|
snprintf(humidity_buf, 32, "%.02f", res.humidity);
|
||||||
meas.value = value_buf;
|
snprintf(pressure_buf, 32, "%.02f", res.pressure);
|
||||||
|
meas_item[0].value = temperature_buf;
|
||||||
|
meas_item[1].value = humidity_buf;
|
||||||
|
meas_item[2].value = pressure_buf;
|
||||||
mqtt_influx_publish_measurement(&g_mqtt_influx, &meas);
|
mqtt_influx_publish_measurement(&g_mqtt_influx, &meas);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,11 +44,17 @@ int user_mqtt_task_deinit(void) {
|
||||||
void *user_mqtt_task(void *arguments) {
|
void *user_mqtt_task(void *arguments) {
|
||||||
UNUSED(arguments);
|
UNUSED(arguments);
|
||||||
|
|
||||||
user_mqtt_impl_init(&s_mqtt_impl);
|
if(user_mqtt_impl_init(&s_mqtt_impl) != 0) {
|
||||||
|
USER_LOG(USER_LOG_ERROR, "MQTT implementation init failed.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
mqtt_influx_init(&g_mqtt_influx);
|
mqtt_influx_init(&g_mqtt_influx);
|
||||||
|
|
||||||
while(g_running) {
|
while(g_running) {
|
||||||
user_mqtt_network_loop(&s_mqtt_impl);
|
if(user_mqtt_network_loop(&s_mqtt_impl) !=0) {
|
||||||
|
USER_LOG(USER_LOG_ERROR, "MQTT network loop returned failure.");
|
||||||
|
}
|
||||||
usleep(5 * 1000);
|
usleep(5 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue