From 9e2630a8f3887df3d8f11ee6a27d0d34b331ef28 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Tue, 18 Nov 2025 11:21:53 +0800
Subject: [PATCH] update thingsboard defconfig file

---
 project/lightd/lightd.c |  164 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 106 insertions(+), 58 deletions(-)

diff --git a/project/lightd/lightd.c b/project/lightd/lightd.c
index 158a3dd..22ce0cb 100644
--- a/project/lightd/lightd.c
+++ b/project/lightd/lightd.c
@@ -26,18 +26,17 @@
 #include "util_proc.h"
 #include "config.h"
 #include "tsl2561.h"
+#include "ds18b20.h"
 
 #define PROG_VERSION               "v1.0.0"
 #define DAEMON_PIDFILE             "/tmp/.lightd.pid"
 
-int check_sample_time(time_t *last_time, int interval);
-void *thingsboard_subsciber(void *args);
-void *thingsboard_publisher(void *args);
+void *thingsboard_worker(void *args);
 
 static void program_usage(char *progname)
 {
     printf("Usage: %s [OPTION]...\n", progname);
-    printf(" %s is LingYun studio MQTT daemon program running on RaspberryPi\n", progname);
+    printf(" %s is LingYun studio lightd program running on RaspberryPi\n", progname);
 
     printf("\nMandatory arguments to long options are mandatory for short options too:\n");
     printf(" -b[daemon  ]  Running in daemon mode\n");
@@ -127,17 +126,13 @@
     /* initial gpio hardware */
     gpio_init(&hwinfo->gpio);
 
-    /*
-     * +--------------------------------+
-     * |    MQTT Subscriber Thread      |
-     * +--------------------------------+
-     */
-    if( thread_start(&tid, thingsboard_subsciber, &ctx.mqtt ) < 0 )
+    /* start thingsboard MQTT thread */
+    if( thread_start(&tid, thingsboard_worker, &ctx.mqtt ) < 0 )
     {
-        log_error("Start MQTT subsciber worker thread failure\n");
+        log_error("Start thingsboard MQTT worker thread failure\n");
         goto cleanup;
     }
-    log_info("Start MQTT subsciber worker thread ok\n");
+    log_info("Start thingsboard MQTT worker thread ok\n");
 
     /*
      * +--------------------------------+
@@ -148,12 +143,22 @@
     while( ! g_signal.stop )
     {
         /* TSL2561 update lux in every 5 minutes  */
-        if(check_sample_time(&last_time, 300))
+        if(check_timeout(&last_time, 300))
         {
+            /* The TSL2561 sensor is not present, do not turn on the light */
+            if( !hwinfo->tsl2561 )
+            {
+                log_error("TSL2561 is not present, do not turn on the light\n");
+                lux = hwinfo->lux_threshold + 100.0;
+                goto nextloop;
+            }
+
+            /* The TSL2561 sensor failed, do not turn on the light */
             if( tsl2561_get_lux(&lux) < 0 )
             {
-                log_error("TSL2561 sample Lux failed\n");
+                log_error("TSL2561 sample failed, do not turn on the light\n");
                 lux = hwinfo->lux_threshold + 100.0;
+                goto nextloop;
             }
 
             if( lux > hwinfo->lux_threshold )
@@ -162,12 +167,13 @@
                 log_info("Lux[%.3f] <= Treshold[%.3f], need auto light\n", lux, hwinfo->lux_threshold);
         }
 
+        /* It's bright enough, no lighting is needed */
         if( lux > hwinfo->lux_threshold )
         {
-            sleep(1);
-            continue;
+            goto nextloop;
         }
 
+        /* It's not bright enough, lighting is necessary */
         rv = infrared_detect();
         if( rv != 0 )
         {
@@ -182,7 +188,8 @@
             turn_light("hallway", OFF);
         }
 
-        msleep(100);
+nextloop:
+        sleep(1);
     }
 
 cleanup:
@@ -192,43 +199,51 @@
     return 0;
 }
 
