From 4deb51ffc4dd4588183aed82bd7877909ba2130e Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 31 Jul 2019 11:19:22 +0800
Subject: [PATCH] update mqtt configure file, support breakin detect support

---
 mqttd/etc/conf.h     |   26 +++++++-
 mqttd/hal/hal.c      |   29 ++++++++-
 mqttd/etc/conf.c     |   55 +++++++++++++++---
 mqttd/hal/hal.h      |    2 
 mqttd/etc/mqttd.conf |   28 +++++----
 mqttd/main.c         |   10 +--
 6 files changed, 112 insertions(+), 38 deletions(-)

diff --git a/mqttd/etc/conf.c b/mqttd/etc/conf.c
index f998ce4..4afe90b 100644
--- a/mqttd/etc/conf.c
+++ b/mqttd/etc/conf.c
@@ -109,20 +109,26 @@
     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 )
+    ctx->hwconf.relay_pin=iniparser_getint(ini, "hardware:relay_pin", 0);
+    if( !ctx->hwconf.relay_pin )
         log_nrml("Parser relay module disabled\n");
     else
-        log_nrml("Parser relay connected wPI #pin nubmer [%d]\n", ctx->hwconf.relay); 
+        log_nrml("Parser relay connected wPI #pin number [%d]\n", ctx->hwconf.relay_pin); 
 
     /* buzzer */
-    ctx->hwconf.beep=iniparser_getint(ini, "hardware:beep_pin", 0);
+    ctx->hwconf.beep_pin=iniparser_getint(ini, "hardware:beep_pin", 0); 
+    log_nrml("Parser buzzer connected wPI #pin number [%d]\n", ctx->hwconf.beep_pin); 
+
+    /* Beep max. times set beep 10 */ 
+    ctx->hwconf.beep_times = iniparser_getint(ini, "hardware:beep_times", 0); 
+    log_nrml("Parser inbreak buzzer beep times [%d]\n", ctx->hwconf.beep_times);
+
 
     /* 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.red_pin=iniparser_getint(ini, "hardware:red_pin", 0);
+    ctx->hwconf.green_pin=iniparser_getint(ini, "hardware:green_pin", 0);
+    ctx->hwconf.blue_pin=iniparser_getint(ini, "hardware:blue_pin", 0);
+    if( !ctx->hwconf.red_pin && !ctx->hwconf.green_pin && !ctx->hwconf.blue_pin )
     {
         ctx->hwconf.leds = 0;
         log_nrml("Parser RGB leds module disabled\n");
@@ -130,8 +136,8 @@
     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);
+        log_nrml("Parser RGB led connected wPI #pin number [%d,%d,%d]\n", 
+            ctx->hwconf.red_pin, ctx->hwconf.green_pin, ctx->hwconf.blue_pin);
     }
 
     /* temperature sensor ds18b20  */
@@ -146,6 +152,35 @@
     ctx->hwconf.lux=iniparser_getint(ini, "hardware:lux", 0);
     log_nrml("Parser light intensity sensor TSL2561 modules [%s]\n", ctx->hwconf.ds18b20 ? "enable" : "disable");
 
+    ctx->hwconf.infrared_pin=iniparser_getint(ini, "hardware:infrared_pin", 0);
+    log_nrml("Parser infrared pin connected wPI #pin number [%d]\n", ctx->hwconf.infrared_pin);
+
+    if( ctx->hwconf.infrared_pin )
+    {
+        ctx->hwconf.inbreak_beep = iniparser_getint(ini, "hardware:inbreak_beep", 0); 
+        log_nrml("Parser %s inbreak beep\n", ctx->hwconf.inbreak_beep? "enable" : "disable");
+        if( !ctx->hwconf.beep_pin ) 
+        {
+            log_warn("WARNNING: beep hardware not enable and will disable inbreak beep\n");
+            ctx->hwconf.inbreak_beep = 0;
+        }
+
+        ctx->hwconf.fillin_light = iniparser_getint(ini, "hardware:fillin_light", 0);
+        log_nrml("Parser inbreak fill-in light mode [%d]\n", ctx->hwconf.fillin_light);
+
+        if( !ctx->hwconf.relay_pin )
+        {
+            log_warn("WARNNING: Relay hardware not enable and change fill-in light mode to OFF\n");
+            ctx->hwconf.fillin_light = FILLIN_LIGHT_OFF;
+        }
+
+        if( !ctx->hwconf.lux )
+        {
+            log_warn("WARNNING: LUX sensor hardware not enable and change fill-in light mode to ON\n");
+            ctx->hwconf.fillin_light = FILLIN_LIGHT_ON;
+        }
+    }
+
 
     /*+------------------------------------------------------+
      *|               parser production ID                   |
diff --git a/mqttd/etc/conf.h b/mqttd/etc/conf.h
index fb26bac..8a9c871 100644
--- a/mqttd/etc/conf.h
+++ b/mqttd/etc/conf.h
@@ -35,22 +35,38 @@
     Qos2, /* Qos2使用两阶段确认来保证消息的不丢失和不重复。在Qos2情况下,Broker肯定会收到消息,且只收到一次  */
 };
 
