From 3ef6cf39749c00366b12aef9f023aac3bda16f2d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 17 Nov 2025 23:39:25 +0800
Subject: [PATCH] update modules for tsl2561 and ds18b20

---
 project/thingsboard/thingsboard.c |   55 +++++++++++++++++++++++++++++++------------------------
 1 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/project/thingsboard/thingsboard.c b/project/thingsboard/thingsboard.c
index c2f443f..70c872a 100644
--- a/project/thingsboard/thingsboard.c
+++ b/project/thingsboard/thingsboard.c
@@ -31,14 +31,13 @@
 #define PROG_VERSION               "v1.0.0"
 #define DAEMON_PIDFILE             "/tmp/.thingsboard.pid"
 
-void *mqtt_sub_worker(void *args);
-void *mqtt_pub_worker(void *args);
+void *thingsboard_subsciber(void *args);
+void *thingsboard_publisher(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 thingsboard client 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");
@@ -120,21 +119,30 @@
     /* initial mosquitto library */
     mosquitto_lib_init();
 
-    /* create MQTT subsciber thread  */
-    if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 )
+    /*
+     * +--------------------------------+
+     * |    MQTT Subscriber Thread      |
+     * +--------------------------------+
+     */
+    if( thread_start(&tid, thingsboard_subsciber, &ctx ) < 0 )
     {
         log_error("Start MQTT subsciber worker thread failure\n");
         goto cleanup;
     }
     log_info("Start MQTT subsciber worker thread ok\n");
 
-    /* create MQTT publisher thread  */
-    if( thread_start(&tid, mqtt_pub_worker, &ctx) < 0 )
+    /*
+     * +--------------------------------+
+     * |     MQTT publisher Thread      |
+     * +--------------------------------+
+     */
+    if( thread_start(&tid, thingsboard_publisher, &ctx) < 0 )
     {
         log_error("Start MQTT publisher worker thread failure\n");
         goto cleanup;
     }
     log_info("Start MQTT publisher worker thread ok\n");
+
 
     while( ! g_signal.stop )
     {
@@ -146,7 +154,7 @@
     log_close();
 
     return 0;
-} /* ----- End of main() ----- */
+}
 
 void pub_connect_callback(struct mosquitto *mosq, void *userdata, int result)
 {
@@ -172,9 +180,10 @@
 
         log_debug("SHT2X temperature and humidity sensor enabled, start broadcast it\n");
 
-        if( 0 == sht2x_get_temp_humidity(&temp, &rh) )
-            snprintf(msg, sizeof(msg), "{\"temperature\":%.2f, \"humidity\":%.2f}", temp, rh);
+        if( sht2x_get_temp_humidity(&temp, &rh)<0 )
+            return ;
 
+        snprintf(msg, sizeof(msg), "{\"temperature\":%.2f, \"humidity\":%.2f}", temp, rh);
         rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain);
         if( rv )
         {
@@ -193,8 +202,7 @@
     return ;
 }
 
-
-void *mqtt_pub_worker(void *args)
+void *thingsboard_publisher(void *args)
 {
     mqtt_ctx_t             *ctx = (mqtt_ctx_t *)args;
     struct mosquitto       *mosq;
@@ -279,7 +287,7 @@
     item_method = cJSON_GetObjectItem(root, "method");
     if (item_method == NULL || !cJSON_IsString(item_method))
     {
-        printf("JSON missing method\n");
+        log_error("JSON missing method\n");
         return;
     }
     method = item_method->valuestring;
@@ -287,7 +295,7 @@
     /* 只处理 setValue 命令 */
     if (strcmp(method, "setValue") != 0)
     {
-        printf("Ignore method: %s\n", method);
+        log_error("Ignore method: %s\n", method);
         return;
     }
 
@@ -295,7 +303,7 @@
     item_params = cJSON_GetObjectItem(root, "params");
     if (item_params == NULL || !cJSON_IsObject(item_params))
     {
-        printf("JSON missing params\n");
+        log_error("JSON missing params\n");
         return;
     }
 
@@ -303,7 +311,7 @@
     item_device = cJSON_GetObjectItem(item_params, "device");
     if (item_device == NULL || !cJSON_IsString(item_device))
     {
-        printf("JSON missing params.device\n");
+        log_error("JSON missing params.device\n");
         return;
     }
     device = item_device->valuestring;
@@ -312,21 +320,21 @@
     item_status = cJSON_GetObjectItem(item_params, "status");
     if (item_status == NULL || !(cJSON_IsBool(item_status)))
     {
-        printf("JSON missing params.status\n");
+        log_error("JSON missing params.status\n");
         return;
     }
     status = cJSON_IsTrue(item_status) ? ON : OFF;
 
     /* 映射 LED 名称并调用控制函数 */
-    if (strcmp(device, "RedLed") == 0)
+    if ( !strcmp(device, "RedLed") )
     {
         turn_led(LED_R, status);
     }
-    else if (strcmp(device, "GreenLed") == 0)
+    else if ( !strcmp(device, "GreenLed") )
     {
         turn_led(LED_G, status);
     }
-    else if (strcmp(device, "BlueLed") == 0)
+    else if ( !strcmp(device, "BlueLed") )
     {
         turn_led(LED_B, status);
     }
@@ -346,7 +354,7 @@
         return ;
     }
 
-    log_debug("Subscriber receive message: '%s'\n", message->payload);
+    log_info("Subscriber receive message: '%s'\n", message->payload);
 
     root = cJSON_Parse(message->payload);
     if( !root )
@@ -363,8 +371,7 @@
     return ;
 }
 
-
-void *mqtt_sub_worker(void *args)
+void *thingsboard_subsciber(void *args)
 {
     mqtt_ctx_t             *ctx = (mqtt_ctx_t *)args;
     struct mosquitto       *mosq;

--
Gitblit v1.9.1