Browse Source

Support for ECH1560 energy monitor in new sensors, untested

fastled
Xose Pérez 6 years ago
parent
commit
f3c494d5da
8 changed files with 359 additions and 8 deletions
  1. +4
    -0
      code/espurna/config/arduino.h
  2. +43
    -5
      code/espurna/config/sensors.h
  3. +6
    -0
      code/espurna/espurna.ino
  4. +10
    -0
      code/espurna/sensor.ino
  5. +1
    -1
      code/espurna/sensors/DHTSensor.h
  6. +264
    -0
      code/espurna/sensors/ECH1560Sensor.h
  7. +1
    -1
      code/espurna/sensors/EventSensor.h
  8. +30
    -1
      code/html/custom.js

+ 4
- 0
code/espurna/config/arduino.h View File

@ -87,10 +87,14 @@
//#define DALLAS_SUPPORT 1
//#define DHT_SUPPORT 1
//#define DIGITAL_SUPPORT 1
//#define ECH1560_SUPPORT 1
//#define EMON_ADC121_SUPPORT 1
//#define EMON_ADS1X15_SUPPORT 1
//#define EMON_ANALOG_SUPPORT 1
//#define EVENTS_SUPPORT 1
//#define HLW8012_SUPPORT 1
//#define MHZ19_SUPPORT 1
//#define PMSX003_SUPPORT 1
//#define SHT3X_I2C_SUPPORT 1
//#define SI7021_SUPPORT 1
//#define V9261F_SUPPORT 1

+ 43
- 5
code/espurna/config/sensors.h View File

@ -74,7 +74,7 @@
// These should remain over time, do not modify them, only add new ones at the end
//--------------------------------------------------------------------------------
#define SENSOR_DHT_ID 0x01
#define SENSOR_DHTXX_ID 0x01
#define SENSOR_DALLAS_ID 0x02
#define SENSOR_EMON_ANALOG_ID 0x03
#define SENSOR_EMON_ADC121_ID 0x04
@ -105,8 +105,8 @@
#endif
#if ANALOG_SUPPORT
#undef ADC_VCC_ENABLED
#define ADC_VCC_ENABLED 0
#undef ADC_VCC_ENABLED
#define ADC_VCC_ENABLED 0
#endif
//------------------------------------------------------------------------------
@ -185,6 +185,27 @@
#define DIGITAL_DEFAULT_STATE 1
#endif
//------------------------------------------------------------------------------
// ECH1560 based power sensor
// Enable support by passing ECH1560_SUPPORT=1 build flag
//------------------------------------------------------------------------------
#ifndef ECH1560_SUPPORT
#define ECH1560_SUPPORT 1
#endif
#ifndef ECH1560_CLK_PIN
#define ECH1560_CLK_PIN 4 // CLK pin for the ECH1560
#endif
#ifndef ECH1560_MISO_PIN
#define ECH1560_MISO_PIN 5 // MISO pin for the ECH1560
#endif
#ifndef ECH1560_INVERTED
#define ECH1560_INVERTED 0 // Signal is inverted
#endif
//------------------------------------------------------------------------------
// Energy Monitor general settings
//------------------------------------------------------------------------------
@ -244,8 +265,8 @@
#endif
#if EMON_ANALOG_SUPPORT
#undef ADC_VCC_ENABLED
#define ADC_VCC_ENABLED 0
#undef ADC_VCC_ENABLED
#define ADC_VCC_ENABLED 0
#endif
//------------------------------------------------------------------------------
@ -271,6 +292,15 @@
#define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis
//------------------------------------------------------------------------------
// HLW8012 Energy monitor IC
// Enable support by passing HLW8012_SUPPORT=1 build flag
//------------------------------------------------------------------------------
#ifndef HLW8012_SUPPORT
#define HLW8012_SUPPORT 0
#endif
//------------------------------------------------------------------------------
// MHZ19 CO2 sensor
// Enable support by passing MHZ19_SUPPORT=1 build flag
@ -425,6 +455,10 @@
#include "sensors/DigitalSensor.h"
#endif
#if ECH1560_SUPPORT
#include "sensors/ECH1560Sensor.h"
#endif
#if EMON_ADC121_SUPPORT
#include "sensors/EmonADC121Sensor.h"
#endif
@ -441,6 +475,10 @@
#include "sensors/EventSensor.h"
#endif
#if HLW8012_SUPPORT
#include "sensors/HLW8012Sensor.h"
#endif
#if MHZ19_SUPPORT
#include <SoftwareSerial.h>
#include "sensors/MHZ19Sensor.h"


+ 6
- 0
code/espurna/espurna.ino View File