+#define LUX_TRIGGLE_VAL            0.01
+enum
+{
+    FILLIN_LIGHT_OFF=0,  /* disable */
+    FILLIN_LIGHT_ON,     /* enable  */
+    FILLIN_LIGHT_AUTO,   /* auto fill-in light based on LUX sensor detected value */
+};
+
 
 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 */
+    int            relay_pin;   /* relay connected wPI #pin number, if set to 0 means disable */
+    int            beep_pin;    /* beep connected wPI #pin number, if set to 0 means disable */
+    int            beep_times;  /* default beep count when beep on */
 
     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 */
+    int            red_pin;     /*  redled connect wPI #pin number, if set to 0 means disable */
+    int            green_pin;   /*greenled connect wPI #pin number, if set to 0 means disable */
+    int            blue_pin;    /* 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 */
+
+    /* Infrared detect inbreank  */
+    unsigned char  infrared_pin; /* enable or disable inbreak detected */
+    void (*ifrd_handler)(void);  /* infrared detected interrupt handler */
+    unsigned char  inbreak_beep; /* enable or diable beep when detect inbreak */
+    unsigned char  fillin_light; /* fill-in light when detect inbrank or not */
+
 } hwconf_t;  
 
 
diff --git a/mqttd/etc/mqttd.conf b/mqttd/etc/mqttd.conf
index 4f1fe8a..0436a3b 100644
--- a/mqttd/etc/mqttd.conf
+++ b/mqttd/etc/mqttd.conf
@@ -1,18 +1,8 @@
 [common]
 id="RPi.3B-GP-0001"
 
-[inbreak]
-# 入侵探测是否使能,如果hardware里面的红外没有设置,则即使设置了也不能生效
-enable=1
-
-# 如果探测到人进来了是否响蜂鸣器: 0: 不响  >0: 蜂鸣器响的次数
-beep=1
-
-# 如果探测到人来了是否通过继电器控制灯泡, 0:不补光 1:补光 2:根据光强传感器的信息自动补光,如果光强传感器不在则补光
-fillin_light=1
-
-[hardware]
 # 树莓派连接的外设信息,0:禁用或未连接  其他: 使能或相关硬件连接的Pin管脚(wPI模式)
+[hardware]
 
 # 是否使能硬件支持,如果是在X86上调试该程序,可以把该选项设置为0 
 enable=1
@@ -22,6 +12,9 @@
 
 # 控制蜂鸣器,0: 禁用   !0:蜂鸣器连接树莓派的wPI管脚编号
 beep_pin=0
+
+# 蜂鸣器响的次数: 0: 不响  >0: 蜂鸣器响的次数
+beep_times=2
 
 # RGB三色LED, 全0:禁用   !0: 三色LED相关引脚连接树莓派的wPI管脚编号
 red_pin=0
@@ -35,7 +28,16 @@
 sht2x=0
 
 # 是否使能 TSL2561 光强传感器模块,0:禁用  1:使能
