From 868d4d5573cabb33b3d50f88f7ff4ac2dd5b56db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 23 May 2019 08:52:04 +0200 Subject: [PATCH] Added LDR sensor (thanks to Altan Altay) --- code/espurna/config/arduino.h | 1 + code/espurna/config/hardware.h | 67 +++++++++- code/espurna/config/progmem.h | 3 + code/espurna/config/sensors.h | 46 +++++++ code/espurna/config/types.h | 1 + code/espurna/sensor.ino | 20 +++ code/espurna/sensors/AnalogSensor.h | 2 +- code/espurna/sensors/LDRSensor.h | 183 ++++++++++++++++++++++++++++ code/espurna/sensors/NTCSensor.h | 2 +- code/platformio.ini | 77 ++++++++++++ 10 files changed, 397 insertions(+), 5 deletions(-) create mode 100644 code/espurna/sensors/LDRSensor.h diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 0a6c255d..9a432d5b 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -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 diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 557f472c..0c075d5a 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -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!!) // ----------------------------------------------------------------------------- @@ -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 diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h index 70c75bba..5f8649ab 100644 --- a/code/espurna/config/progmem.h +++ b/code/espurna/config/progmem.h @@ -193,6 +193,9 @@ PROGMEM const char espurna_sensors[] = #if HLW8012_SUPPORT "HLW8012 " #endif + #if LDR_SUPPORT + "LDR " + #endif #if MHZ19_SUPPORT "MHZ19 " #endif diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index d1e28b0c..24cd9929 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -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 @@ -916,6 +957,7 @@ GEIGER_SUPPORT || \ GUVAS12SD_SUPPORT || \ HLW8012_SUPPORT || \ + LDR_SUPPORT || \ MICS2710_SUPPORT || \ MICS5525_SUPPORT || \ MHZ19_SUPPORT || \ @@ -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 diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 8e89277b..62381260 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -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 diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 6bf718c1..45fb55c2 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -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; @@ -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; @@ -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; } @@ -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(); @@ -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++) { diff --git a/code/espurna/sensors/AnalogSensor.h b/code/espurna/sensors/AnalogSensor.h index 31ae6daa..e58363d9 100644 --- a/code/espurna/sensors/AnalogSensor.h +++ b/code/espurna/sensors/AnalogSensor.h @@ -3,7 +3,7 @@ // Copyright (C) 2017-2019 by Xose Pérez // ----------------------------------------------------------------------------- -#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT) +#if SENSOR_SUPPORT && (ANALOG_SUPPORT || NTC_SUPPORT || LDR_SENSOR) #pragma once diff --git a/code/espurna/sensors/LDRSensor.h b/code/espurna/sensors/LDRSensor.h new file mode 100644 index 00000000..2404ae28 --- /dev/null +++ b/code/espurna/sensors/LDRSensor.h @@ -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 diff --git a/code/espurna/sensors/NTCSensor.h b/code/espurna/sensors/NTCSensor.h index c3239423..c643f0d7 100644 --- a/code/espurna/sensors/NTCSensor.h +++ b/code/espurna/sensors/NTCSensor.h @@ -67,7 +67,7 @@ class NTCSensor : public AnalogSensor { // 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) { diff --git a/code/platformio.ini b/code/platformio.ini index 983261f5..934d8362 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -3318,3 +3318,80 @@ upload_flags = ${common.upload_flags} monitor_speed = ${common.monitor_speed} 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} \ No newline at end of file