From af30c84d820576c37e57de31bc47d18b6e4b89e1 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 21 Apr 2021 16:52:34 +0800
Subject: [PATCH] update iotd, test MQTT subscriber ok
---
iotd/mqtt/mqtt.c | 58 ++++++++++++++--------------------------------------------
1 files changed, 14 insertions(+), 44 deletions(-)
diff --git a/iotd/mqtt/mqtt.c b/iotd/mqtt/mqtt.c
index 9b70fbc..ed29843 100644
--- a/iotd/mqtt/mqtt.c
+++ b/iotd/mqtt/mqtt.c
@@ -56,7 +56,7 @@
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
@@ -76,7 +76,7 @@
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
@@ -168,8 +168,10 @@
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);
}
@@ -178,7 +180,7 @@
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)
@@ -209,51 +211,19 @@
{
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)
@@ -295,7 +265,7 @@
free(value);
goto OUT;
}
-
+
free(value);
/* proc JSON mesage */
@@ -318,7 +288,7 @@
if( !ctx )
{
log_err("Invalid input arguments\n");
- return NULL;
+ return NULL;
}
mqtt_ctx = &ctx->mqtt_ctx;
--
Gitblit v1.9.1