-lux=0
+lux=1
+
+# 是否使能 红外 探测入侵功能, 0: 禁用 !0: 红外模块连接到树莓派的wPI管脚编号
+infrared_pin=28
+
+# 探测到人来了是否响蜂鸣器, 0: 禁用 1:使能
+inbreak_beep=1
+
+# 如果探测到人来了是否通过继电器控制灯泡, 0:不补光 1:补光 2:根据光强传感器的信息自动补光,如果光强传感器不在则补光
+fillin_light=2
 
 
 [logger]
@@ -75,7 +77,7 @@
 pubQos=0
 
 # Publisher上报传感器数据的周期,单位是秒
-interval=300
+interval=60
 
 [subsciber]
 
diff --git a/mqttd/hal/hal.c b/mqttd/hal/hal.c
index 94dd8dc..4840944 100644
--- a/mqttd/hal/hal.c
+++ b/mqttd/hal/hal.c
@@ -16,9 +16,12 @@
                               /* LED_R  LED_G   LED_B */
 static int led_pin[LED_MAX] = { 0,0,0 };
 static int relay_pin = 0;
+static int beep_pin = 0;
+static int infrared_pin = 0;
 
 void init_relay(int pin);
 void init_led(int redpin, int greenpin, int bluepin);
+void init_beep(int pin);
 
 int hal_init(hwconf_t *hwconf)
 {
@@ -31,10 +34,16 @@
     wiringPiSetup();
 
     if( hwconf->leds )
-        init_led(hwconf->redled, hwconf->greenled, hwconf->blueled); 
+        init_led(hwconf->red_pin, hwconf->green_pin, hwconf->blue_pin); 
 
-    if(hwconf->relay)
-        init_relay(hwconf->relay);
+    if( hwconf->relay_pin )
+        init_relay(hwconf->relay_pin);
+
+    if( hwconf->beep_pin )
+        init_beep(hwconf->beep_pin);
+
+    if( hwconf->infrared_pin && hwconf->ifrd_handler )
+        init_infrared(hwconf->infrared_pin, INT_EDGE_RISING, hwconf->ifrd_handler);
 
     if( hwconf->sht2x && sht2x_init() < 0 )
     {
@@ -86,6 +95,20 @@
     pinMode(relay_pin, OUTPUT);
 }
 
+void init_beep(int pin)
+{
+    beep_pin = pin;
+    //pinMode(relay_pin, OUTPUT);
+}
+
+void init_infrared(int pin, int irq_type, void (*handler)(void))
+{
+    pinMode(pin, INPUT);
+
+    delay(100);
+
+    wiringPiISR(pin, irq_type, handler);
+}
 
 void turn_relay(int cmd)
 {
diff --git a/mqttd/hal/hal.h b/mqttd/hal/hal.h
index 775885b..027a078 100644
--- a/mqttd/hal/hal.h
+++ b/mqttd/hal/hal.h
@@ -37,5 +37,7 @@
 extern int hal_init(hwconf_t *conf);
 extern void turn_relay(int cmd);
 extern int turn_led(int which, int cmd);
+extern void init_infrared(int pin, int irq_type, void (*handler)(void));
+extern void turn_beep(int times);
 
 #endif   /* ----- #ifndef _HAL_H_  ----- */
diff --git a/mqttd/main.c b/mqttd/main.c
index 2c99317..be26876 100644
--- a/mqttd/main.c
+++ b/mqttd/main.c
@@ -359,7 +359,7 @@
             value = cJSON_Print(item);
 
             /* light controled by relay */
-            if( hwconf->relay && !strcasecmp(item->string, "light") )
+            if( hwconf->relay_pin && !strcasecmp(item->string, "light") )
             {
                 if( strcasestr(value, "on") )
                 {
@@ -374,15 +374,11 @@
             }
 
             /* buzzer */
-            if( hwconf->beep && !strcasecmp(item->string, "buzzer") )
+            if( hwconf->beep_pin && !strcasecmp(item->string, "buzzer") )
             {
                 if( strcasestr(value, "on") )
                 {
-                    log_nrml("Turn buzzer on\n");
-                }
-                else if( strcasestr(value, "music") )
-                {
-                    log_nrml("Turn buzzer play music\n");
+                    log_nrml("Turn buzzer on [%d] times\n", hwconf->beep_times);
                 }
             }
 

--
Gitblit v1.9.1