Skip to content

Commit

Permalink
sns: add INA219 (current / power monitor)
Browse files Browse the repository at this point in the history
resolves #2501

Co-Authored-by: Maxim Prokhorov <prokhorov.max@outlook.com>
  • Loading branch information
hamed-ta and mcspr committed May 18, 2022
1 parent 8f19484 commit 660d8c3
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 8 deletions.
45 changes: 40 additions & 5 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,39 @@
#endif // non-volatile memory. A common value would be every 6h or
// 360 * 60 * 1000 milliseconds. By default, this is disabled.

// -----------------------------------------------------------------------------
// INA219
// Enable support by passing INA219_SUPPORT=1 build flag
// -----------------------------------------------------------------------------

#ifndef INA219_SUPPORT
#define INA219_SUPPORT 0
#endif

#ifndef INA219_ADDRESS
#define INA219_ADDRESS 0x00 // 0x00 means auto
#endif

#ifndef INA219_OPERATING_MODE
#define INA219_OPERATING_MODE SHUNT_AND_BUS_CONTINUOUS
#endif

#ifndef INA219_SHUNT_MODE
#define INA219_SHUNT_MODE BIT_MODE_12
#endif

#ifndef INA219_BUS_MODE
#define INA219_BUS_MODE BIT_MODE_12
#endif

#ifndef INA219_BUS_RANGE
#define INA219_BUS_RANGE BRNG_32
#endif

#ifndef INA219_GAIN
#define INA219_GAIN PG_320
#endif

// -----------------------------------------------------------------------------
// ADC
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1432,11 +1465,12 @@
#if ( ADE7953_SUPPORT || \
AM2320_SUPPORT || \
BH1750_SUPPORT || \
BME680_SUPPORT || \
BMP180_SUPPORT || \
BMX280_SUPPORT || \
BME680_SUPPORT || \
EMON_ADC121_SUPPORT || \
EMON_ADS1X15_SUPPORT || \
INA219_SUPPORT || \
SHT3X_I2C_SUPPORT || \
SI1145_SUPPORT || \
SI7021_SUPPORT || \
Expand Down Expand Up @@ -1469,8 +1503,8 @@
AM2320_SUPPORT || \
ANALOG_SUPPORT || \
BH1750_SUPPORT || \
BMP180_SUPPORT || \
BME680_SUPPORT || \
BMP180_SUPPORT || \
BMX280_SUPPORT || \
CSE7766_SUPPORT || \
DALLAS_SUPPORT || \
Expand All @@ -1485,7 +1519,9 @@
EZOPH_SUPPORT || \
GEIGER_SUPPORT || \
GUVAS12SD_SUPPORT || \
HDC1080_SUPPORT || \
HLW8012_SUPPORT || \
INA219_SUPPORT || \
LDR_SUPPORT || \
MAX6675_SUPPORT || \
MHZ19_SUPPORT || \
Expand All @@ -1495,6 +1531,7 @@
PM1006_SUPPORT || \
PMSX003_SUPPORT || \
PULSEMETER_SUPPORT || \
PZEM004TV30_SUPPORT || \
PZEM004T_SUPPORT || \
SDS011_SUPPORT || \
SENSEAIR_SUPPORT || \
Expand All @@ -1508,9 +1545,7 @@
TMP3X_SUPPORT || \
V9261F_SUPPORT || \
VEML6075_SUPPORT || \
VL53L1X_SUPPORT || \
HDC1080_SUPPORT || \
PZEM004TV30_SUPPORT \
VL53L1X_SUPPORT \
)
#endif

1 change: 1 addition & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@
#define SENSOR_BME680_ID 42
#define SENSOR_SM300D2_ID 43
#define SENSOR_PM1006_ID 44
#define SENSOR_INA219_ID 45

//--------------------------------------------------------------------------------
// Magnitudes
Expand Down
17 changes: 17 additions & 0 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ namespace {
#include "sensors/HLW8012Sensor.h"
#endif

#if INA219_SUPPORT
#include "sensors/INA219Sensor.h"
#endif

#if LDR_SUPPORT
#include "sensors/LDRSensor.h"
#endif
Expand Down Expand Up @@ -2858,6 +2862,19 @@ void _sensorLoad() {
}
#endif

#if INA219_SUPPORT
{
auto* sensor = new INA219Sensor();
sensor->setAddress(INA219_ADDRESS);
sensor->setOperatingMode(INA219Sensor::INA219_OPERATING_MODE);
sensor->setShuntMode(INA219Sensor::INA219_SHUNT_MODE);
sensor->setBusMode(INA219Sensor::INA219_BUS_MODE);
sensor->setBusRange(INA219Sensor::INA219_BUS_RANGE);
sensor->setGain(INA219Sensor::INA219_GAIN);
_sensors.push_back(sensor);
}
#endif

#if LDR_SUPPORT
{
LDRSensor * sensor = new LDRSensor();
Expand Down
2 changes: 2 additions & 0 deletions code/espurna/sensors/I2CSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class I2CSensorAddress {
template <typename T = BaseSensor>
class I2CSensor : public T {
public:
// Make sure to export base constructor, we don't have any
using T::T;

// Descriptive name of the slot # index
String description(unsigned char) const override {
Expand Down
Loading

0 comments on commit 660d8c3

Please sign in to comment.