Skip to content

Commit

Permalink
Option to report energy data in kWh (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Feb 26, 2018
1 parent f0b799b commit da3f7b7
Show file tree
Hide file tree
Showing 7 changed files with 3,231 additions and 3,205 deletions.
7 changes: 0 additions & 7 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,6 @@ PROGMEM const char* const custom_reset_string[] = {
// Do not save relay state after these many milliseconds
#define RELAY_SAVE_DELAY 1000

//------------------------------------------------------------------------------
// I18N
//------------------------------------------------------------------------------

#define TMP_CELSIUS 0
#define TMP_FAHRENHEIT 1

//------------------------------------------------------------------------------
// LED
//------------------------------------------------------------------------------
Expand Down
25 changes: 20 additions & 5 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#define SENSOR_USE_INDEX 0 // Use the index in topic (i.e. temperature/0)
// even if just one sensor (0 for backwards compatibility)

#ifndef SENSOR_TEMPERATURE_UNITS
#define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT)
#endif

#ifndef SENSOR_TEMPERATURE_CORRECTION
#define SENSOR_TEMPERATURE_CORRECTION 0.0 // Offset correction
#endif
Expand All @@ -39,6 +35,24 @@
#define SENSOR_PUBLISH_ADDRESSES 0 // Publish sensor addresses
#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses

//------------------------------------------------------------------------------
// UNITS
//------------------------------------------------------------------------------

#define ENERGY_JOULES 0
#define ENERGY_KWH 1

#define TMP_CELSIUS 0
#define TMP_FAHRENHEIT 1

#ifndef SENSOR_TEMPERATURE_UNITS
#define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT)
#endif

#ifndef SENSOR_ENERGY_UNITS
#define SENSOR_ENERGY_UNITS ENERGY_JOULES // Energy units (ENERGY_JOULES | ENERGY_KWH)
#endif

//--------------------------------------------------------------------------------
// Sensor ID
// These should remain over time, do not modify them, only add new ones at the end
Expand Down Expand Up @@ -504,7 +518,7 @@
PROGMEM const unsigned char magnitude_decimals[] = {
0,
1, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 0, 0, 3, 3,
0, 0, 0,
0, 0, 0,
0, 0
Expand Down Expand Up @@ -550,6 +564,7 @@ PROGMEM const char magnitude_amperes[] = "A";
PROGMEM const char magnitude_volts[] = "V";
PROGMEM const char magnitude_watts[] = "W";
PROGMEM const char magnitude_joules[] = "J";
PROGMEM const char magnitude_kwh[] = "kWh";
PROGMEM const char magnitude_ugm3[] = "µg/m3";
PROGMEM const char magnitude_ppm[] = "ppm";
PROGMEM const char magnitude_lux[] = "lux";
Expand Down
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
10 changes: 10 additions & 0 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bool _sensor_realtime = API_REAL_TIME_VALUES;
unsigned long _sensor_read_interval = 1000 * SENSOR_READ_INTERVAL;
unsigned char _sensor_report_every = SENSOR_REPORT_EVERY;
unsigned char _sensor_temperature_units = SENSOR_TEMPERATURE_UNITS;
unsigned char _sensor_energy_units = SENSOR_ENERGY_UNITS;
double _sensor_temperature_correction = SENSOR_TEMPERATURE_CORRECTION;

// -----------------------------------------------------------------------------
Expand All @@ -50,6 +51,9 @@ double _magnitudeProcess(unsigned char type, double value) {
if (_sensor_temperature_units == TMP_FAHRENHEIT) value = value * 1.8 + 32;
value = value + _sensor_temperature_correction;
}
if (type == MAGNITUDE_ENERGY || type == MAGNITUDE_ENERGY_DELTA) {
if (_sensor_energy_units == ENERGY_KWH) value = value / 3600000;
}
return roundTo(value, _magnitudeDecimals(type));
}

Expand Down Expand Up @@ -110,6 +114,7 @@ void _sensorWebSocketStart(JsonObject& root) {
root["sensorsVisible"] = 1;
//root["apiRealTime"] = _sensor_realtime;
root["tmpUnits"] = _sensor_temperature_units;
root["energyUnits"] = _sensor_energy_units;
root["tmpCorrection"] = _sensor_temperature_correction;
root["snsRead"] = _sensor_read_interval / 1000;
root["snsReport"] = _sensor_report_every;
Expand Down Expand Up @@ -491,6 +496,7 @@ void _sensorConfigure() {
_sensor_report_every = constrain(getSetting("snsReport", SENSOR_REPORT_EVERY).toInt(), SENSOR_REPORT_MIN_EVERY, SENSOR_REPORT_MAX_EVERY);
_sensor_realtime = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
_sensor_temperature_units = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS).toInt();
_sensor_energy_units = getSetting("energyUnits", SENSOR_ENERGY_UNITS).toInt();
_sensor_temperature_correction = getSetting("tmpCorrection", SENSOR_TEMPERATURE_CORRECTION).toFloat();

// Update filter sizes
Expand Down Expand Up @@ -608,6 +614,10 @@ String magnitudeUnits(unsigned char type) {
if (type < MAGNITUDE_MAX) {
if ((type == MAGNITUDE_TEMPERATURE) && (_sensor_temperature_units == TMP_FAHRENHEIT)) {
strncpy_P(buffer, magnitude_fahrenheit, sizeof(buffer));
} else if ((type == MAGNITUDE_ENERGY) && (_sensor_energy_units == ENERGY_KWH)) {
strncpy_P(buffer, magnitude_kwh, sizeof(buffer));
} else if ((type == MAGNITUDE_ENERGY_DELTA) && (_sensor_energy_units == ENERGY_KWH)) {
strncpy_P(buffer, magnitude_kwh, sizeof(buffer));
} else {
strncpy_P(buffer, magnitude_units[type], sizeof(buffer));
}
Expand Down
Loading

0 comments on commit da3f7b7

Please sign in to comment.