Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sns: add SI1145 sensor #2216

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions code/espurna/board.ino
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ PROGMEM const char espurna_sensors[] =
#if ADE7953_SUPPORT
"ADE7953 "
#endif
#if SI1145_SUPPORT
"SI1145 "
#endif
"";

#endif // SENSOR_SUPPORT == 1
Expand Down
41 changes: 28 additions & 13 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
// =============================================================================
Expand All @@ -1294,6 +1307,7 @@
EMON_ADC121_SUPPORT || \
EMON_ADS1X15_SUPPORT || \
SHT3X_I2C_SUPPORT || \
SI1145_SUPPORT || \
SI7021_SUPPORT || \
VEML6075_SUPPORT || \
VL53L1X_SUPPORT \
Expand Down Expand Up @@ -1350,6 +1364,7 @@
SDS011_SUPPORT || \
SENSEAIR_SUPPORT || \
SHT3X_I2C_SUPPORT || \
SI1145_SUPPORT || \
SI7021_SUPPORT || \
SONAR_SUPPORT || \
T6613_SUPPORT || \
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 @@ -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
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,9 @@ void sensorLoop();
#include "sensors/ADE7953Sensor.h"
#endif

#if SI1145_SUPPORT
#include "sensors/SI1145Sensor.h"
#endif

//--------------------------------------------------------------------------------

10 changes: 9 additions & 1 deletion code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1531,14 +1531,22 @@ void _sensorLoad() {
}
#endif

#if ADE7953_SUPPORT
#if ADE7953_SUPPORT
{
ADE7953Sensor * sensor = new ADE7953Sensor();
sensor->setAddress(ADE7953_ADDRESS);
_sensors.push_back(sensor);
}
#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) {
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/sensors/I2CSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T = BaseSensor>
class I2CSensor : public T {

Expand Down
80 changes: 80 additions & 0 deletions code/espurna/sensors/SI1145Sensor.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include <Adafruit_SI1145.h>

#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
1 change: 1 addition & 0 deletions code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions code/test/build/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down