Browse Source

Using external EmonLiteESP library

fastled
Xose Pérez 8 years ago
parent
commit
0ab4fdec71
4 changed files with 25 additions and 170 deletions
  1. +0
    -3
      README.md
  2. +0
    -99
      code/lib/EmonLiteESP/EmonLiteESP.cpp
  3. +0
    -46
      code/lib/EmonLiteESP/EmonLiteESP.h
  4. +25
    -22
      code/src/main.cpp

+ 0
- 3
README.md View File

@ -85,9 +85,6 @@ You can also use "{identifier}" as place holder in the topic. It will be transla
After flashing the firmware via serial do a hard reset of the device (unplug & plug). There is an issue with the ESP.reset() method. Check [https://github.com/esp8266/Arduino/issues/1017][4] for more info.
Current version of ESP8266httpUpdate restarts the modules after SPIFFS update, thus preventing the firmware to be updated too. There is a recent commit fixing that which is not yet pushed to PLatformIO. Check [Fix example for ESP8266httpUpdate][5] for more info. Anyway, current expected behaviour is to not resetting the board from the ESP8266httpUpdate class (comment out line 300 in ```ESP8266httpUpdate.cpp```).
[1]: https://www.itead.cc/sonoff-wifi-wireless-switch.html
[2]: http://tinkerman.cat/adding-rf-to-a-non-rf-itead-sonoff
[3]: http://www.platformio.org


+ 0
- 99
code/lib/EmonLiteESP/EmonLiteESP.cpp View File

@ -1,99 +0,0 @@
/*
EmonLiteESP
Energy Monitor Library for ESP8266 based on EmonLib
Currently only support current sensing
*/
#include "Arduino.h"
#include "EmonLiteESP.h"
void EnergyMonitor::initCurrent(current_c callback, double ref, double ratio) {
_currentCallback = callback;
_referenceVoltage = ref;
_currentRatio = ratio;
_currentMidPoint = (ADC_COUNTS>>1);
calculatePrecision();
warmup();
};
void EnergyMonitor::warmup() {
int sample;
for (unsigned int n = 0; n < WARMUP_COUNTS; n++) {
sample = _currentCallback();
_currentMidPoint = (_currentMidPoint + (sample - _currentMidPoint) / ADC_COUNTS);
}
}
void EnergyMonitor::calculatePrecision() {
_currentFactor = _currentRatio * _referenceVoltage / ADC_COUNTS;
_precision = 0;
_multiplier = 1;
while (_multiplier * _currentFactor < 1) {
_multiplier *= 10;
++_precision;
}
--_precision;
_multiplier /= 10;
}
void EnergyMonitor::setReference(double ref) {
_referenceVoltage = ref;
}
void EnergyMonitor::setCurrentRatio(double ratio) {
_currentRatio = ratio;
}
byte EnergyMonitor::getPrecision() {
return _precision;
}
void EnergyMonitor::setPrecision(byte precision) {
_precision = precision;
_multiplier = 1;
for (byte i=0; i<_precision; i++) _multiplier *= 10;
}
double EnergyMonitor::getCurrent(unsigned int samples) {
int sample;
double filtered;
double sum;
//Serial.print("_currentMidPoint: "); Serial.println(_currentMidPoint);
for (unsigned int n = 0; n < samples; n++) {
// Read analog value
sample = _currentCallback();
// Digital low pass filter extracts the VDC offset
_currentMidPoint = (_currentMidPoint + (sample - _currentMidPoint) / ADC_COUNTS);
filtered = sample - _currentMidPoint;
// Root-mean-square method
sum += (filtered * filtered);
}
double rms = sqrt(sum / samples) - COUNT_OFFSET;
if (rms < 0) rms = 0;
double current = _currentFactor * rms;
/*
Serial.print("_currentMidPoint: "); Serial.println(_currentMidPoint);
Serial.print("sample : "); Serial.println(sample);
Serial.print("sum : "); Serial.println(sum);
Serial.print("samples : "); Serial.println(samples);
Serial.print("rms1 : "); Serial.println(sqrt(sum / samples));
Serial.print("rms2 : "); Serial.println(rms);
Serial.print("current : "); Serial.println(current);
*/
current = round(current * _multiplier) / _multiplier;
return current;
};

+ 0
- 46
code/lib/EmonLiteESP/EmonLiteESP.h View File

@ -1,46 +0,0 @@
/*
EmonLiteESP
Energy Monitor Library for ESP8266 based on EmonLib
Currently only support current sensing
*/
#ifndef EmonLiteESP_h
#define EmonLiteESP_h
#define ADC_BITS 10
#define ADC_COUNTS (1<<ADC_BITS)
#define COUNT_OFFSET 3
#define FILTER_SPEED 1024.0
#define WARMUP_COUNTS 3000
typedef unsigned int (*current_c)();
class EnergyMonitor {
public:
void initCurrent(current_c callback, double ref, double ratio);
double getCurrent(unsigned int samples);
byte getPrecision();
void setPrecision(byte precision);
void setReference(double ref);
void setCurrentRatio(double ratio);
void setCurrentOffset(double offset);
void calculatePrecision();
private:
current_c _currentCallback;
double _referenceVoltage;
double _currentRatio;
double _currentMidPoint;
double _currentFactor;
byte _precision;
double _multiplier;
void warmup();
};
#endif

+ 25
- 22
code/src/main.cpp View File

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


Loading…
Cancel
Save