| | |
| | | return -2; |
| | | } |
| | | |
| | | if( hal_init() < 0 ) |
| | | if( ctx.hwconf.enable ) |
| | | { |
| | | if( hal_init(&ctx.hwconf) < 0 ) |
| | | log_err("Initialise hardware failure\n"); |
| | | else |
| | | log_nrml("HAL initialise ok\n"); |
| | | |
| | | return -1; |
| | | } |
| | | log_nrml("HAL initialise ok\n"); |
| | | else |
| | | { |
| | | } |
| | | |
| | | install_proc_signal(); |
| | | |
| | | if( check_set_program_running(daemon) < 0 ) |
| | | goto OUT; |
| | | |
| | | if( !debug ) |
| | | |
| | | mosquitto_lib_init(); |
| | | |
| | |
| | | |
| | | log_nrml("Publisher connect to broker server[%s:%d] successfully\n", ctx->host, ctx->port); |
| | | |
| | | if( ctx->hwconf.ds18b20 ) |
| | | { |
| | | memset(msg, 0, sizeof(msg)); |
| | | |
| | | if( ds18b20_get_temperature(&temp) < 0 ) |
| | | snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id); |
| | | else |
| | |
| | | else |
| | | { |
| | | log_nrml("Publisher broadcast message '%s' ok\n", msg); |
| | | } |
| | | } |
| | | |
| | | log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); |
| | |
| | | turn_led(which, OFF); |
| | | } |
| | | |
| | | void proc_json_items(cJSON *root) |
| | | void proc_json_items(cJSON *root, mqtt_ctx_t *ctx) |
| | | { |
| | | int i; |
| | | char *value; |
| | | cJSON *item; |
| | | cJSON *array; |
| | | hwconf_t *hwconf = &ctx->hwconf; |
| | | |
| | | if( !root ) |
| | | { |
| | |
| | | /* if item is cJSON_Object, then recursive call proc_json */ |
| | | if( cJSON_Object == item->type ) |
| | | { |
| | | proc_json_items(item); |
| | | proc_json_items(item, ctx); |
| | | } |
| | | else if( cJSON_Array == item->type ) |
| | | { |
| | | /* RGB colors led control */ |
| | | if( !strcasecmp(item->string, "led") ) |
| | | if( hwconf->leds && !strcasecmp(item->string, "led") ) |
| | | { |
| | | array = cJSON_GetArrayItem(item, 0); |
| | | if( NULL != array ) |
| | |
| | | value = cJSON_Print(item); |
| | | |
| | | /* light controled by relay */ |
| | | if( !strcasecmp(item->string, "light") ) |
| | | if( hwconf->relay && !strcasecmp(item->string, "light") ) |
| | | { |
| | | if( strcasestr(value, "on") ) |
| | | { |
| | |
| | | } |
| | | |
| | | /* buzzer */ |
| | | if( !strcasecmp(item->string, "buzzer") ) |
| | | if( hwconf->beep && !strcasecmp(item->string, "buzzer") ) |
| | | { |
| | | if( strcasestr(value, "on") ) |
| | | { |
| | |
| | | free(value); |
| | | log_nrml("Subscriber receive message: '%s'\n", message->payload); |
| | | |
| | | proc_json_items(root); |
| | | proc_json_items(root, ctx); |
| | | |
| | | OUT: |
| | | cJSON_Delete(root); /* must delete it, or it will result memory leak */ |