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 +++++++----------------------
iotd/hal/gpio.c | 6 +-
iotd/hal/gpio.h | 8 ---
3 files changed, 18 insertions(+), 54 deletions(-)
diff --git a/iotd/hal/gpio.c b/iotd/hal/gpio.c
index bd050b1..a5532a5 100644
--- a/iotd/hal/gpio.c
+++ b/iotd/hal/gpio.c
@@ -117,7 +117,7 @@
gpiod_chip_close(s_chip);
}
-void gpio_out(char *name, int cmd)
+void gpio_out(char *name, char *cmd)
{
int i;
int found = 0;
@@ -137,11 +137,11 @@
return ;
}
- if( OFF == cmd )
+ if( strstr(cmd, "on") )
{
gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level);
}
- else
+ else if( strstr(cmd, "off") )
{
gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level);
}
diff --git a/iotd/hal/gpio.h b/iotd/hal/gpio.h
index 2a12d84..455960f 100644
--- a/iotd/hal/gpio.h
+++ b/iotd/hal/gpio.h
@@ -16,9 +16,6 @@
#include <gpiod.h>
-#define OFF 0
-#define ON 1
-
#define GPIO_MAXOUT 8
#define GPIO_MAXIN 4
@@ -46,10 +43,7 @@
/* turn which light on/off */
-extern void turn_light(int which, int cmd);
-
-/* turn which led on/off */
-extern void turn_led(int which, int cmd);
+void gpio_out(char *name, char *cmd);
/* Return value: 0(LOW): Nobody detected, !0: Which infrared detect incoming */
extern int infrared_detect(void);
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