guowenxue
2021-04-21 af30c84d820576c37e57de31bc47d18b6e4b89e1
update iotd, test MQTT subscriber ok
3 files modified
58 ■■■■ changed files
iotd/hal/gpio.c 6 ●●●● patch | view | raw | blame | history
iotd/hal/gpio.h 8 ●●●● patch | view | raw | blame | history
iotd/mqtt/mqtt.c 44 ●●●● patch | view | raw | blame | history
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);
    }
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);
iotd/mqtt/mqtt.c
@@ -170,6 +170,8 @@
    }
    
    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);
}
@@ -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");
                }
            }
            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");
                    log_nrml("parser get turn '%s' %s from JSON string\n", item->string, value);
                    gpio_out(item->string, value);
                }
            }
            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)