diff --git a/code/espurna/board.ino b/code/espurna/board.ino index 85e319ae..fa4b2d0c 100644 --- a/code/espurna/board.ino +++ b/code/espurna/board.ino @@ -294,6 +294,9 @@ PROGMEM const char espurna_sensors[] = #if ADE7953_SUPPORT "ADE7953 " #endif + #if SI1145_SUPPORT + "SI1145 " + #endif ""; #endif // SENSOR_SUPPORT == 1 diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index bd601b94..3f583aaa 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -1225,6 +1225,32 @@ #define EZOPH_SYNC_INTERVAL 1000 // Amount of time (in ms) sync new readings. #endif +// ----------------------------------------------------------------------------- +// ADE7953 Shelly Sensor +// Enable support by passing ADE7953_SUPPORT=1 build flag +// ----------------------------------------------------------------------------- + +#ifndef ADE7953_SUPPORT +#define ADE7953_SUPPORT 0 +#endif + +#ifndef ADE7953_ADDRESS +#define ADE7953_ADDRESS 0x38 +#endif + +// ----------------------------------------------------------------------------- +// SI1145 UV Sensor over I2C +// Enable support by passing SI1145_SUPPORT=1 build flag +// ----------------------------------------------------------------------------- + +#ifndef SI1145_SUPPORT +#define SI1145_SUPPORT 0 +#endif + +#ifndef SI1145_ADDRESS +#define SI1145_ADDRESS 0x60 +#endif + // ----------------------------------------------------------------------------- // ADC // ----------------------------------------------------------------------------- @@ -1268,19 +1294,6 @@ #define I2C_PERFORM_SCAN 1 // Perform a bus scan on boot #endif -// ----------------------------------------------------------------------------- -// ADE7953 Shelly Sensor -// Enable support by passing ADE7953_SUPPORT=1 build flag -// ----------------------------------------------------------------------------- - -#ifndef ADE7953_SUPPORT -#define ADE7953_SUPPORT 0 -#endif - -#ifndef ADE7953_ADDRESS -#define ADE7953_ADDRESS 0x38 -#endif - // ============================================================================= // Configuration helpers // ============================================================================= @@ -1294,6 +1307,7 @@ EMON_ADC121_SUPPORT || \ EMON_ADS1X15_SUPPORT || \ SHT3X_I2C_SUPPORT || \ + SI1145_SUPPORT || \ SI7021_SUPPORT || \ VEML6075_SUPPORT || \ VL53L1X_SUPPORT \ @@ -1350,6 +1364,7 @@ SDS011_SUPPORT || \ SENSEAIR_SUPPORT || \ SHT3X_I2C_SUPPORT || \ + SI1145_SUPPORT || \ SI7021_SUPPORT || \ SONAR_SUPPORT || \ T6613_SUPPORT || \ diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h index 00c45584..47959d56 100644 --- a/code/espurna/config/types.h +++ b/code/espurna/config/types.h @@ -330,6 +330,7 @@ #define SENSOR_LDR_ID 36 #define SENSOR_ADE7953_ID 37 #define SENSOR_T6613_ID 38 +#define SENSOR_SI1145_ID 39 //-------------------------------------------------------------------------------- // Magnitudes diff --git a/code/espurna/sensor.h b/code/espurna/sensor.h index c333d244..846af9e8 100644 --- a/code/espurna/sensor.h +++ b/code/espurna/sensor.h @@ -311,5 +311,9 @@ void sensorLoop(); #include "sensors/ADE7953Sensor.h" #endif +#if SI1145_SUPPORT + #include "sensors/SI1145Sensor.h" +#endif + //-------------------------------------------------------------------------------- diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 60ff73c5..516a40be 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -1532,7 +1532,7 @@ void _sensorLoad() { } #endif - #if ADE7953_SUPPORT + #if ADE7953_SUPPORT { ADE7953Sensor * sensor = new ADE7953Sensor(); sensor->setAddress(ADE7953_ADDRESS); @@ -1540,6 +1540,14 @@ void _sensorLoad() { } #endif + #if SI1145_SUPPORT + { + SI1145Sensor * sensor = new SI1145Sensor(); + sensor->setAddress(SI1145_ADDRESS); + _sensors.push_back(sensor); + } + #endif + } void _sensorCallback(unsigned char i, unsigned char type, double value) { diff --git a/code/espurna/sensors/I2CSensor.h b/code/espurna/sensors/I2CSensor.h index 9a50e8d6..7726c34a 100644 --- a/code/espurna/sensors/I2CSensor.h +++ b/code/espurna/sensors/I2CSensor.h @@ -27,6 +27,9 @@ #include "BaseSensor.h" +// TODO: Must inherit from I2CSensor<>, not just I2CSensor. Even with default value :( +// Perhaps I2CSensor should be alias for I2CSensorBase? + template class I2CSensor : public T { diff --git a/code/espurna/sensors/SI1145Sensor.h b/code/espurna/sensors/SI1145Sensor.h new file mode 100644 index 00000000..18b98dba --- /dev/null +++ b/code/espurna/sensors/SI1145Sensor.h @@ -0,0 +1,80 @@ +// ----------------------------------------------------------------------------- +// SI1145 Sensor over I2C +// Copyright (C) 2020 by @HilverinkJ (https://github.com/HilverinkJ) +// Based on https://github.com/xoseperez/espurna/issues/2192#issuecomment-603430308 +// ----------------------------------------------------------------------------- + +#if SENSOR_SUPPORT && SI1145_SUPPORT + +#pragma once + +#include +#include + +#include "I2CSensor.h" + +class SI1145Sensor : public I2CSensor<> { + + public: + + SI1145Sensor() { + _count = 1; + _sensor_id = SENSOR_SI1145_ID; + _si1145 = new Adafruit_SI1145(); + } + + void begin() { + static unsigned char addresses[1] = { SI1145_ADDRESS }; + _address = _begin_i2c(_address, sizeof(addresses), addresses); + if (_address == 0) return; + + if (!_si1145->begin()) { + _ready = false; + return; + } + + // Adafruit library never sets any errors + _error = SENSOR_ERROR_OK; + + _ready = true; + } + + // --------------------------------------------------------------------- + // Sensor API + // --------------------------------------------------------------------- + + // Descriptive name of the sensor + String description() { + char buffer[25]; + snprintf(buffer, sizeof(buffer), "SI1145 @ 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_UVI; + return MAGNITUDE_NONE; + } + + // Pre-read hook (usually to populate registers with up-to-date data) + void pre() { + _uvi = _si1145->readUV() / 100.0; + } + + // Current value for slot # index + double value(unsigned char index) { + if (index == 0) return _uvi; + return 0.0; + } + + protected: + Adafruit_SI1145 * _si1145 = nullptr; + double _uvi = 0.0; +}; + +#endif // SENSOR_SUPPORT && SI1145_SUPPORT diff --git a/code/platformio.ini b/code/platformio.ini index 212fbd90..167deffe 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -157,6 +157,7 @@ lib_deps = https://github.com/pololu/vl53l1x-arduino#1.0.1 https://github.com/mcleng/MAX6675-Library#2.0.1 https://github.com/ThingPulse/esp8266-oled-ssd1306#3398c97 + Adafruit SI1145 Library@~1.1.1 lib_ignore = AsyncTCP diff --git a/code/test/build/sensor.h b/code/test/build/sensor.h index 9a8f6c0f..10bf6e1f 100644 --- a/code/test/build/sensor.h +++ b/code/test/build/sensor.h @@ -20,6 +20,7 @@ #define SDS011_SUPPORT 1 #define SENSEAIR_SUPPORT 1 #define SHT3X_I2C_SUPPORT 1 +#define SI1145_SUPPORT 1 #define SI7021_SUPPORT 1 #define SONAR_SUPPORT 1 #define T6613_SUPPORT 1