From a903b916f09419e7324a2352232e601acc598d67 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Thu, 28 Jan 2021 17:13:32 +0800 Subject: [PATCH] update lightd main.c, add lux sensor support and test ok --- mqttd/main.c | 36 +++++++++++++++++++++++++++++++++--- 1 files changed, 33 insertions(+), 3 deletions(-) diff --git a/mqttd/main.c b/mqttd/main.c index 98cf2d6..f021966 100644 --- a/mqttd/main.c +++ b/mqttd/main.c @@ -254,7 +254,8 @@ mqtt_ctx_t *ctx = (mqtt_ctx_t *)userdata; int rv = 0; char msg[128]; - float temp = 0.0; + float temp = 0.0; /* temperature */ + float rh = 0.0; /* relative humidity */ int retain = 0; if( result ) @@ -274,7 +275,7 @@ else snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp); - rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain); + rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain); if( rv ) { log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv); @@ -284,6 +285,27 @@ log_nrml("Publisher broadcast message '%s' ok\n", msg); } } + + if( ctx->hwconf.sht2x ) + { + memset(msg, 0, sizeof(msg)); + + if( sht2x_get_temp_humidity(&temp, &rh) < 0 ) + snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\", \"RH\":\"error\" }", ctx->id); + else + snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\", \"RH\":\"%.2f\" }", ctx->id, temp, rh); + + rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain); + if( rv ) + { + log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv); + } + else + { + log_nrml("Publisher broadcast message '%s' ok\n", msg); + } + } + log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); mosquitto_disconnect(mosq); @@ -442,7 +464,15 @@ { if( strcasestr(value, "on") ) { - log_nrml("Turn buzzer on [%d] times\n", hwconf->beep_times); + turn_passive_beep(ON, BEEP_FREQ); + } + else if( strcasestr(value, "off") ) + { + turn_passive_beep(OFF, BEEP_FREQ); + } + else if( strcasestr(value, "music") ) + { + play_little_star(); } } -- Gitblit v1.9.1