Napisałem sobie obsługę licznika opartego o układ HJL01. Nie wiem dlaczego ale metoda onSaveState wywołuje się tylko przy starcie urządzenia, przez co nie zapisuje się stan licznika. A może idę zupełnie w złym kierunku?
Code: Select all
#ifndef _Hj101_h
#define _Hj101_h
#include <Arduino.h>
// https://github.com/xoseperez/hlw8012
#include <HLW8012.h>
#include <supla/storage/storage.h>
#include <supla/element.h>
#include "one_phase_electricity_meter.h"
#define CURRENT_MODE LOW
#define HJL01_CURRENT_RATIO 25740
#define HJL01_VOLTAGE_RATIO 313400
#define HJL01_POWER_RATIO 3414290
namespace Supla {
namespace Sensor {
class HJ101 : public OnePhaseElectricityMeter, public Element {
public:
HJ101(int8_t pinCF, int8_t pinCF1, int8_t pinSEL, bool currentWhen = HIGH, bool use_interrupts = true)
: pinCF(pinCF), pinCF1(pinCF1), pinSEL(pinSEL), currentWhen(currentWhen), use_interrupts(use_interrupts) {
}
void onInit() {
hj101.setCurrentMultiplier(HJL01_CURRENT_RATIO);
hj101.setVoltageMultiplier(HJL01_VOLTAGE_RATIO);
hj101.setPowerMultiplier(HJL01_POWER_RATIO);
hj101.begin(pinCF, pinCF1, pinSEL, currentWhen, use_interrupts);
attachInterrupt(pinCF, hjl01_cf_interrupt, FALLING);
attachInterrupt(pinCF1, hjl01_cf1_interrupt, FALLING);
readValuesFromDevice();
updateChannelValues();
}
virtual void readValuesFromDevice() {
energy += hj101.getEnergy();
setVoltage(0, hj101.getVoltage() * 100); // voltage in 0.01 V
setCurrent(0, hj101.getCurrent() * 1000); // current in 0.001 A
setPowerActive(0, hj101.getActivePower() * 100000); // power in 0.00001 kW
setFwdActEnergy(0, energy); // energy in 0.00001 kWh
setPowerApparent(0, hj101.getApparentPower() * 100000); // power in 0.00001 kVA
setPowerReactive(0, hj101.getReactivePower() * 100000); // power in 0.00001 kvar
setPowerFactor(0, hj101.getPowerFactor() * 1000); // power in 0.001
}
void onSaveState() {
Serial.println("onSaveState------------------------");
Supla::Storage::WriteState((unsigned char *)&energy, sizeof(energy));
}
void onLoadState() {
_supla_int64_t data;
if (Supla::Storage::ReadState((unsigned char *)&data, sizeof(data))) {
Serial.println("onLoadState--------------------");
setCounter(data);
}
}
_supla_int64_t getCounter() {
return energy;
}
void setCounter(_supla_int64_t value) {
energy = value;
setFwdActEnergy(0, value);
}
// When using interrupts we have to call the library entry point
// whenever an interrupt is triggered
static void ICACHE_RAM_ATTR hjl01_cf1_interrupt() {
hj101.cf1_interrupt();
}
static void ICACHE_RAM_ATTR hjl01_cf_interrupt() {
hj101.cf_interrupt();
}
protected:
static HLW8012 hj101;
int8_t pinCF;
int8_t pinCF1;
int8_t pinSEL;
bool currentWhen;
bool use_interrupts;
unsigned _supla_int64_t energy;
};
HLW8012 HJ101::hj101;
}; // namespace Sensor
}; // namespace Supla
#endif