Skip to content

Commit

Permalink
Added LDR sensor (thanks to Altan Altay)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed May 23, 2019
1 parent d9b9e14 commit 868d4d5
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 5 deletions.
1 change: 1 addition & 0 deletions code/espurna/config/arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
//#define GEIGER_SUPPORT 1
//#define GUVAS12SD_SUPPORT 1
//#define HLW8012_SUPPORT 1
//#define LDR_SUPPORT 1
//#define MAX6675_SUPPORT 1
//#define MHZ19_SUPPORT 1
//#define MICS2710_SUPPORT 1
Expand Down
67 changes: 64 additions & 3 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,65 @@
#define LED2_MODE LED_MODE_RELAY
#define LED2_PIN_INVERSE 1

// -----------------------------------------------------------------------------
// PSH
// -----------------------------------------------------------------------------

#elif defined(PSH_WIFI_PLUG)

// Info
#define MANUFACTURER "PSH"
#define DEVICE "WIFI_PLUG"

// Relays
#define RELAY1_PIN 2
#define RELAY1_TYPE RELAY_TYPE_NORMAL

// LEDs
#define LED1_PIN 0
#define LED1_PIN_INVERSE 0

#elif defined(PSH_RGBW_CONTROLLER)

// Info
#define MANUFACTURER "PSH"
#define DEVICE "RGBW_CONTROLLER"
#define RELAY_PROVIDER RELAY_PROVIDER_LIGHT
#define LIGHT_PROVIDER LIGHT_PROVIDER_DIMMER
#define DUMMY_RELAY_COUNT 1

// LEDs
#define LED1_PIN 13
#define LED1_PIN_INVERSE 1

// Light
#define LIGHT_CHANNELS 4
#define LIGHT_CH1_PIN 5 // RED
#define LIGHT_CH2_PIN 4 // GREEN
#define LIGHT_CH3_PIN 12 // BLUE
#define LIGHT_CH4_PIN 14 // WHITE1
#define LIGHT_CH1_INVERSE 0
#define LIGHT_CH2_INVERSE 0
#define LIGHT_CH3_INVERSE 0
#define LIGHT_CH4_INVERSE 0

#elif defined(PSH_WIFI_SENSOR)

// Info
#define MANUFACTURER "PSH"
#define DEVICE "WIFI_SENSOR"

// DHT12 Sensor
#define DHT_SUPPORT 1
#define DHT_PIN 14
#define DHT_TYPE DHT_CHIP_DHT12

// LDR Sensor
#define LDR_SUPPORT 1
#define LDR_TYPE LDR_GL5528
#define LDR_ON_GROUND false
#define LDR_RESISTOR 10000

// -----------------------------------------------------------------------------
// TEST boards (do not use!!)
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -3695,10 +3754,12 @@
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#define MY92XX_MAPPING 4, 3, 5, 0, 1

// A bit of Analog EMON (analog)
#ifndef EMON_ANALOG_SUPPORT
// A bit of analog,
// will not work on real life since they all share GPIO
// but it's OK to test build
#define EMON_ANALOG_SUPPORT 1
#endif
#define NTC_SENSOR 1
#define LDR_SENSOR 1

#define PULSEMETER_SUPPORT 1

Expand Down
3 changes: 3 additions & 0 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ PROGMEM const char espurna_sensors[] =
#if HLW8012_SUPPORT
"HLW8012 "
#endif
#if LDR_SUPPORT
"LDR "
#endif
#if MHZ19_SUPPORT
"MHZ19 "
#endif
Expand Down
46 changes: 46 additions & 0 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,47 @@
// Use FALLING for BL0937 / HJL0
#endif

//------------------------------------------------------------------------------
// LDR sensor
// Enable support by passing LDR_SUPPORT=1 build flag
//------------------------------------------------------------------------------

#ifndef SENSOR_LUX_CORRECTION
#define SENSOR_LUX_CORRECTION 0.0 // Offset correction
#endif

#ifndef LDR_SUPPORT
#define LDR_SUPPORT 0
#endif

#ifndef LDR_SAMPLES
#define LDR_SAMPLES 10 // Number of samples
#endif

