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.

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