-int check_sample_time(time_t *last_time, int interval)
+/*
+ * +--------------------------------+
+ * |    ThingsBoard MQTT Thread     |
+ * +--------------------------------+
+ */
+
+/* process publisher(uplink) data */
+void pub_connect_callback(struct mosquitto *mosq, void *userdata, int result)
 {
-    int                  need = 0; /* no need sample now */
-    time_t               now;
+    mqtt_ctx_t             *mqtt = (mqtt_ctx_t *)userdata;
+    iotd_ctx_t             *iotd = to_iotd(mqtt);
+    int                     rv = 0;
+    char                    msg[128];
+    float                   temp = 0.0; /* temperature */
+    int                     retain = 0;
+    static time_t           last_time = 0;
 
-    time(&now);
-
-    if( difftime(now, *last_time)>interval )
-    {
-        need = 1; /* need sample now  */
-        *last_time = now;
-    }
-
-    return need;
-}
-
-void sub_connect_callback(struct mosquitto *mosq, void *userdata, int result)
-{
-    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
-
-    if( result )
-    {
-        log_error("Subscriber connect to broker server failed, rv=%d\n", result);
+    /* publish time is not arrive */
+    if( !check_timeout(&last_time, mqtt->interval) )
         return ;
+
+    log_debug("Publish topic '%s'\n", mqtt->pubTopic);
+    if( iotd->hwinfo.ds18b20 )
+    {
+        log_debug("DS18B20 temperature sensor enabled, start broadcast it\n");
+        if( ds18b20_get_temperature(&temp)<0 )
+            return ;
+
+        memset(msg, 0, sizeof(msg));
+        snprintf(msg, sizeof(msg), "{\"temperature\":%.2f}", temp);
+        rv = mosquitto_publish(mosq, NULL, mqtt->pubTopic, strlen(msg), msg, mqtt->pubQos, retain);
+        if( rv )
+        {
+            log_error("Publisher broadcast message '%s' failure: %d\n", msg, rv);
+        }
+        else
+        {
+            log_info("Publisher broadcast message '%s' ok\n", msg);
+        }
     }
 
-    log_info("Subscriber connect to broker server[%s:%d] successfully\n", ctx->host, ctx->port);
-    mosquitto_subscribe(mosq, NULL, ctx->subTopic, ctx->subQos);
+    return ;
 }
 
-void sub_disconnect_callback(struct mosquitto *mosq, void *userdata, int result)
-{
-    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
-
-    log_warn("Subscriber disconnect to broker server[%s:%d], reason=%d\n", ctx->host, ctx->port, result);
-}
-
+/* process subscriber(downlink) data */
 void proc_json_items(cJSON *root, mqtt_ctx_t *ctx)
 {
     cJSON *item_method;
@@ -282,8 +297,6 @@
     }
     status = cJSON_IsTrue(item_status) ? ON : OFF;
 
-    log_info("parser JSON message to control '%s' status '%d'\n", device, status);
-
     /* 映射 Light 名称并调用控制函数 */
     if ( strstr(device, "Indoor") || strstr(device, "indoor"))
     {
@@ -318,6 +331,7 @@
         return ;
     }
 
+    /* process receive message data */
     proc_json_items(root, ctx);
 
     /* must delete it, or it will result memory leak */
@@ -326,11 +340,33 @@
     return ;
 }
 
-void *thingsboard_subsciber(void *args)
+void mqtt_connect_callback(struct mosquitto *mosq, void *userdata, int result)
+{
+    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
+
+    if( result )
+    {
+        log_error("mosquitto connect to broker server failed, rv=%d\n", result);
+        return ;
+    }
+
+    log_info("mosquitto connect to broker server[%s:%d] successfully\n", ctx->host, ctx->port);
+    mosquitto_subscribe(mosq, NULL, ctx->subTopic, ctx->subQos);
+}
+
+void mqtt_disconnect_callback(struct mosquitto *mosq, void *userdata, int result)
+{
+    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
+
+    log_warn("mosquitto disconnect to broker server[%s:%d], reason=%d\n", ctx->host, ctx->port, result);
+}
+
+void *thingsboard_worker(void *args)
 {
     mqtt_ctx_t             *ctx = (mqtt_ctx_t *)args;
     struct mosquitto       *mosq;
     bool                    session = true;
+    int                     rv = 0;
 
     mosq = mosquitto_new(ctx->devid, session, ctx);
     if( !mosq )
@@ -342,7 +378,7 @@
     /* connnect to broker by token or uid/pwd */
     if( strlen(ctx->token ) > 0)
     {
-        log_info("Using token authentication\n");
+        log_info("Using token authentication: '%s'\n", ctx->token);
         mosquitto_username_pw_set(mosq, ctx->token, NULL);
     }
     else if( strlen(ctx->uid)> 0  && strlen(ctx->pwd)> 0 )
@@ -352,8 +388,8 @@
     }
 
     /* set callback functions */
-    mosquitto_connect_callback_set(mosq, sub_connect_callback);
-    mosquitto_disconnect_callback_set(mosq, sub_disconnect_callback);
+    mosquitto_connect_callback_set(mosq, mqtt_connect_callback);
+    mosquitto_disconnect_callback_set(mosq, mqtt_disconnect_callback);
     mosquitto_message_callback_set(mosq, sub_message_callback);
 
     while( !g_signal.stop )
@@ -361,16 +397,28 @@
         /* connect to MQTT broker  */
         if( mosquitto_connect(mosq, ctx->host, ctx->port, ctx->keepalive) )
         {
-            log_error("Subscriber connect to broker[%s:%d] failure: %s\n", ctx->host, ctx->port, strerror(errno));
-            msleep(1000);
+            log_error("mosquitto connect to broker[%s:%d] failure: %s\n", ctx->host, ctx->port, strerror(errno));
+            sleep(1);
             continue;
         }
 
-        /* -1: use default timeout 1000ms  1: unused */
-        mosquitto_loop_forever(mosq, -1, 1);
+        while(!g_signal.stop)
+        {
+            /* periodically publish and report data */
+            pub_connect_callback(mosq, ctx, MOSQ_ERR_SUCCESS);
+
+            /* MQTT process in Non-blocking mode, timeout for 1s */
+            if( MOSQ_ERR_SUCCESS != (rv = mosquitto_loop(mosq, 1000, 1)) )
+            {
+                log_warn("MQTT loop error: %s, reconnecting...\n", mosquitto_strerror(rv));
+                break;
+            }
+        }
+
+        mosquitto_disconnect(mosq);
+        sleep(1);
     }
 
     mosquitto_destroy(mosq);
     return NULL;
 }
-

--
Gitblit v1.9.1