#ifndef LDR_DELAY
#define LDR_DELAY 0 // Delay between samples in micros
#endif

#ifndef LDR_TYPE
#define LDR_TYPE LDR_GL5528
#endif

#ifndef LDR_ON_GROUND
#define LDR_ON_GROUND true
#endif

#ifndef LDR_RESISTOR
#define LDR_RESISTOR 10000 // Resistance
#endif

#ifndef LDR_MULTIPLICATION
#define LDR_MULTIPLICATION 32017200
#endif

#ifndef LDR_POWER
#define LDR_POWER 1.5832
#endif

//------------------------------------------------------------------------------
// MHZ19 CO2 sensor
// Enable support by passing MHZ19_SUPPORT=1 build flag
Expand Down Expand Up @@ -916,6 +957,7 @@
GEIGER_SUPPORT || \
GUVAS12SD_SUPPORT || \
HLW8012_SUPPORT || \
LDR_SUPPORT || \
MICS2710_SUPPORT || \
MICS5525_SUPPORT || \
MHZ19_SUPPORT || \
Expand Down Expand Up @@ -1047,6 +1089,10 @@
#include "../sensors/HLW8012Sensor.h"
#endif

#if LDR_SUPPORT
#include "../sensors/LDRSensor.h"
#endif

#if MAX6675_SUPPORT
#include "../sensors/MAX6675Sensor.h"
#endif
Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
#define SENSOR_EZOPH_ID 33
#define SENSOR_BMP180_ID 34
#define SENSOR_MAX6675_ID 35
#define SENSOR_LDR_ID 36

//--------------------------------------------------------------------------------
// Magnitudes
Expand Down
20 changes: 20 additions & 0 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ unsigned char _sensor_energy_units = SENSOR_ENERGY_UNITS;
unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;
double _sensor_humidity_correction = SENSOR_HUMIDITY_CORRECTION;
double _sensor_lux_correction = SENSOR_LUX_CORRECTION;

#if PZEM004T_SUPPORT
PZEM004TSensor *pzem004t_sensor;
Expand Down Expand Up @@ -88,6 +89,10 @@ double _magnitudeProcess(unsigned char type, unsigned char decimals, double valu
value = constrain(value + _sensor_humidity_correction, 0, 100);
}

if (type == MAGNITUDE_LUX) {
value = value + _sensor_lux_correction;
}

if (type == MAGNITUDE_ENERGY ||
type == MAGNITUDE_ENERGY_DELTA) {
if (_sensor_energy_units == ENERGY_KWH) value = value / 3600000;
Expand Down Expand Up @@ -137,6 +142,7 @@ bool _sensorWebSocketOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "tmp", 3) == 0) return true;
if (strncmp(key, "hum", 3) == 0) return true;
if (strncmp(key, "ene", 3) == 0) return true;
if (strncmp(key, "lux", 3) == 0) return true;
return false;
}

Expand Down Expand Up @@ -677,6 +683,19 @@ void _sensorLoad() {
}
#endif

#if LDR_SUPPORT
{
LDRSensor * sensor = new LDRSensor();
sensor->setSamples(LDR_SAMPLES);
sensor->setDelay(LDR_DELAY);
sensor->setType(LDR_TYPE);
sensor->setPhotocellPositionOnGround(LDR_ON_GROUND);
sensor->setResistor(LDR_RESISTOR);
sensor->setPhotocellParameters(LDR_MULTIPLICATION, LDR_POWER);
_sensors.push_back(sensor);
}
#endif

#if MHZ19_SUPPORT
{
MHZ19Sensor * sensor = new MHZ19Sensor();
Expand Down Expand Up @@ -1048,6 +1067,7 @@ void _sensorConfigure() {
_sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
_sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat();
_sensor_energy_reset_ts = getSetting("snsResetTS", "");
_sensor_lux_correction = getSetting("luxCorrection", SENSOR_LUX_CORRECTION).toFloat();

// Specific sensor settings
for (unsigned char i=0; i<_sensors.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/sensors/AnalogSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------

#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT)
#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT || LDR_SENSOR)

#pragma once

Expand Down
Loading

0 comments on commit 868d4d5

Please sign in to comment.