Browse Source

Added LDR sensor (thanks to Altan Altay)

master
Xose Pérez 5 years ago
parent
commit
868d4d5573
10 changed files with 397 additions and 5 deletions
  1. +1
    -0
      code/espurna/config/arduino.h
  2. +64
    -3
      code/espurna/config/hardware.h
  3. +3
    -0
      code/espurna/config/progmem.h
  4. +46
    -0
      code/espurna/config/sensors.h
  5. +1
    -0
      code/espurna/config/types.h
  6. +20
    -0
      code/espurna/sensor.ino
  7. +1
    -1
      code/espurna/sensors/AnalogSensor.h
  8. +183
    -0
      code/espurna/sensors/LDRSensor.h
  9. +1
    -1
      code/espurna/sensors/NTCSensor.h
  10. +77
    -0
      code/platformio.ini

+ 1
- 0
code/espurna/config/arduino.h View File

@ -191,6 +191,7 @@
//#define GEIGER_SUPPORT 1 //#define GEIGER_SUPPORT 1
//#define GUVAS12SD_SUPPORT 1 //#define GUVAS12SD_SUPPORT 1
//#define HLW8012_SUPPORT 1 //#define HLW8012_SUPPORT 1
//#define LDR_SUPPORT 1
//#define MAX6675_SUPPORT 1 //#define MAX6675_SUPPORT 1
//#define MHZ19_SUPPORT 1 //#define MHZ19_SUPPORT 1
//#define MICS2710_SUPPORT 1 //#define MICS2710_SUPPORT 1


+ 64
- 3
code/espurna/config/hardware.h View File

@ -3506,6 +3506,65 @@
#define LED2_MODE LED_MODE_RELAY #define LED2_MODE LED_MODE_RELAY
#define LED2_PIN_INVERSE 1 #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!!) // TEST boards (do not use!!)
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -3695,10 +3754,12 @@
#define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT
#define MY92XX_MAPPING 4, 3, 5, 0, 1 #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 #define EMON_ANALOG_SUPPORT 1
#endif
#define NTC_SENSOR 1
#define LDR_SENSOR 1
#define PULSEMETER_SUPPORT 1 #define PULSEMETER_SUPPORT 1


+ 3
- 0
code/espurna/config/progmem.h View File

@ -193,6 +193,9 @@ PROGMEM const char espurna_sensors[] =
#if HLW8012_SUPPORT #if HLW8012_SUPPORT
"HLW8012 " "HLW8012 "
#endif #endif
#if LDR_SUPPORT
"LDR "
#endif
#if MHZ19_SUPPORT #if MHZ19_SUPPORT
"MHZ19 " "MHZ19 "
#endif #endif


+ 46
- 0
code/espurna/config/sensors.h View File

@ -491,6 +491,47 @@
// Use FALLING for BL0937 / HJL0 // Use FALLING for BL0937 / HJL0
#endif #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 // MHZ19 CO2 sensor
// Enable support by passing MHZ19_SUPPORT=1 build flag // Enable support by passing MHZ19_SUPPORT=1 build flag
@ -916,6 +957,7 @@
GEIGER_SUPPORT || \ GEIGER_SUPPORT || \
GUVAS12SD_SUPPORT || \ GUVAS12SD_SUPPORT || \
HLW8012_SUPPORT || \ HLW8012_SUPPORT || \
LDR_SUPPORT || \
MICS2710_SUPPORT || \ MICS2710_SUPPORT || \
MICS5525_SUPPORT || \ MICS5525_SUPPORT || \
MHZ19_SUPPORT || \ MHZ19_SUPPORT || \
@ -1047,6 +1089,10 @@
#include "../sensors/HLW8012Sensor.h" #include "../sensors/HLW8012Sensor.h"
#endif #endif
#if LDR_SUPPORT
#include "../sensors/LDRSensor.h"
#endif
#if MAX6675_SUPPORT #if MAX6675_SUPPORT
#include "../sensors/MAX6675Sensor.h" #include "../sensors/MAX6675Sensor.h"
#endif #endif


+ 1
- 0
code/espurna/config/types.h View File

@ -302,6 +302,7 @@
#define SENSOR_EZOPH_ID 33 #define SENSOR_EZOPH_ID 33
#define SENSOR_BMP180_ID 34 #define SENSOR_BMP180_ID 34
#define SENSOR_MAX6675_ID 35 #define SENSOR_MAX6675_ID 35
#define SENSOR_LDR_ID 36
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Magnitudes // Magnitudes


