| | |
| | | if( hal_ctx->ds18b20_enable ) |
| | | { |
| | | memset(msg, 0, sizeof(msg)); |
| | | |
| | | |
| | | if( ds18b20_get_temperature(&temp) < 0 ) |
| | | snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", mqtt_ctx->id); |
| | | else |
| | |
| | | if( hal_ctx->sht2x_enable ) |
| | | { |
| | | memset(msg, 0, sizeof(msg)); |
| | | |
| | | |
| | | if( sht2x_get_temp_humidity(&temp, &rh) < 0 ) |
| | | snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\", \"RH\":\"error\" }", mqtt_ctx->id); |
| | | else |
| | |
| | | log_err("Subscriber connect to broker server failed, rv=%d\n", result); |
| | | return ; |
| | | } |
| | | |
| | | |
| | | log_nrml("Subscriber connect to broker server[%s:%d] successfully\n", ctx->mqtt_ctx.host, ctx->mqtt_ctx.port); |
| | | |
| | | log_nrml("Subscriber subTopic '%s' with Qos[%d]\n", ctx->mqtt_ctx.subTopic, ctx->mqtt_ctx.subQos); |
| | | mosquitto_subscribe(mosq, NULL, ctx->mqtt_ctx.subTopic, ctx->mqtt_ctx.subQos); |
| | | } |
| | | |
| | |
| | | iotd_ctx_t *ctx = (iotd_ctx_t *)userdata; |
| | | |
| | | log_warn("Subscriber disconnect to broker server[%s:%d], reason=%d\n", |
| | | ctx->mqtt_ctx.host, ctx->mqtt_ctx.port, result); |
| | | ctx->mqtt_ctx.host, ctx->mqtt_ctx.port, result); |
| | | } |
| | | |
| | | void proc_json_items(cJSON *root) |
| | |
| | | { |
| | | value = cJSON_Print(item); |
| | | |
| | | /* light controled by relay */ |
| | | if( !strcasecmp(item->string, "hallway") ) |
| | | log_dbg("JSON Parser key: %s value: %s\n", item->string, value); |
| | | if( strstr(item->string, "light") || strstr(item->string, "led") || strstr(item->string, "relay")) |
| | | { |
| | | if( strcasestr(value, "on") ) |
| | | if( strstr(value, "on") || strstr(value, "off") ) |
| | | { |
| | | //turn_light(LIGHT_HALLWAY, ON); |
| | | log_nrml("Turn on hallway light\n"); |
| | | } |
| | | else if( strcasestr(value, "off") ) |
| | | { |
| | | //turn_light(LIGHT_HALLWAY, OFF); |
| | | log_nrml("Turn off hallway light\n"); |
| | | log_nrml("parser get turn '%s' %s from JSON string\n", item->string, value); |
| | | gpio_out(item->string, value); |
| | | } |
| | | } |
| | | else if( !strcasecmp(item->string, "livroom_left") ) |
| | | { |
| | | if( strcasestr(value, "on") ) |
| | | { |
| | | //turn_light(LIGHT_LVROOM_LEFT, ON); |
| | | log_nrml("Turn on livingroom left light\n"); |
| | | } |
| | | else if( strcasestr(value, "off") ) |
| | | { |
| | | //turn_light(LIGHT_LVROOM_LEFT, OFF); |
| | | log_nrml("Turn off livingroom left light\n"); |
| | | } |
| | | } |
| | | if( !strcasecmp(item->string, "livroom_right") ) |
| | | { |
| | | if( strcasestr(value, "on") ) |
| | | { |
| | | //turn_light(LIGHT_LVROOM_RIGHT, ON); |
| | | log_nrml("Turn on livingroom right light\n"); |
| | | } |
| | | else if( strcasestr(value, "off") ) |
| | | { |
| | | //turn_light(LIGHT_LVROOM_RIGHT, OFF); |
| | | log_nrml("Turn off livingroom right light\n"); |
| | | } |
| | | } |
| | | |
| | | |
| | | free(value); /* must free it, or it will result memory leak */ |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | void sub_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) |
| | |
| | | free(value); |
| | | goto OUT; |
| | | } |
| | | |
| | | |
| | | free(value); |
| | | |
| | | /* proc JSON mesage */ |
| | |
| | | if( !ctx ) |
| | | { |
| | | log_err("Invalid input arguments\n"); |
| | | return NULL; |
| | | return NULL; |
| | | } |
| | | |
| | | mqtt_ctx = &ctx->mqtt_ctx; |