From a903b916f09419e7324a2352232e601acc598d67 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Thu, 28 Jan 2021 17:13:32 +0800
Subject: [PATCH] update lightd main.c, add lux sensor support and test ok
---
lightd/hal/hal.c | 1 -
lightd/etc/lightd.conf | 2 +-
lightd/hal/tsl2561.c | 3 +--
lightd/hal/hal.h | 2 ++
lightd/main.c | 24 +++++++++++++++++++-----
5 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/lightd/etc/lightd.conf b/lightd/etc/lightd.conf
index 5747e7d..9c8341a 100644
--- a/lightd/etc/lightd.conf
+++ b/lightd/etc/lightd.conf
@@ -16,7 +16,7 @@
light_intval=15
# 光强传感器采样值低于该阈值将开灯
-lux_threshold=0.02
+lux_threshold=0.10
[logger]
diff --git a/lightd/hal/hal.c b/lightd/hal/hal.c
index 4428bb6..e7cb1d8 100644
--- a/lightd/hal/hal.c
+++ b/lightd/hal/hal.c
@@ -12,7 +12,6 @@
********************************************************************************/
#include "lylib/logger.h"
-#include "tsl2561.h"
#include "hal.h"
#define RPI_GPIONAME "gpiochip0"
diff --git a/lightd/hal/hal.h b/lightd/hal/hal.h
index 9b657bb..6319f0c 100644
--- a/lightd/hal/hal.h
+++ b/lightd/hal/hal.h
@@ -16,6 +16,8 @@
#include <gpiod.h>
+#include "tsl2561.h"
+
#define OFF 0
#define ON 1
diff --git a/lightd/hal/tsl2561.c b/lightd/hal/tsl2561.c
index 3aaf087..341eefa 100644
--- a/lightd/hal/tsl2561.c
+++ b/lightd/hal/tsl2561.c
@@ -12,8 +12,6 @@
*
********************************************************************************/
-#include <wiringPi.h>
-#include <wiringPiI2C.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -187,6 +185,7 @@
OUT:
tsl2561_power(OFF);
+ return lux;
}
diff --git a/lightd/main.c b/lightd/main.c
index 03aee1a..db7e6b5 100644
--- a/lightd/main.c
+++ b/lightd/main.c
@@ -16,6 +16,7 @@
#include <getopt.h>
#include <libgen.h>
#include <string.h>
+#include <math.h>
#include <cjson/cJSON.h>
#include <mosquitto.h>
@@ -67,7 +68,7 @@
int debug = 0;
int opt;
char *progname=NULL;
- double lux = 0.0;
+ float lux = 0.0;
struct option long_options[] = {
{"conf", required_argument, NULL, 'c'},
@@ -146,12 +147,25 @@
log_nrml("Start infrared monitor thread working...\n");
while( ! g_signal.stop )
- {
+ {
+ lux = tsl2561_get_lux();
+ if( lux > hal_ctx->lux_threshold )
+ {
+ log_nrml("Lux[%.3f] > Treshold[%.3f], don't need auto light.\n", lux, hal_ctx->lux_threshold);
+ sleep(30);
+ continue;
+ }
+
+ log_nrml("start infrared monitor detect...\n");
if( infrared_detect() )
{
log_nrml("Someone incoming detected by infrared\n");
- turn_light(LIGHT_HALLWAY, ON);
- alarm(hal_ctx->light_intval);
+ if( lux < hal_ctx->lux_threshold )
+ {
+ log_nrml("Lux[%.3f] < Treshold[%.3f], auto light on now..\n", lux, hal_ctx->lux_threshold);
+ turn_light(LIGHT_HALLWAY, ON);
+ alarm(hal_ctx->light_intval);
+ }
}
}
@@ -280,7 +294,7 @@
else if( strcasestr(value, "off") )
{
turn_light(LIGHT_LVROOM_RIGHT, OFF);
- log_nrml("Turn on livingroom right light\n");
+ log_nrml("Turn off livingroom right light\n");
}
}
--
Gitblit v1.9.1