|
@ -9,11 +9,49 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
#if ENABLE_EMON
|
|
|
#if ENABLE_EMON
|
|
|
|
|
|
|
|
|
#include <EmonLiteESP.h>
|
|
|
#include <EmonLiteESP.h>
|
|
|
|
|
|
#include "brzo_i2c.h"
|
|
|
|
|
|
|
|
|
|
|
|
// ADC121 Registers
|
|
|
|
|
|
|
|
|
|
|
|
#define ADC121_REG_RESULT 0x00
|
|
|
|
|
|
#define ADC121_REG_ALERT 0x01
|
|
|
|
|
|
#define ADC121_REG_CONFIG 0x02
|
|
|
|
|
|
#define ADC121_REG_LIMITL 0x03
|
|
|
|
|
|
#define ADC121_REG_LIMITH 0x04
|
|
|
|
|
|
#define ADC121_REG_HYST 0x05
|
|
|
|
|
|
#define ADC121_REG_CONVL 0x06
|
|
|
|
|
|
#define ADC121_REG_CONVH 0x07
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EmonLiteESP emon; |
|
|
EmonLiteESP emon; |
|
|
double _current = 0; |
|
|
double _current = 0; |
|
|
unsigned int _power = 0; |
|
|
unsigned int _power = 0; |
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
// Provider
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int currentCallback() { |
|
|
|
|
|
|
|
|
|
|
|
#if EMON_PROVIDER == EMON_ANALOG_PROVIDER
|
|
|
|
|
|
return analogRead(EMON_CURRENT_PIN); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if EMON_PROVIDER == EMON_ADC121_PROVIDER
|
|
|
|
|
|
uint8_t buffer[2]; |
|
|
|
|
|
brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY); |
|
|
|
|
|
buffer[0] = ADC121_REG_RESULT; |
|
|
|
|
|
brzo_i2c_write(buffer, 1, false); |
|
|
|
|
|
brzo_i2c_read(buffer, 2, false); |
|
|
|
|
|
brzo_i2c_end_transaction(); |
|
|
|
|
|
unsigned int value; |
|
|
|
|
|
value = (buffer[0] & 0x0F) << 8; |
|
|
|
|
|
value |= buffer[1]; |
|
|
|
|
|
return value; |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// EMON
|
|
|
// EMON
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
@ -30,10 +68,6 @@ double getCurrent() { |
|
|
return _current; |
|
|
return _current; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
unsigned int currentCallback() { |
|
|
|
|
|
return analogRead(EMON_CURRENT_PIN); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void powerMonitorSetup() { |
|
|
void powerMonitorSetup() { |
|
|
|
|
|
|
|
|
// backwards compatibility
|
|
|
// backwards compatibility
|
|
@ -53,6 +87,15 @@ void powerMonitorSetup() { |
|
|
); |
|
|
); |
|
|
emon.setPrecision(EMON_CURRENT_PRECISION); |
|
|
emon.setPrecision(EMON_CURRENT_PRECISION); |
|
|
|
|
|
|
|
|
|
|
|
#if EMON_PROVIDER == EMON_ADC121_PROVIDER
|
|
|
|
|
|
uint8_t buffer[2]; |
|
|
|
|
|
buffer[0] = ADC121_REG_CONFIG; |
|
|
|
|
|
buffer[1] = 0x00; |
|
|
|
|
|
brzo_i2c_start_transaction(EMON_ADC121_ADDRESS, I2C_SCL_FREQUENCY); |
|
|
|
|
|
brzo_i2c_write(buffer, 2, false); |
|
|
|
|
|
brzo_i2c_end_transaction(); |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
apiRegister("/api/power", "power", [](char * buffer, size_t len) { |
|
|
apiRegister("/api/power", "power", [](char * buffer, size_t len) { |
|
|
snprintf(buffer, len, "%d", _power); |
|
|
snprintf(buffer, len, "%d", _power); |
|
|
}); |
|
|
}); |
|
@ -97,7 +140,10 @@ void powerMonitorLoop() { |
|
|
|
|
|
|
|
|
float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat(); |
|
|
float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat(); |
|
|
|
|
|
|
|
|
//DEBUG_MSG("[ENERGY] Power now: %dW\n", int(_current * mainsVoltage));
|
|
|
|
|
|
|
|
|
char current[6]; |
|
|
|
|
|
dtostrf(_current, 5, 2, current); |
|
|
|
|
|
DEBUG_MSG("[ENERGY] Current: %sA\n", current); |
|
|
|
|
|
DEBUG_MSG("[ENERGY] Power: %dW\n", int(_current * mainsVoltage)); |
|
|
|
|
|
|
|
|
// Update websocket clients
|
|
|
// Update websocket clients
|
|
|
char text[20]; |
|
|
char text[20]; |
|
@ -112,7 +158,7 @@ void powerMonitorLoop() { |
|
|
measurements = 0; |
|
|
measurements = 0; |
|
|
|
|
|
|
|
|
char power[6]; |
|
|
char power[6]; |
|
|
snprintf(power, "%d", 6, _power); |
|
|
|
|
|
|
|
|
snprintf(power, 6, "%d", _power); |
|
|
mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); |
|
|
mqttSend(getSetting("emonPowerTopic", EMON_POWER_TOPIC).c_str(), power); |
|
|
#if ENABLE_DOMOTICZ
|
|
|
#if ENABLE_DOMOTICZ
|
|
|
domoticzSend("dczPowIdx", power); |
|
|
domoticzSend("dczPowIdx", power); |
|
|