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.

309 lines
11 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. _enableInterrupts(false);
  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. // Initialize HLW8012
  92. // 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);
  93. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  94. // * currentWhen is the value in sel_pin to select current sampling
  95. // * set use_interrupts to true to use interrupts to monitor pulse widths
  96. // * leave pulse_timeout to the default value, recommended when using interrupts
  97. #if HLW8012_USE_INTERRUPTS
  98. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, true);
  99. #else
  100. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, false, 1000000);
  101. #endif
  102. // These values are used to calculate current, voltage and power factors as per datasheet formula
  103. // These are the nominal values for the Sonoff POW resistors:
  104. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  105. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  106. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  107. _hlw8012->setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  108. // Handle interrupts
  109. #if HLW8012_USE_INTERRUPTS
  110. _enableInterrupts(true);
  111. #else
  112. _onconnect_handler = WiFi.onStationModeGotIP([this](WiFiEventStationModeGotIP ipInfo) {
  113. _enableInterrupts(true);
  114. });
  115. _ondisconnect_handler = WiFi.onStationModeDisconnected([this](WiFiEventStationModeDisconnected ipInfo) {
  116. _enableInterrupts(false);
  117. });
  118. #endif
  119. }
  120. // Descriptive name of the sensor
  121. String description() {
  122. char buffer[25];
  123. snprintf(buffer, sizeof(buffer), "HLW8012 @ GPIO(%i,%i,%i)", _sel, _cf, _cf1);
  124. return String(buffer);
  125. }
  126. // Type for slot # index
  127. magnitude_t type(unsigned char index) {
  128. _error = SENSOR_ERROR_OK;
  129. if (index == 0) return MAGNITUDE_CURRENT;
  130. if (index == 1) return MAGNITUDE_VOLTAGE;
  131. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  132. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  133. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  134. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  135. if (index == 6) return MAGNITUDE_ENERGY;
  136. _error = SENSOR_ERROR_OUT_OF_RANGE;
  137. return MAGNITUDE_NONE;
  138. }
  139. // Current value for slot # index
  140. double value(unsigned char index) {
  141. _error = SENSOR_ERROR_OK;
  142. if (index == 0) return _hlw8012->getCurrent();
  143. if (index == 1) return _hlw8012->getVoltage();
  144. if (index == 2) return _hlw8012->getActivePower();
  145. if (index == 3) return _hlw8012->getReactivePower();
  146. if (index == 4) return _hlw8012->getApparentPower();
  147. if (index == 5) return _hlw8012->getPowerFactor();
  148. if (index == 6) return _hlw8012->getEnergy();
  149. _error = SENSOR_ERROR_OUT_OF_RANGE;
  150. return 0;
  151. }
  152. // Toggle between current and voltage monitoring
  153. #if HLW8012_USE_INTERRUPTS == 0
  154. // Post-read hook (usually to reset things)
  155. void post() { _hlw8012->toggleMode(); }
  156. #endif // HLW8012_USE_INTERRUPTS == 0
  157. // Handle interrupt calls
  158. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  159. if (gpio == _cf) _hlw8012->cf_interrupt();
  160. if (gpio == _cf1) _hlw8012->cf1_interrupt();
  161. }
  162. protected:
  163. // ---------------------------------------------------------------------
  164. // Interrupt management
  165. // ---------------------------------------------------------------------
  166. void _attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode);
  167. void _detach(unsigned char gpio);
  168. void _enableInterrupts(bool value) {
  169. static unsigned char _interrupt_cf = GPIO_NONE;
  170. static unsigned char _interrupt_cf1 = GPIO_NONE;
  171. if (value) {
  172. if (_interrupt_cf != _cf) {
  173. if (_interrupt_cf != GPIO_NONE) _detach(_interrupt_cf);
  174. _attach(this, _cf, CHANGE);
  175. _interrupt_cf = _cf;
  176. }
  177. if (_interrupt_cf1 != _cf1) {
  178. if (_interrupt_cf1 != GPIO_NONE) _detach(_interrupt_cf1);
  179. _attach(this, _cf1, CHANGE);
  180. _interrupt_cf1 = _cf1;
  181. }
  182. } else {
  183. _detach(_cf);
  184. _detach(_cf1);
  185. _interrupt_cf = GPIO_NONE;
  186. _interrupt_cf1 = GPIO_NONE;
  187. }
  188. }
  189. // ---------------------------------------------------------------------
  190. unsigned char _sel;
  191. unsigned char _cf;
  192. unsigned char _cf1;
  193. bool _sel_current;
  194. HLW8012 * _hlw8012 = NULL;
  195. #if HLW8012_USE_INTERRUPTS == 0
  196. WiFiEventHandler _onconnect_handler;
  197. WiFiEventHandler _ondisconnect_handler;
  198. #endif
  199. };
  200. // -----------------------------------------------------------------------------
  201. // Interrupt helpers
  202. // -----------------------------------------------------------------------------
  203. HLW8012Sensor * _hlw8012_sensor_instance[10] = {NULL};
  204. void ICACHE_RAM_ATTR _hlw8012_sensor_isr(unsigned char gpio) {
  205. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  206. if (_hlw8012_sensor_instance[index]) {
  207. _hlw8012_sensor_instance[index]->handleInterrupt(gpio);
  208. }
  209. }
  210. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_0() { _hlw8012_sensor_isr(0); }
  211. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_1() { _hlw8012_sensor_isr(1); }
  212. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_2() { _hlw8012_sensor_isr(2); }
  213. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_3() { _hlw8012_sensor_isr(3); }
  214. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_4() { _hlw8012_sensor_isr(4); }
  215. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_5() { _hlw8012_sensor_isr(5); }
  216. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_12() { _hlw8012_sensor_isr(12); }
  217. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_13() { _hlw8012_sensor_isr(13); }
  218. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_14() { _hlw8012_sensor_isr(14); }
  219. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_15() { _hlw8012_sensor_isr(15); }
  220. static void (*_hlw8012_sensor_isr_list[10])() = {
  221. _hlw8012_sensor_isr_0, _hlw8012_sensor_isr_1, _hlw8012_sensor_isr_2,
  222. _hlw8012_sensor_isr_3, _hlw8012_sensor_isr_4, _hlw8012_sensor_isr_5,
  223. _hlw8012_sensor_isr_12, _hlw8012_sensor_isr_13, _hlw8012_sensor_isr_14,
  224. _hlw8012_sensor_isr_15
  225. };
  226. void HLW8012Sensor::_attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode) {
  227. if (!_validGPIO(gpio)) return;
  228. _detach(gpio);
  229. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  230. _hlw8012_sensor_instance[index] = instance;
  231. attachInterrupt(gpio, _hlw8012_sensor_isr_list[index], mode);
  232. #if SENSOR_DEBUG
  233. DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, instance->description().c_str());
  234. #endif
  235. }
  236. void HLW8012Sensor::_detach(unsigned char gpio) {
  237. if (!_validGPIO(gpio)) return;
  238. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  239. if (_hlw8012_sensor_instance[index]) {
  240. detachInterrupt(gpio);
  241. #if SENSOR_DEBUG
  242. DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, _hlw8012_sensor_instance[index]->description().c_str());
  243. #endif
  244. _hlw8012_sensor_instance[index] = NULL;
  245. }
  246. }