Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

125 lines
3.5 KiB

// -----------------------------------------------------------------------------
// Energy Monitor Sensor using builtin ADC
// Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT && EMON_ANALOG_SUPPORT
#pragma once
#include "Arduino.h"
#include "EmonSensor.h"
#define EMON_ANALOG_RESOLUTION 10
#define EMON_ANALOG_CHANNELS 1
class EmonAnalogSensor : public EmonSensor {
public:
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
EmonAnalogSensor(): EmonSensor() {
_channels = EMON_ANALOG_CHANNELS;
_sensor_id = SENSOR_EMON_ANALOG_ID;
init();
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
// Initialization method, must be idempotent
void begin() {
if (!_dirty) return;
_dirty = false;
// Number of slots
_count = _magnitudes;
// Bit depth
_resolution = EMON_ANALOG_RESOLUTION;
// Init analog PIN)
pinMode(0, INPUT);
// Call the parent class method
EmonSensor::begin();
// warmup channel 0 (the only one)
read(0);
}
// Descriptive name of the sensor
String description() {
return String("EMON @ ANALOG @ GPIO0");
}
// Type for slot # index
unsigned char type(unsigned char index) {
_error = SENSOR_ERROR_OK;
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (index == i++) return MAGNITUDE_CURRENT;
#endif
#if EMON_REPORT_POWER
if (index == i++) return MAGNITUDE_POWER_APPARENT;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return MAGNITUDE_ENERGY;
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
// Pre-read hook (usually to populate registers with up-to-date data)
void pre() {
_current[0] = read(0);
#if EMON_REPORT_ENERGY
static unsigned long last = 0;
if (last > 0) {
_energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
}
last = millis();
#endif
}
// Current value for slot # index
double value(unsigned char index) {
_error = SENSOR_ERROR_OK;
unsigned char channel = index / _magnitudes;
unsigned char i=0;
#if EMON_REPORT_CURRENT
if (index == i++) return _current[channel];
#endif
#if EMON_REPORT_POWER
if (index == i++) return _current[channel] * _voltage;
#endif
#if EMON_REPORT_ENERGY
if (index == i) return _energy[channel];
#endif
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
protected:
unsigned int readADC(unsigned char channel) {
(void) channel;
return analogRead(0);
}
};
#endif // SENSOR_SUPPORT && EMON_ANALOG_SUPPORT