@ -55,7 +55,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define ENABLE_NOFUSS 1
# define ENABLE_NOFUSS 1
# define ENABLE_MQTT 1
# define ENABLE_MQTT 1
# define ENABLE_WEBSERVER 1
# define ENABLE_WEBSERVER 1
# define ENABLE_ENERGYMONITOR 0
# define ENABLE_POWERMONITOR 1
# define ENABLE_DHT 0
# define ENABLE_DHT 0
# define APP_NAME "Espurna"
# define APP_NAME "Espurna"
@ -109,9 +109,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define WIFI_STATUS_CONNECTED 1
# define WIFI_STATUS_CONNECTED 1
# define WIFI_STATUS_AP 2
# define WIFI_STATUS_AP 2
# define CURRENT_PIN A0
# define CURRENT_PIN 0
# define ADC_BITS 10
# define REFERENCE_VOLTAGE 1.0
# define REFERENCE_VOLTAGE 1.0
# define CURRENT_PRECISION 1
# define CURRENT_PRECISION 1
# define CURRENT_OFFSET 0.3
# define SAMPLES_X_MEASUREMENT 1000
# define SAMPLES_X_MEASUREMENT 1000
# define MEASUREMENT_INTERVAL 10000
# define MEASUREMENT_INTERVAL 10000
# define MEASUREMENTS_X_MESSAGE 6
# define MEASUREMENTS_X_MESSAGE 6
@ -138,7 +140,7 @@ DebounceEvent button1 = false;
char mqttTemperatureTopic [ 40 ] ;
char mqttTemperatureTopic [ 40 ] ;
char mqttHumidityTopic [ 40 ] ;
char mqttHumidityTopic [ 40 ] ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
char mqttPowerTopic [ 40 ] ;
char mqttPowerTopic [ 40 ] ;
# endif
# endif
bool isMQTTMessage = false ;
bool isMQTTMessage = false ;
@ -156,8 +158,8 @@ DebounceEvent button1 = false;
double humidity ;
double humidity ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
EnergyMonitor monito r ;
# if ENABLE_POWER MONITOR
EmonLiteESP powe r ;
double current ;
double current ;
# endif
# endif
@ -707,7 +709,7 @@ void wifiLoop() {
root [ " rfDevice " ] = config . rfDevice ;
root [ " rfDevice " ] = config . rfDevice ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
root [ " pwMainsVoltage " ] = config . pwMainsVoltage ;
root [ " pwMainsVoltage " ] = config . pwMainsVoltage ;
root [ " pwCurrentRatio " ] = config . pwCurrentRatio ;
root [ " pwCurrentRatio " ] = config . pwCurrentRatio ;
# endif
# endif
@ -730,7 +732,7 @@ void wifiLoop() {
# if ENABLE_MQTT
# if ENABLE_MQTT
root [ " mqtt " ] = mqtt . connected ( ) ? 1 : 0 ;
root [ " mqtt " ] = mqtt . connected ( ) ? 1 : 0 ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
root [ " power " ] = current * config . pwMainsVoltage . toFloat ( ) ;
root [ " power " ] = current * config . pwMainsVoltage . toFloat ( ) ;
# endif
# endif
# if ENABLE_DHT
# if ENABLE_DHT
@ -776,7 +778,7 @@ void wifiLoop() {
if ( server . hasArg ( " rfChannel " ) ) config . rfChannel = server . arg ( " rfChannel " ) ;
if ( server . hasArg ( " rfChannel " ) ) config . rfChannel = server . arg ( " rfChannel " ) ;
if ( server . hasArg ( " rfDevice " ) ) config . rfDevice = server . arg ( " rfDevice " ) ;
if ( server . hasArg ( " rfDevice " ) ) config . rfDevice = server . arg ( " rfDevice " ) ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
if ( server . hasArg ( " pwMainsVoltage " ) ) config . pwMainsVoltage = server . arg ( " pwMainsVoltage " ) ;
if ( server . hasArg ( " pwMainsVoltage " ) ) config . pwMainsVoltage = server . arg ( " pwMainsVoltage " ) ;
if ( server . hasArg ( " pwCurrentRatio " ) ) config . pwCurrentRatio = server . arg ( " pwCurrentRatio " ) ;
if ( server . hasArg ( " pwCurrentRatio " ) ) config . pwCurrentRatio = server . arg ( " pwCurrentRatio " ) ;
# endif
# endif
@ -787,8 +789,8 @@ void wifiLoop() {
# if ENABLE_RF
# if ENABLE_RF
rfBuildCodes ( ) ;
rfBuildCodes ( ) ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
monito r. setCurrentRatio ( config . pwCurrentRatio . toFloat ( ) ) ;
# if ENABLE_POWER MONITOR
powe r. setCurrentRatio ( config . pwCurrentRatio . toFloat ( ) ) ;
# endif
# endif
wifiSetup ( true ) ;
wifiSetup ( true ) ;
@ -859,7 +861,7 @@ void wifiLoop() {
mqttIPTopic [ tmp . length ( ) + 1 ] = 0 ;
mqttIPTopic [ tmp . length ( ) + 1 ] = 0 ;
// Get publish current topic
// Get publish current topic
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
tmp = base + " /power " ;
tmp = base + " /power " ;
tmp . toCharArray ( mqttPowerTopic , tmp . length ( ) + 1 ) ;
tmp . toCharArray ( mqttPowerTopic , tmp . length ( ) + 1 ) ;
mqttPowerTopic [ tmp . length ( ) + 1 ] = 0 ;
mqttPowerTopic [ tmp . length ( ) + 1 ] = 0 ;
@ -1060,19 +1062,19 @@ void wifiLoop() {
// Energy Monitor
// Energy Monitor
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
# if ENABLE_ENERGY MONITOR
# if ENABLE_POWER MONITOR
# if ENABLE_MQTT
# if ENABLE_MQTT
unsigned int getCurrent ( ) {
unsigned int currentCallback ( ) {
return analogRead ( CURRENT_PIN ) ;
return analogRead ( CURRENT_PIN ) ;
}
}
void energy MonitorSetup( ) {
monito r. initCurrent ( getCurrent , REFERENCE_VOLTAGE , config . pwCurrentRatio . toFloat ( ) ) ;
monito r. setPrecision ( CURRENT_PRECISION ) ;
void power MonitorSetup( ) {
powe r. initCurrent ( currentCallback , ADC_BITS , REFERENCE_VOLTAGE , config . pwCurrentRatio . toFloat ( ) ) ;
powe r. setPrecision ( CURRENT_PRECISION ) ;
}
}
void energy MonitorLoop( ) {
void power MonitorLoop( ) {
static unsigned long next_measurement = millis ( ) ;
static unsigned long next_measurement = millis ( ) ;
static byte measurements = 0 ;
static byte measurements = 0 ;
@ -1088,7 +1090,8 @@ void wifiLoop() {
if ( ! digitalRead ( RELAY_PIN ) ) {
if ( ! digitalRead ( RELAY_PIN ) ) {
current = 0 ;
current = 0 ;
} else {
} else {
current = monitor . getCurrent ( SAMPLES_X_MEASUREMENT ) ;
current = power . getCurrent ( SAMPLES_X_MEASUREMENT ) ;
current - = CURRENT_OFFSET ;
}
}
if ( measurements = = 0 ) {
if ( measurements = = 0 ) {
@ -1208,8 +1211,8 @@ void setup() {
# if ENABLE_DHT
# if ENABLE_DHT
dhtSetup ( ) ;
dhtSetup ( ) ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
energy MonitorSetup( ) ;
# if ENABLE_POWER MONITOR
power MonitorSetup( ) ;
# endif
# endif
}
}
@ -1237,8 +1240,8 @@ void loop() {
# if ENABLE_DHT
# if ENABLE_DHT
dhtLoop ( ) ;
dhtLoop ( ) ;
# endif
# endif
# if ENABLE_ENERGY MONITOR
energy MonitorLoop( ) ;
# if ENABLE_POWER MONITOR
power MonitorLoop( ) ;
# endif
# endif
delay ( 1 ) ;
delay ( 1 ) ;