| | |
| | | #include <cjson/cJSON.h> |
| | | |
| | | #include "logger.h" |
| | | #include "util_proc.h" |
| | | #include "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_timeout(time_t *last_time, int interval); |
| | | 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"); |
| | |
| | | /* initial gpio hardware */ |
| | | gpio_init(&hwinfo->gpio); |
| | | |
| | | /* |
| | | * +--------------------------------+ |
| | | * | ThingsBoard MQTT Thread | |
| | | * +--------------------------------+ |
| | | */ |
| | | /* 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"); |
| | | |
| | | /* |
| | | * +--------------------------------+ |
| | |
| | | if( !hwinfo->tsl2561 ) |
| | | { |
| | | log_error("TSL2561 is not present, do not turn on the light\n"); |
| | | lux = hwinfo->lux_threshold + 100.0; |
| | | lux = hwinfo->lux_threshold + 100.0; |
| | | goto nextloop; |
| | | } |
| | | |
| | |
| | | if( tsl2561_get_lux(&lux) < 0 ) |
| | | { |
| | | log_error("TSL2561 sample failed, do not turn on the light\n"); |
| | | lux = hwinfo->lux_threshold + 100.0; |
| | | lux = hwinfo->lux_threshold + 100.0; |
| | | goto nextloop; |
| | | } |
| | | |
| | |
| | | * +--------------------------------+ |
| | | */ |
| | | |
| | | int check_timeout(time_t *last_time, int interval) |
| | | { |
| | | int timeout = 0; |
| | | time_t now; |
| | | |
| | | time(&now); |
| | | |
| | | if( difftime(now, *last_time)>interval ) |
| | | { |
| | | timeout = 1; |
| | | *last_time = now; |
| | | } |
| | | |
| | | return timeout; |
| | | } |
| | | |
| | | /* compatible with MQTT publish callback function */ |
| | | /* process publisher(uplink) data */ |
| | | void pub_connect_callback(struct mosquitto *mosq, void *userdata, int result) |
| | | { |
| | | mqtt_ctx_t *ctx = (mqtt_ctx_t *)userdata; |
| | | hwinfo_t *hwinfo = (hwinfo_t *)ctx->userdata; |
| | | 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 */ |
| | |
| | | static time_t last_time = 0; |
| | | |
| | | /* publish time is not arrive */ |
| | | if( !check_timeout(&last_time, ctx->interval) ) |
| | | { |
| | | if( !check_timeout(&last_time, mqtt->interval) ) |
| | | return ; |
| | | } |
| | | |
| | | log_debug("Publish topic '%s'\n", ctx->pubTopic); |
| | | |
| | | if( hwinfo->ds18b20 ) |
| | | log_debug("Publish topic '%s'\n", mqtt->pubTopic); |
| | | if( iotd->hwinfo.ds18b20 ) |
| | | { |
| | | memset(msg, 0, sizeof(msg)); |
| | | |
| | | 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, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain); |
| | | 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); |
| | |
| | | return ; |
| | | } |
| | | |
| | | void sub_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 sub_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); |
| | | } |
| | | |
| | | /* process subscriber(downlink) data */ |
| | | void proc_json_items(cJSON *root, mqtt_ctx_t *ctx) |
| | | { |
| | | cJSON *item_method; |
| | |
| | | return ; |
| | | } |
| | | |
| | | /* process receive message data */ |
| | | proc_json_items(root, ctx); |
| | | |
| | | /* must delete it, or it will result memory leak */ |
| | | cJSON_Delete(root); |
| | | |
| | | return ; |
| | | } |
| | | |
| | | 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) |
| | |
| | | } |
| | | |
| | | /* 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 ) |
| | |
| | | |
| | | while(!g_signal.stop) |
| | | { |
| | | /* periodically publish and report data */ |
| | | pub_connect_callback(mosq, ctx, MOSQ_ERR_SUCCESS); |
| | | |
| | | /* MQTT process in nonblock mode, timeout for 1s */ |
| | | /* 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)); |