+ 20
- 0
code/espurna/sensor.ino View File

@ -44,6 +44,7 @@ unsigned char _sensor_energy_units = SENSOR_ENERGY_UNITS;
unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS; unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION; double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;
double _sensor_humidity_correction = SENSOR_HUMIDITY_CORRECTION; double _sensor_humidity_correction = SENSOR_HUMIDITY_CORRECTION;
double _sensor_lux_correction = SENSOR_LUX_CORRECTION;
#if PZEM004T_SUPPORT #if PZEM004T_SUPPORT
PZEM004TSensor *pzem004t_sensor; PZEM004TSensor *pzem004t_sensor;
@ -88,6 +89,10 @@ double _magnitudeProcess(unsigned char type, unsigned char decimals, double valu
value = constrain(value + _sensor_humidity_correction, 0, 100); value = constrain(value + _sensor_humidity_correction, 0, 100);
} }
if (type == MAGNITUDE_LUX) {
value = value + _sensor_lux_correction;
}
if (type == MAGNITUDE_ENERGY || if (type == MAGNITUDE_ENERGY ||
type == MAGNITUDE_ENERGY_DELTA) { type == MAGNITUDE_ENERGY_DELTA) {
if (_sensor_energy_units == ENERGY_KWH) value = value / 3600000; if (_sensor_energy_units == ENERGY_KWH) value = value / 3600000;
@ -137,6 +142,7 @@ bool _sensorWebSocketOnReceive(const char * key, JsonVariant& value) {
if (strncmp(key, "tmp", 3) == 0) return true; if (strncmp(key, "tmp", 3) == 0) return true;
if (strncmp(key, "hum", 3) == 0) return true; if (strncmp(key, "hum", 3) == 0) return true;
if (strncmp(key, "ene", 3) == 0) return true; if (strncmp(key, "ene", 3) == 0) return true;
if (strncmp(key, "lux", 3) == 0) return true;
return false; return false;
} }
@ -677,6 +683,19 @@ void _sensorLoad() {
} }
#endif #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 #if MHZ19_SUPPORT
{ {
MHZ19Sensor * sensor = new MHZ19Sensor(); MHZ19Sensor * sensor = new MHZ19Sensor();
@ -1048,6 +1067,7 @@ void _sensorConfigure() {
_sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat(); _sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();
_sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat(); _sensor_humidity_correction = getSetting("humCorrection", SENSOR_HUMIDITY_CORRECTION).toFloat();
_sensor_energy_reset_ts = getSetting("snsResetTS", ""); _sensor_energy_reset_ts = getSetting("snsResetTS", "");
_sensor_lux_correction = getSetting("luxCorrection", SENSOR_LUX_CORRECTION).toFloat();
// Specific sensor settings // Specific sensor settings
for (unsigned char i=0; i<_sensors.size(); i++) { for (unsigned char i=0; i<_sensors.size(); i++) {


+ 1
- 1
code/espurna/sensors/AnalogSensor.h View File

@ -3,7 +3,7 @@
// Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com> // 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 #pragma once


+ 183
- 0
code/espurna/sensors/LDRSensor.h View File

@ -0,0 +1,183 @@
// -----------------------------------------------------------------------------
// LDR Sensor (maps to a LDRSensor)
// Copyright (C) 2019 by Altan Altay
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT && LDR_SUPPORT
#pragma once
// Set ADC to TOUT pin
#undef ADC_MODE_VALUE
#define ADC_MODE_VALUE ADC_TOUT
#include "Arduino.h"
#include "AnalogSensor.h"
#define LDR_GL5516 1
#define LDR_GL5528 2
#define LDR_GL5537_1 3
#define LDR_GL5537_2 4
#define LDR_GL5539 5
#define LDR_GL5549 6
#define LDR_OTHER 99
extern "C" {
#include "../libs/fs_math.h"
}
class LDRSensor : public AnalogSensor {
public:
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
LDRSensor(): AnalogSensor() {
_count = 1;
_sensor_id = SENSOR_LDR_ID;
}
void setType(unsigned char type) {
_type = type;
switch (_type) {
case LDR_GL5516:
_mult_value = 29634400;
_pow_value = 1.6689;
break;
case LDR_GL5537_1:
_mult_value = 32435800;
_pow_value = 1.4899;
break;
case LDR_GL5537_2:
_mult_value = 2801820;
_pow_value = 1.1772;
break;
case LDR_GL5539:
_mult_value = 208510000;
_pow_value = 1.4850;
break;
case LDR_GL5549:
_mult_value = 44682100;
_pow_value = 1.2750;
break;
case LDR_OTHER:
_mult_value = LDR_MULTIPLICATION;
_pow_value = LDR_POWER;
break;
case LDR_GL5528:
default:
_mult_value = 32017200;
_pow_value = 1.5832;
break;
}
}
/*!
* \brief setPhotocellPositionOnGround Configure the photocell as connected to 3.3V or GND
*
* \param on_ground (bool) True if the photocell is connected to GND, else false
*
* True: EXTERNAL ADC
* ^ ^
* _____ | ___/___
* 3.3V |---|_____|--*--|__/____|--| GND
* Other /
* Resistor Photocell
*
* False:
* EXTERNAL ADC
* ^ ^
* _____ | ___/___
* GND |---|_____|--*--|__/____|--| 3.3V
* Other /
* Resistor Photocell
*/
void setPhotocellPositionOnGround(bool on_ground) {
_photocell_on_ground = on_ground;
}
void setResistor(unsigned long resistor) {
_resistor = resistor;
}
/*!
* \brief updatePhotocellParameters Redefine the photocell parameters
*
* \parameter mult_value (float) Multiplication parameter in "I[lux]=mult_value/(R[Ω]^pow_value)" expression
* \parameter pow_value (float) Power parameter in "I[lux]=mult_value/(R[Ω]^pow_value)" expression
*/
void setPhotocellParameters(float mult_value, float pow_value) {
if (_type == LDR_OTHER) {
_mult_value = mult_value;
_pow_value = pow_value;
}
}
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
// Descriptive name of the sensor
String description() {
return String("LDR @ TOUT");
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return description();
}
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String("0");
}
// Type for slot # index
unsigned char type(unsigned char index) {
if (index == 0) return MAGNITUDE_LUX;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
double current_lux = 0;
if (index == 0) {
unsigned long photocell_resistor = 0;
// sampled reading
double read = _read();
float ratio = ((float)1024/(float)read) - 1;
if (_photocell_on_ground) {
photocell_resistor = _resistor / ratio;
} else {
photocell_resistor = _resistor * ratio;
}
current_lux = _mult_value / (float)pow(photocell_resistor, _pow_value);
}
return current_lux;
}
protected:
unsigned char _type = LDR_GL5528;
bool _photocell_on_ground = false;
unsigned long _resistor = 10000;
float _mult_value = 0;
float _pow_value = 0;
};
#endif // SENSOR_SUPPORT && LDR_SUPPORT

+ 1
- 1
code/espurna/sensors/NTCSensor.h View File

@ -67,7 +67,7 @@ class NTCSensor : public AnalogSensor {
// Descriptive name of the slot # index // Descriptive name of the slot # index
String slot(unsigned char index) { String slot(unsigned char index) {
return description(); return description();
};
}
// Address of the sensor (it could be the GPIO or I2C address) // Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) { String address(unsigned char index) {


+ 77
- 0
code/platformio.ini View File

@ -3318,3 +3318,80 @@ upload_flags = ${common.upload_flags}
monitor_speed = ${common.monitor_speed} monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts} extra_scripts = ${common.extra_scripts}
[env:psh-plug]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_1m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_1m0m} -DPSH_PLUG
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
[env:psh-plug-ota]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_1m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_1m0m} -DPSH_PLUG
upload_speed = ${common.upload_speed}
upload_port = ${common.upload_port}
upload_flags = ${common.upload_flags}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
[env:psh-led-controller]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_4m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_4m1m} -DPSH_LED_CONTROLLER
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
[env:psh-led-controller-ota]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_4m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_4m1m} -DPSH_LED_CONTROLLER
upload_speed = ${common.upload_speed}
upload_port = ${common.upload_port}
upload_flags = ${common.upload_flags}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
[env:psh-wifi-sensor]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_4m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_4m1m} -DPSH_WIFI_SENSOR
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}
[env:psh-wifi-sensor-ota]
platform = ${common.platform}
framework = ${common.framework}
board = ${common.board_4m}
board_build.flash_mode = ${common.flash_mode}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
build_flags = ${common.build_flags_4m1m} -DPSH_WIFI_SENSOR
upload_speed = ${common.upload_speed}
upload_port = ${common.upload_port}
upload_flags = ${common.upload_flags}
monitor_speed = ${common.monitor_speed}
extra_scripts = ${common.extra_scripts}

Loading…
Cancel
Save