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.

256 lines
8.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.h"
  8. #include <ESP8266WiFi.h>
  9. #include <HLW8012.h>
  10. class HLW8012Sensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. HLW8012Sensor(): BaseSensor() {
  16. _count = 7;
  17. _sensor_id = SENSOR_HLW8012_ID;
  18. _hlw8012 = new HLW8012();
  19. }
  20. ~HLW8012Sensor() {
  21. detach(_interrupt_cf);
  22. detach(_interrupt_cf1);
  23. }
  24. void expectedCurrent(double expected) {
  25. _hlw8012->expectedCurrent(expected);
  26. }
  27. void expectedVoltage(unsigned int expected) {
  28. _hlw8012->expectedVoltage(expected);
  29. }
  30. void expectedPower(unsigned int expected) {
  31. _hlw8012->expectedActivePower(expected);
  32. }
  33. void resetRatios() {
  34. _hlw8012->resetMultipliers();
  35. }
  36. // ---------------------------------------------------------------------
  37. void setSEL(unsigned char sel) {
  38. if (_sel == sel) return;
  39. _sel = sel;
  40. _dirty = true;
  41. }
  42. void setCF(unsigned char cf) {
  43. if (_cf == cf) return;
  44. _cf = cf;
  45. _dirty = true;
  46. }
  47. void setCF1(unsigned char cf1) {
  48. if (_cf1 == cf1) return;
  49. _cf1 = cf1;
  50. _dirty = true;
  51. }
  52. void setSELCurrent(bool value) {
  53. _sel_current = value;
  54. }
  55. void setCurrentRatio(double value) {
  56. _hlw8012->setCurrentMultiplier(value);
  57. };
  58. void setVoltageRatio(double value) {
  59. _hlw8012->setVoltageMultiplier(value);
  60. };
  61. void setPowerRatio(double value) {
  62. _hlw8012->setPowerMultiplier(value);
  63. };
  64. // ---------------------------------------------------------------------
  65. unsigned char getSEL() {
  66. return _sel;
  67. }
  68. unsigned char getCF() {
  69. return _cf;
  70. }
  71. unsigned char getCF1() {
  72. return _cf1;
  73. }
  74. unsigned char getSELCurrent() {
  75. return _sel_current;
  76. }
  77. double getCurrentRatio() {
  78. return _hlw8012->getCurrentMultiplier();
  79. };
  80. double getVoltageRatio() {
  81. return _hlw8012->getVoltageMultiplier();
  82. };
  83. double getPowerRatio() {
  84. return _hlw8012->getPowerMultiplier();
  85. };
  86. // ---------------------------------------------------------------------
  87. // Sensors API
  88. // ---------------------------------------------------------------------
  89. // Initialization method, must be idempotent
  90. // Defined outside the class body
  91. void begin() {
  92. // Initialize HLW8012
  93. // void begin(unsigned char cf_pin, unsigned char cf1_pin, unsigned char sel_pin, unsigned char currentWhen = HIGH, bool use_interrupts = false, unsigned long pulse_timeout = PULSE_TIMEOUT);
  94. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  95. // * currentWhen is the value in sel_pin to select current sampling
  96. // * set use_interrupts to true to use interrupts to monitor pulse widths
  97. // * leave pulse_timeout to the default value, recommended when using interrupts
  98. #if HLW8012_USE_INTERRUPTS
  99. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, true);
  100. #else
  101. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, false, 1000000);
  102. #endif
  103. // These values are used to calculate current, voltage and power factors as per datasheet formula
  104. // These are the nominal values for the Sonoff POW resistors:
  105. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  106. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  107. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  108. _hlw8012->setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  109. // Handle interrupts
  110. #if HLW8012_USE_INTERRUPTS
  111. _enable(true);
  112. #else
  113. _onconnect_handler = WiFi.onStationModeGotIP([this](WiFiEventStationModeGotIP ipInfo) {
  114. _enable(true);
  115. });
  116. _ondisconnect_handler = WiFi.onStationModeDisconnected([this](WiFiEventStationModeDisconnected ipInfo) {
  117. _enable(false);
  118. });
  119. #endif
  120. }
  121. // Descriptive name of the sensor
  122. String description() {
  123. char buffer[25];
  124. snprintf(buffer, sizeof(buffer), "HLW8012 @ GPIO(%i,%i,%i)", _sel, _cf, _cf1);
  125. return String(buffer);
  126. }
  127. // Type for slot # index
  128. magnitude_t type(unsigned char index) {
  129. _error = SENSOR_ERROR_OK;
  130. if (index == 0) return MAGNITUDE_CURRENT;
  131. if (index == 1) return MAGNITUDE_VOLTAGE;
  132. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  133. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  134. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  135. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  136. if (index == 6) return MAGNITUDE_ENERGY;
  137. _error = SENSOR_ERROR_OUT_OF_RANGE;
  138. return MAGNITUDE_NONE;
  139. }
  140. // Current value for slot # index
  141. double value(unsigned char index) {
  142. _error = SENSOR_ERROR_OK;
  143. if (index == 0) return _hlw8012->getCurrent();
  144. if (index == 1) return _hlw8012->getVoltage();
  145. if (index == 2) return _hlw8012->getActivePower();
  146. if (index == 3) return _hlw8012->getReactivePower();
  147. if (index == 4) return _hlw8012->getApparentPower();
  148. if (index == 5) return _hlw8012->getPowerFactor();
  149. if (index == 6) return _hlw8012->getEnergy();
  150. _error = SENSOR_ERROR_OUT_OF_RANGE;
  151. return 0;
  152. }
  153. // Post-read hook (usually to reset things)
  154. void post() {
  155. // Toggle between current and voltage monitoring
  156. #if (HLW8012_USE_INTERRUPTS == 0)
  157. _hlw8012->toggleMode();
  158. #endif // (HLW8012_USE_INTERRUPTS == 0)
  159. }
  160. // Handle interrupt calls
  161. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  162. if (gpio == _interrupt_cf) _hlw8012->cf_interrupt();
  163. if (gpio == _interrupt_cf1) _hlw8012->cf1_interrupt();
  164. }
  165. // Interrupt attach callback
  166. void attached(unsigned char gpio) {
  167. BaseSensor::attached(gpio);
  168. if (_cf == gpio) _interrupt_cf = gpio;
  169. if (_cf1 == gpio) _interrupt_cf1 = gpio;
  170. }
  171. // Interrupt detach callback
  172. void detached(unsigned char gpio) {
  173. BaseSensor::detached(gpio);
  174. if (_interrupt_cf == gpio) _interrupt_cf = GPIO_NONE;
  175. if (_interrupt_cf1 == gpio) _interrupt_cf1 = GPIO_NONE;
  176. }
  177. protected:
  178. // ---------------------------------------------------------------------
  179. // Protected
  180. // ---------------------------------------------------------------------
  181. void _enable(bool value) {
  182. if (value) {
  183. if (_interrupt_cf != _cf) {
  184. detach(_interrupt_cf);
  185. attach(this, _cf, CHANGE);
  186. }
  187. if (_interrupt_cf1 != _cf1) {
  188. detach(_interrupt_cf1);
  189. attach(this, _cf1, CHANGE);
  190. }
  191. } else {
  192. detach(_interrupt_cf);
  193. detach(_interrupt_cf1);
  194. }
  195. }
  196. // ---------------------------------------------------------------------
  197. unsigned char _sel;
  198. unsigned char _cf;
  199. unsigned char _cf1;
  200. bool _sel_current;
  201. HLW8012 * _hlw8012 = NULL;
  202. WiFiEventHandler _onconnect_handler;
  203. WiFiEventHandler _ondisconnect_handler;
  204. unsigned char _interrupt_cf = GPIO_NONE;
  205. unsigned char _interrupt_cf1 = GPIO_NONE;
  206. };