diff options
| author | Jon duSaint | 2026-02-23 20:35:01 -0800 |
|---|---|---|
| committer | Jon duSaint | 2026-02-23 20:35:01 -0800 |
| commit | dc7e31b146c9ceffeeb3c4084dcd8bc4106a96a0 (patch) | |
| tree | d8c23b944793e40fef30f7b2a23d6d5fb66a8c55 | |
| parent | 3ca2962dfb84194b276d8b04f825c563e02b74ea (diff) | |
mqtt-quail: WIP
| -rw-r--r-- | mqtt-quail.c | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/mqtt-quail.c b/mqtt-quail.c index c48fd57..f76d745 100644 --- a/mqtt-quail.c +++ b/mqtt-quail.c @@ -14,35 +14,23 @@ #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> -#include <syslog.h> #include <time.h> #include <unistd.h> #ifndef VERSION -# define VERSION "20240214" +# define VERSION "20240218" #endif #ifndef MQTT_NAME -# define MQTT_NAME "auk" +# define MQTT_NAME "quail" #endif #define I2C_BUS 1 -#define CO2_ADDR 0x5e -#define CO2_CO2_PPM 0 -#define CO2_T_C 2 -#define CO2_P_MBAR 6 #define DEFAULT_HOSTNAME "localhost" #define DEFAULT_PORT 1883 -#define MQTT_TOPIC "tiny-house/ee895/state" +#define MQTT_TOPIC "shack/quail/state" -#define info(m,...) message (LOG_INFO, "info[%s:%u]: " m, __func__, __LINE__, ##__VA_ARGS__); -#define warning(m,...) message (LOG_WARNING, "warning[%s:%u]: " m, __func__, __LINE__, ##__VA_ARGS__); -#define error(m,...) message (LOG_ERR, "error[%s:%u]: " m, __func__, __LINE__, ##__VA_ARGS__); -#define fatal(m,...) do { \ - message (LOG_EMERG, "fatal[%s:%u]: " m, __func__, __LINE__, ##__VA_ARGS__); \ - exit (EXIT_FAILURE); \ - } while (0); bool foreground = false; @@ -135,27 +123,23 @@ read_secret_file (char *file, char **username, char **password) { } } -int -ee895_setup (unsigned i2c_bus) { - char i2c_dev[PATH_MAX] = {0}; - int fd; - snprintf (i2c_dev, sizeof (i2c_dev), "/dev/i2c-%u", i2c_bus); - fd = open (i2c_dev, O_RDWR); - if (fd < 0) { - fatal ("open(%s): %s", i2c_dev, strerror (errno)); - } +int +sht3x_setup (unsigned i2c_bus) { - if (ioctl(fd, I2C_SLAVE, CO2_ADDR) < 0) { - fatal ("ioctl(%s): %s", i2c_dev, strerror (errno)); - } - return fd; + /* Set up period data acquisition mode, 1 measurement per sec., high repeatability */ + } int -ee895_read (int fd, uint16_t *co2, uint16_t *t, uint16_t *p) { +sht3x_read (int fd, uint16_t *co2, uint16_t *t, uint16_t *p) { + + + + + int i, v; struct { uint16_t *out; uint8_t addr; } to_read[] = { { co2, CO2_CO2_PPM }, { t, CO2_T_C }, { p, CO2_P_MBAR } }; @@ -188,9 +172,9 @@ discovery (struct mosquitto *mosq, char *ident, char *class, char *unit) { "\"state_topic\":\"%s\"," "\"unit_of_measurement\":\"%s\"," "\"value_template\":\"{{ value_json.%s }}\"," - "\"unique_id\":\"ee895%c0001\"," + "\"unique_id\":\"sht3x%c0001\"," "\"device\":{" - "\"identifiers\":[\"tinyhouse-ee895\"]," + "\"identifiers\":[\"tinyhouse-sht3x\"]," "\"name\":\"EE895\"," "\"model\":\"EE895\"," "\"manufacturer\":\"E+E Elektronik\"," @@ -364,7 +348,7 @@ main (int argc, char *argv[]) { mosquitto_lib_init (); - i2c_fd = ee895_setup (i2c_bus); + i2c_fd = sht3x_setup (i2c_bus); mosq = mosquitto_new (MQTT_NAME, false, NULL); if (mosq == NULL) { @@ -395,10 +379,10 @@ main (int argc, char *argv[]) { discovery (mosq, "pressure", "pressure", "mbar"); while (keep_going) { - uint16_t ee895_co2, ee895_t, ee895_p; + uint16_t sht3x_co2, sht3x_t, sht3x_p; - if (ee895_read (i2c_fd, &ee895_co2, &ee895_t, &ee895_p) == 0) { - publish (mosq, ee895_co2, ee895_t, ee895_p); + if (sht3x_read (i2c_fd, &sht3x_co2, &sht3x_t, &sht3x_p) == 0) { + publish (mosq, sht3x_co2, sht3x_t, sht3x_p); } sleep (interval); |
