guowenxue
2019-06-27 45e62dbd9e964c74b24f7fcadad9b418f117e008
update mqttd, add hardware configured in configure files
7 files modified
4 files deleted
402 ■■■■ changed files
mqttd/etc/conf.c 47 ●●●●● patch | view | raw | blame | history
mqttd/etc/conf.h 23 ●●●●● patch | view | raw | blame | history
mqttd/etc/mqttd.conf 27 ●●●●● patch | view | raw | blame | history
mqttd/hal/hal.c 83 ●●●●● patch | view | raw | blame | history
mqttd/hal/hal.h 17 ●●●● patch | view | raw | blame | history
mqttd/hal/led.c 57 ●●●●● patch | view | raw | blame | history
mqttd/hal/led.h 48 ●●●●● patch | view | raw | blame | history
mqttd/hal/relay.c 36 ●●●●● patch | view | raw | blame | history
mqttd/hal/relay.h 31 ●●●●● patch | view | raw | blame | history
mqttd/lylib/proc.c 4 ●●●● patch | view | raw | blame | history
mqttd/main.c 29 ●●●●● patch | view | raw | blame | history
mqttd/etc/conf.c
@@ -101,6 +101,53 @@
    /*+------------------------------------------------------+
     *|      parser hardware module configuration ID         |
     *+------------------------------------------------------+*/
    /* hardware module enable or not */
    ctx->hwconf.enable=iniparser_getint(ini, "hardware:enable", 0);
    log_nrml("Parser hardware modules [%s]\n", ctx->hwconf.enable ? "enable" : "disable");
    /* relay control light  */
    ctx->hwconf.relay=iniparser_getint(ini, "hardware:relay_pin", 0);
    if( !ctx->hwconf.relay )
        log_nrml("Parser relay module disabled\n");
    else
        log_nrml("Parser relay connected wPI #pin nubmer [%d]\n", ctx->hwconf.relay);
    /* buzzer */
    ctx->hwconf.beep=iniparser_getint(ini, "hardware:beep_pin", 0);
    /* RGB 3-colors LED  */
    ctx->hwconf.redled=iniparser_getint(ini, "hardware:red_pin", 0);
    ctx->hwconf.greenled=iniparser_getint(ini, "hardware:green_pin", 0);
    ctx->hwconf.blueled=iniparser_getint(ini, "hardware:blue_pin", 0);
    if( !ctx->hwconf.redled && !ctx->hwconf.greenled && !ctx->hwconf.blueled )
    {
        ctx->hwconf.leds = 0;
        log_nrml("Parser RGB leds module disabled\n");
    }
    else
    {
        ctx->hwconf.leds = 1;
        log_nrml("Parser RGB led connected wPI #pin nubmer [%d,%d,%d]\n",
            ctx->hwconf.redled, ctx->hwconf.greenled, ctx->hwconf.blueled);
    }
    /* temperature sensor ds18b20  */
    ctx->hwconf.ds18b20=iniparser_getint(ini, "hardware:ds18b20", 0);
    log_nrml("Parser temperature sensor DS18B20 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
    /* temperature and hummidity sensor sht2x  */
    ctx->hwconf.sht2x=iniparser_getint(ini, "hardware:sht2x", 0);
    log_nrml("Parser temperature and hummidity sensor sht2x modules [%s]\n", ctx->hwconf.sht2x ? "enable" : "disable");
    /* light intensity sensor TSL2561 */
    ctx->hwconf.lux=iniparser_getint(ini, "hardware:lux", 0);
    log_nrml("Parser light intensity sensor TSL2561 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
    /*+------------------------------------------------------+
     *|               parser production ID                   |
     *+------------------------------------------------------+*/
mqttd/etc/conf.h
@@ -35,10 +35,33 @@
    Qos2, /* Qos2使用两阶段确认来保证消息的不丢失和不重复。在Qos2情况下,Broker肯定会收到消息,且只收到一次  */
};
typedef struct hwconf_s
{
    unsigned char  enable;      /* Hardware enable or not, help to running on X86 */
    int            relay;       /* relay connected wPI #pin number, if set to 0 means disable */
    int            beep;        /* beep connected wPI #pin number, if set to 0 means disable */
    unsigned char  leds;
    int            redled;     /*  redled connect wPI #pin number, if set to 0 means disable */
    int            greenled;   /*greenled connect wPI #pin number, if set to 0 means disable */
    int            blueled;    /* blueled connect wPI #pin number, if set to 0 means disable */
    unsigned char  ds18b20;    /* enable or disable temperature sensor ds18b20 */
    unsigned char  sht2x;      /* enable or disable temperature and hummidity sensor sht2x */
    unsigned char  lux;        /* enable or disable light intensity sensor TSL2561 */
} hwconf_t;
typedef struct mqtt_ctx_s
{
    char          id[32];       /*  production ID */
    /* hardware configuration */
    hwconf_t      hwconf;
    /* logger settings */
    char          logfile[128]; /* logger record file */
    int           loglevel;     /* logger level  */
mqttd/etc/mqttd.conf
@@ -1,6 +1,33 @@
[common]
id="rpi3b001"
[hardware]
# Settings 0:sisable  others: enable or connected #PIN nubmer(wPI)
# Enablue hardware support or not, help to running on X86
enable=1
# relay connected wPI #pin number, if set to 0 means disable
relay_pin=29
# beep connected wPI #pin number, if set to 0 means disable
beep_pin=0
# RGB 3-colors LED connect wPI #pin number, if all set to 0 means disable
red_pin=0
green_pin=0
blue_pin=0
# temperature sensor ds18b20
ds18b20=1
# temperature and hummidity sensor sht2x
sht2x=0
# light intensity sensor TSL2561
lux=0
[logger]
# 日志记录文件
mqttd/hal/hal.c
@@ -13,21 +13,90 @@
#include "hal.h"
int hal_init(void)
                              /* LED_R  LED_G   LED_B */
static int led_pin[LED_MAX] = { 0,0,0 };
static int relay_pin = 0;
void init_relay(int pin);
void init_led(int redpin, int greenpin, int bluepin);
int hal_init(hwconf_t *hwconf)
{
    if( !hwconf->enable )
    {
        log_err("All hardware modules are disabled, skip initialise HAL.\n");
        return 0;
    }
    wiringPiSetup();
    init_led();
    init_relay();
    if( hwconf->leds )
        init_led(hwconf->redled, hwconf->greenled, hwconf->blueled);
#if 0
    if( sht2x_init() < 0 )
    if(hwconf->relay)
        init_relay(hwconf->relay);
    if( hwconf->sht2x && sht2x_init() < 0 )
    {
        log_err("Initialise SHT20 failure\n");
        return -2;
        return -1;
    } 
#endif
    
    return 0;
}
void init_led(int redpin, int greenpin, int bluepin)
{
    int         i;
    led_pin[LED_R]=redpin;
    led_pin[LED_G]=greenpin;
    led_pin[LED_B]=bluepin;
    for(i=0; i<LED_MAX; i++)
    {
        if(led_pin[i])
            pinMode(led_pin[i], OUTPUT );
    }
}
int turn_led(int which, int cmd)
{
    if( which<0 || which>=LED_MAX )
        return -1;
    if( OFF == cmd )
    {
        if(led_pin[which])
            digitalWrite (led_pin[which], LOW);
    }
    else
    {
        if(led_pin[which])
            digitalWrite (led_pin[which], HIGH);
    }
    return 0;
}
void init_relay(int pin)
{
    relay_pin = pin;
    pinMode(relay_pin, OUTPUT);
}
void turn_relay(int cmd)
{
    if( OFF == cmd )
    {
        digitalWrite ( relay_pin, LOW );
    }
    else
    {
        digitalWrite ( relay_pin, HIGH );
    }
}
mqttd/hal/hal.h
@@ -17,12 +17,23 @@
#include <wiringPi.h>
#include "lylib/logger.h"
#include "etc/conf.h"
#include "led.h"
#include "relay.h"
#include "ds18b20.h"
#include "sht20.h"
int hal_init(void);
#define OFF   0
#define ON    1
/* Three LEDs code */
enum
{
    LED_R = 0,
    LED_G,
    LED_B,
    LED_MAX,
};
int hal_init(hwconf_t *conf);
#endif   /* ----- #ifndef _HAL_H_  ----- */
mqttd/hal/led.c
File was deleted
mqttd/hal/led.h
File was deleted
mqttd/hal/relay.c
File was deleted
mqttd/hal/relay.h
File was deleted
mqttd/lylib/proc.c
@@ -234,7 +234,7 @@
    if (0 == retVal) 
    { 
        pid_t pid = -1; 
        printf("PID record file \"%s\" exist.\n", pid_file);
        //printf("PID record file \"%s\" exist.\n", pid_file);
        pid = get_daemon_pid(pid_file);
        if (pid > 0)  /*  Process pid exist */
@@ -246,7 +246,7 @@
            } 
            else   /* Send signal to the old process get no reply. */ 
            { 
                printf("Program with PID[%d] seems exit.\n", pid);
                printf("Program with PID[%d] already exit.\n", pid);
                remove(pid_file); 
                return 0; 
            } 
mqttd/main.c
@@ -109,19 +109,23 @@
        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();
@@ -200,7 +204,10 @@
    
    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 
@@ -214,6 +221,7 @@
    else 
    {
        log_nrml("Publisher broadcast message '%s' ok\n", msg);
        }
    }
    log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); 
@@ -294,12 +302,13 @@
        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 )
    {
@@ -316,12 +325,12 @@
        /* 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 )
@@ -353,7 +362,7 @@
            value = cJSON_Print(item);
            /* light controled by relay */
            if( !strcasecmp(item->string, "light") )
            if( hwconf->relay && !strcasecmp(item->string, "light") )
            {
                if( strcasestr(value, "on") )
                {
@@ -368,7 +377,7 @@
            }
            /* buzzer */
            if( !strcasecmp(item->string, "buzzer") )
            if( hwconf->beep && !strcasecmp(item->string, "buzzer") )
            {
                if( strcasestr(value, "on") )
                {
@@ -427,7 +436,7 @@
    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 */