From e2b7f953a77c8fcfdd42261c98648273fe97dbee Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Mon, 6 Aug 2018 01:16:06 +0100 Subject: [PATCH 1/2] Add support for VEML6075 --- code/espurna/config/progmem.h | 18 +++-- code/espurna/config/sensors.h | 26 ++++++- code/espurna/config/types.h | 25 ++++--- code/espurna/sensor.ino | 8 +++ code/espurna/sensors/GUVAS12SDSensor.h | 2 +- code/espurna/sensors/VEML6075Sensor.h | 96 ++++++++++++++++++++++++++ code/html/custom.js | 4 +- code/platformio.ini | 1 + 8 files changed, 160 insertions(+), 20 deletions(-) create mode 100644 code/espurna/sensors/VEML6075Sensor.h diff --git a/code/espurna/config/progmem.h b/code/espurna/config/progmem.h index 11811783..c4afe583 100644 --- a/code/espurna/config/progmem.h +++ b/code/espurna/config/progmem.h @@ -223,6 +223,9 @@ PROGMEM const char espurna_sensors[] = #if V9261F_SUPPORT "V9261F " #endif + #if VEML6075_SUPPORT + "VEML6075 " + #endif ""; @@ -232,7 +235,9 @@ PROGMEM const unsigned char magnitude_decimals[] = { 3, 0, 0, 0, 0, 0, 0, 0, // Power decimals 0, 0, 0, // analog, digital, event 0, 0, 0, // PM - 0, 0, 3, 3, 0, + 0, 0, + 0, 0, 3, // UVA, UVB, UVI + 3, 0, 4, 4, // Geiger Counter decimals 0, 0, 0, 0 // NO2, CO, Ohms @@ -258,7 +263,9 @@ PROGMEM const char magnitude_pm2dot5_topic[] = "pm2dot5"; PROGMEM const char magnitude_pm10_topic[] = "pm10"; PROGMEM const char magnitude_co2_topic[] = "co2"; PROGMEM const char magnitude_lux_topic[] = "lux"; -PROGMEM const char magnitude_uv_topic[] = "uv"; +PROGMEM const char magnitude_uva_topic[] = "uva"; +PROGMEM const char magnitude_uvb_topic[] = "uvb"; +PROGMEM const char magnitude_uvi_topic[] = "uvi"; PROGMEM const char magnitude_distance_topic[] = "distance"; PROGMEM const char magnitude_hcho_topic[] = "hcho"; PROGMEM const char magnitude_geiger_cpm_topic[] = "ldr_cpm"; // local dose rate [Counts per minute] @@ -275,7 +282,8 @@ PROGMEM const char* const magnitude_topics[] = { magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic, magnitude_analog_topic, magnitude_digital_topic, magnitude_event_topic, magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic, - magnitude_co2_topic, magnitude_lux_topic, magnitude_uv_topic, + magnitude_co2_topic, magnitude_lux_topic, + magnitude_uva_topic, magnitude_uvb_topic, magnitude_uvi_topic, magnitude_distance_topic, magnitude_hcho_topic, magnitude_geiger_cpm_topic, magnitude_geiger_sv_topic, magnitude_count_topic, @@ -296,7 +304,6 @@ PROGMEM const char magnitude_kwh[] = "kWh"; PROGMEM const char magnitude_ugm3[] = "µg/m³"; PROGMEM const char magnitude_ppm[] = "ppm"; PROGMEM const char magnitude_lux[] = "lux"; -PROGMEM const char magnitude_uv[] = "uv"; PROGMEM const char magnitude_distance[] = "m"; PROGMEM const char magnitude_mgm3[] = "mg/m³"; PROGMEM const char magnitude_geiger_cpm[] = "cpm"; // Counts per Minute: Unit of local dose rate (Geiger counting) @@ -311,7 +318,8 @@ PROGMEM const char* const magnitude_units[] = { magnitude_percentage, magnitude_joules, magnitude_joules, magnitude_empty, magnitude_empty, magnitude_empty, magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, - magnitude_ppm, magnitude_lux, magnitude_uv, + magnitude_ppm, magnitude_lux, + magnitude_empty, magnitude_empty, magnitude_empty, magnitude_distance, magnitude_mgm3, magnitude_geiger_cpm, magnitude_geiger_sv, // Geiger counter units magnitude_empty, // diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index b0ea4dcc..ac11f82a 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -690,6 +690,25 @@ #define V9261F_POWER_FACTOR 153699.0 #define V9261F_RPOWER_FACTOR V9261F_CURRENT_FACTOR +//------------------------------------------------------------------------------ +// VEML6075 based power sensor +// Enable support by passing VEML6075_SUPPORT=1 build flag +//------------------------------------------------------------------------------ + +#ifndef VEML6075_SUPPORT +#define VEML6075_SUPPORT 0 +#endif + +#ifndef VEML6075_INTEGRATION_TIME +#define VEML6075_INTEGRATION_TIME VEML6075::IT_100MS // The time, in milliseconds, allocated for a single +#endif // measurement. A longer timing budget allows for more + // accurate results at the cost of power. + +#ifndef VEML6075_DYNAMIC_MODE +#define VEML6075_DYNAMIC_MODE VEML6075::DYNAMIC_NORMAL // The dynamic mode can either be normal or high. In high +#endif // dynamic mode, the resolution increases by about two + // times. + // ============================================================================= // Sensor helpers configuration - can't move to dependencies.h // ============================================================================= @@ -724,7 +743,8 @@ SI7021_SUPPORT || \ SONAR_SUPPORT || \ TMP3X_SUPPORT || \ - V9261F_SUPPORT \ + V9261F_SUPPORT || \ + VEML6075_SUPPORT \ ) #endif @@ -882,4 +902,8 @@ #include "../sensors/V9261FSensor.h" #endif +#if VEML6075_SUPPORT + #include "../sensors/VEML6075Sensor.h" +#endif + #endif // SENSOR_SUPPORT diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index a984bb7e..ede6347b 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -279,6 +279,7 @@ #define SENSOR_SDS011_ID 0x27 #define SENSOR_MICS2710_ID 0x28 #define SENSOR_MICS5525_ID 0x29 +#define SENSOR_VEML6075_ID 0x30 //-------------------------------------------------------------------------------- // Magnitudes @@ -304,14 +305,16 @@ #define MAGNITUDE_PM10 17 #define MAGNITUDE_CO2 18 #define MAGNITUDE_LUX 19 -#define MAGNITUDE_UV 20 -#define MAGNITUDE_DISTANCE 21 -#define MAGNITUDE_HCHO 22 -#define MAGNITUDE_GEIGER_CPM 23 -#define MAGNITUDE_GEIGER_SIEVERT 24 -#define MAGNITUDE_COUNT 25 -#define MAGNITUDE_NO2 26 -#define MAGNITUDE_CO 27 -#define MAGNITUDE_RESISTANCE 28 - -#define MAGNITUDE_MAX 29 +#define MAGNITUDE_UVA 20 +#define MAGNITUDE_UVB 21 +#define MAGNITUDE_UVI 22 +#define MAGNITUDE_DISTANCE 23 +#define MAGNITUDE_HCHO 24 +#define MAGNITUDE_GEIGER_CPM 25 +#define MAGNITUDE_GEIGER_SIEVERT 26 +#define MAGNITUDE_COUNT 27 +#define MAGNITUDE_NO2 28 +#define MAGNITUDE_CO 29 +#define MAGNITUDE_RESISTANCE 30 + +#define MAGNITUDE_MAX 31 diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 48fef408..ff7396f3 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -637,6 +637,14 @@ void _sensorLoad() { } #endif + #if VEML6075_SUPPORT + { + VEML6075Sensor * sensor = new VEML6075Sensor(); + sensor->setIntegrationTime(VEML6075_INTEGRATION_TIME); + sensor->setDynamicMode(VEML6075_DYNAMIC_MODE); + _sensors.push_back(sensor); + } + #endif } void _sensorCallback(unsigned char i, unsigned char type, double value) { diff --git a/code/espurna/sensors/GUVAS12SDSensor.h b/code/espurna/sensors/GUVAS12SDSensor.h index f188b2b3..a4b65c54 100644 --- a/code/espurna/sensors/GUVAS12SDSensor.h +++ b/code/espurna/sensors/GUVAS12SDSensor.h @@ -104,7 +104,7 @@ class GUVAS12SDSensor : public BaseSensor { // Type for slot # index unsigned char type(unsigned char index) { - if (index == 0) return MAGNITUDE_UV; + if (index == 0) return MAGNITUDE_UVI; return MAGNITUDE_NONE; } diff --git a/code/espurna/sensors/VEML6075Sensor.h b/code/espurna/sensors/VEML6075Sensor.h new file mode 100644 index 00000000..60753401 --- /dev/null +++ b/code/espurna/sensors/VEML6075Sensor.h @@ -0,0 +1,96 @@ +// ----------------------------------------------------------------------------- +// VEML6075 Sensor over I2C +// Copyright (C) 2017-2018 by Xose Pérez +// ----------------------------------------------------------------------------- + +#if SENSOR_SUPPORT && VEML6075_SUPPORT + +#pragma once + +#undef I2C_SUPPORT +#define I2C_SUPPORT 1 // Explicitly request I2C support. + + +#include "Arduino.h" +#include "I2CSensor.h" +#include "SparkFun_VEML6075_Arduino_Library.h" + +class VEML6075Sensor : public I2CSensor { + + public: + + // --------------------------------------------------------------------- + // Public + // --------------------------------------------------------------------- + + VEML6075Sensor(): I2CSensor() { + _count = 3; + _sensor_id = SENSOR_VEML6075_ID; + _veml6075 = new VEML6075(); + } + + ~VEML6075Sensor() { + delete _veml6075; + } + + void begin() { + if (!_veml6075->begin()) { + return; + }; + + _ready = true; + } + + // --------------------------------------------------------------------- + // Sensor API + // --------------------------------------------------------------------- + + // Descriptive name of the sensor + String description() { + char buffer[25]; + snprintf(buffer, sizeof(buffer), "VEML6075 @ I2C (0x%02X)", _address); + return String(buffer); + } + + // Descriptive name of the slot # index + String slot(unsigned char index) { + return description(); + }; + + // Type for slot # index + unsigned char type(unsigned char index) { + if (index == 0) return MAGNITUDE_UVA; + if (index == 1) return MAGNITUDE_UVB; + if (index == 2) return MAGNITUDE_UVI; + return MAGNITUDE_NONE; + } + + // Pre-read hook (usually to populate registers with up-to-date data) + void pre() { + _error = SENSOR_ERROR_OK; + } + + // Current value for slot # index + double value(unsigned char index) { + if (index == 0) return _veml6075->a(); + if (index == 1) return _veml6075->b(); + if (index == 2) return _veml6075->index(); + + return 0; + } + + void setIntegrationTime(VEML6075::veml6075_uv_it_t integration_time) { + _veml6075->setIntegrationTime(integration_time); + } + + void setDynamicMode(VEML6075::veml6075_hd_t dynamic_mode) { + _veml6075->setHighDynamic(dynamic_mode); + } + + protected: + + VEML6075 * _veml6075 = NULL; + +}; + +#endif // SENSOR_SUPPORT && VEML6075_SUPPORT diff --git a/code/html/custom.js b/code/html/custom.js index f10f4d2c..5eee06d1 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -47,7 +47,7 @@ function sensorName(id) { "Events", "PMSX003", "BMX280", "MHZ19", "SI7021", "SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD", "TMP3X", "Sonar", "SenseAir", "GeigerTicks", "GeigerCPM", - "NTC", "SDS011", "MICS2710", "MICS5525" + "NTC", "SDS011", "MICS2710", "MICS5525", "VEML6075" ]; if (1 <= id && id <= names.length) { return names[id - 1]; @@ -61,7 +61,7 @@ function magnitudeType(type) { "Current", "Voltage", "Active Power", "Apparent Power", "Reactive Power", "Power Factor", "Energy", "Energy (delta)", "Analog", "Digital", "Event", - "PM1.0", "PM2.5", "PM10", "CO2", "Lux", "UV", "Distance" , "HCHO", + "PM1.0", "PM2.5", "PM10", "CO2", "Lux", "UVA", "UVB", "UV Index", "Distance" , "HCHO", "Local Dose Rate", "Local Dose Rate", "Count", "NO2", "CO", "Resistance" diff --git a/code/platformio.ini b/code/platformio.ini index 9d81cf13..afaa9607 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -97,6 +97,7 @@ lib_deps = https://github.com/LowPowerLab/RFM69#1.1.3 https://github.com/xoseperez/Time NewPing + https://github.com/sparkfun/SparkFun_VEML6075_Arduino_Library#V_1.0.3 lib_ignore = # ------------------------------------------------------------------------------ From c1658da1c484e305b6cf917e99bd89a09eeefc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 19 Nov 2018 22:50:09 +0100 Subject: [PATCH 2/2] Update sensor.ino --- code/espurna/sensor.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 5dd16208..53e41df5 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -712,6 +712,7 @@ void _sensorLoad() { sensor->setDynamicMode(VEML6075_DYNAMIC_MODE); _sensors.push_back(sensor); } + #endif #if VL53L1X_SUPPORT {