@ -215,6 +215,9 @@ void welcome() {
#if DIGITAL_SUPPORT
DEBUG_MSG_P(PSTR(" DIGITAL"));
#endif
#if ECH1560_SUPPORT
DEBUG_MSG_P(PSTR(" ECH1560"));
#endif
#if EMON_ADC121_SUPPORT
DEBUG_MSG_P(PSTR(" EMON_ADC121"));
#endif
@ -227,6 +230,9 @@ void welcome() {
#if EVENTS_SUPPORT
DEBUG_MSG_P(PSTR(" EVENTS"));
#endif
#if HLW8012_SUPPORT
DEBUG_MSG_P(PSTR(" HLW8012"));
#endif
#if MHZ19_SUPPORT
DEBUG_MSG_P(PSTR(" MHZ19"));
#endif


+ 10
- 0
code/espurna/sensor.ino View File

@ -271,6 +271,16 @@ void _sensorInit() {
}
#endif
#if ECH1560_SUPPORT
{
ECH1560Sensor * sensor = new ECH1560Sensor();
sensor->setCLK(ECH1560_CLK_PIN);
sensor->setMISO(ECH1560_MISO_PIN);
sensor->setInverted(ECH1560_INVERTED);
_sensors.push_back(sensor);
}
#endif
#if EMON_ADC121_SUPPORT
{
EmonADC121Sensor * sensor = new EmonADC121Sensor();


+ 1
- 1
code/espurna/sensors/DHTSensor.h View File

@ -27,7 +27,7 @@ class DHTSensor : public BaseSensor {
DHTSensor(): BaseSensor() {
_count = 2;
_sensor_id = SENSOR_DHT_ID;
_sensor_id = SENSOR_DHTXX_ID;
}
// ---------------------------------------------------------------------


+ 264
- 0
code/espurna/sensors/ECH1560Sensor.h View File

@ -0,0 +1,264 @@
// -----------------------------------------------------------------------------
// ECH1560 based power monitor
// Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
class ECH1560Sensor : public BaseSensor {
public:
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
ECH1560Sensor(): BaseSensor() {
_count = 3;
_sensor_id = SENSOR_ECH1560_ID;
}
~ECH1560Sensor() {
if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
}
// ---------------------------------------------------------------------
void setCLK(unsigned char clk) {
if (_clk == clk) return;
_clk = clk;
_dirty = true;
}
void setMISO(unsigned char miso) {
if (_miso == miso) return;
_miso = miso;
_dirty = true;
}
void setInverted(bool inverted) {
_inverted = inverted;
}
// ---------------------------------------------------------------------
unsigned char getCLK() {
return _clk;
}
unsigned char getMISO() {
return _miso;
}
bool getInverted() {
return _inverted;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
// Initialization method, must be idempotent
void begin() {
if (!_dirty) return;
_dirty = false;
pinMode(_clk, INPUT);
pinMode(_miso, INPUT);
if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
attach(this, _clk, RISING);
}
// Interrupt attach callback
void attached(unsigned char gpio) {
BaseSensor::attached(gpio);
_interrupt_gpio = gpio;
}
// Interrupt detach callback
void detached(unsigned char gpio) {
BaseSensor::detached(gpio);
if (_interrupt_gpio == gpio) _interrupt_gpio = GPIO_NONE;
}
void handleInterrupt() {
_isr();
}
// Descriptive name of the sensor
String description() {
char buffer[25];
snprintf(buffer, sizeof(buffer), "ECH1560 @ GPIO(%i,%i)", _clk, _miso);
return String(buffer);
}
// Type for slot # index
magnitude_t type(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return MAGNITUDE_CURRENT;
if (index == 1) return MAGNITUDE_VOLTAGE;
if (index == 2) return MAGNITUDE_POWER_APPARENT;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
_error = SENSOR_ERROR_OK;
if (index == 0) return _current;
if (index == 1) return _voltage;
if (index == 2) return _apparent;
_error = SENSOR_ERROR_OUT_OF_RANGE;
return 0;
}
protected:
// ---------------------------------------------------------------------
// Protected
// ---------------------------------------------------------------------
void ICACHE_RAM_ATTR _isr() {
// if we are trying to find the sync-time (CLK goes high for 1-2ms)
if (_dosync == false) {
_clk_count = 0;
// register how long the ClkHigh is high to evaluate if we are at the part wher clk goes high for 1-2 ms
while (digitalRead(_clk) == HIGH) {
_clk_count += 1;
delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
}
// if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
if (_clk_count >= 33 && _clk_count <= 67) {
_dosync = true;
}
// we are in sync and logging CLK-highs
} else {
// increment an integer to keep track of how many bits we have read.
_bits_count += 1;
_nextbit = true;
}
}
void _sync() {
unsigned int byte1 = 0;
unsigned int byte2 = 0;
unsigned int byte3 = 0;
_bits_count = 0;
while (_bits_count < 40); // skip the uninteresting 5 first bytes
_bits_count = 0;
while (_bits_count < 24) { // loop through the next 3 Bytes (6-8) and save byte 6 and 7 in Ba and Bb
if (_nextbit) {
if (_bits_count < 9) { // first Byte/8 bits in Ba
byte1 = byte1 << 1;
if (digitalRead(_miso) == HIGH) byte1 |= 1;
_nextbit = false;
} else if (_bits_count < 17) { // bit 9-16 is byte 7, stor in Bb
byte2 = byte2 << 1;
if (digitalRead(_miso) == HIGH) byte2 |= 1;
_nextbit = false;
}
}
}
if (byte2 != 3) { // if bit Bb is not 3, we have reached the important part, U is allready in Ba and Bb and next 8 Bytes will give us the Power.
// voltage = 2 * (Ba + Bb / 255)
_voltage = 2.0 * ((float) byte1 + (float) byte2 / 255.0);
// power:
_bits_count = 0;
while (_bits_count < 40); // skip the uninteresting 5 first bytes
_bits_count = 0;
byte1 = 0;
byte2 = 0;
byte3 = 0;
while (_bits_count < 24) { //store byte 6, 7 and 8 in Ba and Bb & Bc.
if (_nextbit) {
if (_bits_count < 9) {
byte1 = byte1 << 1;
if (digitalRead(_miso) == HIGH) byte1 |= 1;
_nextbit = false;
} else if (_bits_count < 17) {
byte2 = byte2 << 1;
if (digitalRead(_miso) == HIGH) byte2 |= 1;
_nextbit = false;
} else {
byte3 = byte3 << 1;
if (digitalRead(_miso) == HIGH) byte3 |= 1;
_nextbit = false;
}
}
}
if (_inverted) {
byte1 = 255 - byte1;
byte2 = 255 - byte2;
byte3 = 255 - byte3;
}
// power = (Ba*255+Bb+Bc/255)/2
_apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
_current = _apparent / _voltage;
_dosync = false;
}
// If Bb is not 3 or something else than 0, something is wrong!
if (byte2 == 0) _dosync = false;
}
// ---------------------------------------------------------------------
unsigned char _clk = 0;
unsigned char _miso = 0;
unsigned char _interrupt_gpio = GPIO_NONE;
bool _inverted = false;
volatile long _bits_count = 0;
volatile long _clk_count = 0;
volatile bool _dosync = false;
volatile bool _nextbit = true;
double _apparent = 0;
double _voltage = 0;
double _current = 0;
unsigned char _data[24];
};

+ 1
- 1
code/espurna/sensors/EventSensor.h View File

@ -22,7 +22,7 @@ class EventSensor : public BaseSensor {
}
~EventSensor() {
detachInterrupt(_gpio);
detach(_gpio);
}
// ---------------------------------------------------------------------


+ 30
- 1
code/html/custom.js View File

@ -28,6 +28,34 @@ function initMessages() {
messages[10] = "Session expired, please reload page...";
}
#define SENSOR_DHTXX_ID 0x01
#define SENSOR_DALLAS_ID 0x02
#define SENSOR_EMON_ANALOG_ID 0x03
#define SENSOR_EMON_ADC121_ID 0x04
#define SENSOR_EMON_ADS1X15_ID 0x05
#define SENSOR_HLW8012_ID 0x06
#define SENSOR_V9261F_ID 0x07
#define SENSOR_ECH1560_ID 0x08
#define SENSOR_ANALOG_ID 0x09
#define SENSOR_DIGITAL_ID 0x10
#define SENSOR_EVENTS_ID 0x11
#define SENSOR_PMSX003_ID 0x12
#define SENSOR_BMX280_ID 0x13
#define SENSOR_MHZ19_ID 0x14
#define SENSOR_SI7021_ID 0x15
#define SENSOR_SHT3X_I2C_ID 0x16
function sensorName(id) {
var names = [
"DHT", "Dallas", "Emon Analog", "Emon ADC121", "Emon ADS1X15",
"HLW8012", "V9261F", "ECH1560", "Analog", "Digital",
"Events", "PMSX003", "BMX280", "MHZ19", "SI7021",
"SHT3X I2C"
];
if (1 <= id && id <= names.length) return names[id-1];
return null;
}
function magnitudeType(type) {
var types = [
"Temperature", "Humidity", "Pressure",
@ -42,7 +70,8 @@ function magnitudeType(type) {
function magnitudeError(error) {
var errors = [
"OK", "Out of range", "Warming up", "Timeout", "Wrong ID", "CRC Error"
"OK", "Out of Range", "Warming Up", "Timeout", "Wrong ID",
"CRC Error", "I2C Error"
];
if (0 <= error && error < errors.length) return errors[error];
return "Error " + error;


Loading…
Cancel
Save