| | |
| | | #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"); |
| | |
| | | /* 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 ) |
| | | { |
| | |
| | | log_close(); |
| | | |
| | | return 0; |
| | | } /* ----- End of main() ----- */ |
| | | } |
| | | |
| | | void pub_connect_callback(struct mosquitto *mosq, void *userdata, int result) |
| | | { |
| | |
| | | |
| | | 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 ) |
| | | { |
| | |
| | | return ; |
| | | } |
| | | |
| | | |
| | | void *mqtt_pub_worker(void *args) |
| | | void *thingsboard_publisher(void *args) |
| | | { |
| | | mqtt_ctx_t *ctx = (mqtt_ctx_t *)args; |
| | | struct mosquitto *mosq; |
| | |
| | | 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; |
| | |
| | | /* 只处理 setValue 命令 */ |
| | | if (strcmp(method, "setValue") != 0) |
| | | { |
| | | printf("Ignore method: %s\n", method); |
| | | log_error("Ignore method: %s\n", method); |
| | | return; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | |
| | | return ; |
| | | } |
| | | |
| | | |
| | | void *mqtt_sub_worker(void *args) |
| | | void *thingsboard_subsciber(void *args) |
| | | { |
| | | mqtt_ctx_t *ctx = (mqtt_ctx_t *)args; |
| | | struct mosquitto *mosq; |