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.

310 lines
11 KiB

7 years ago
  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. delete _hlw8012;
  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. _enableInterrupts(true);
  112. #else
  113. _onconnect_handler = WiFi.onStationModeGotIP([this](WiFiEventStationModeGotIP ipInfo) {
  114. _enableInterrupts(true);
  115. });
  116. _ondisconnect_handler = WiFi.onStationModeDisconnected([this](WiFiEventStationModeDisconnected ipInfo) {
  117. _enableInterrupts(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 100 * _hlw8012->getPowerFactor();
  149. if (index == 6) return _hlw8012->getEnergy();
  150. _error = SENSOR_ERROR_OUT_OF_RANGE;
  151. return 0;
  152. }
  153. // Toggle between current and voltage monitoring
  154. #if HLW8012_USE_INTERRUPTS == 0
  155. // Post-read hook (usually to reset things)
  156. void post() { _hlw8012->toggleMode(); }
  157. #endif // HLW8012_USE_INTERRUPTS == 0
  158. // Handle interrupt calls
  159. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  160. if (gpio == _cf) _hlw8012->cf_interrupt();
  161. if (gpio == _cf1) _hlw8012->cf1_interrupt();
  162. }
  163. protected:
  164. // ---------------------------------------------------------------------
  165. // Interrupt management
  166. // ---------------------------------------------------------------------
  167. void _attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode);
  168. void _detach(unsigned char gpio);
  169. void _enableInterrupts(bool value) {
  170. static unsigned char _interrupt_cf = GPIO_NONE;
  171. static unsigned char _interrupt_cf1 = GPIO_NONE;
  172. if (value) {
  173. if (_interrupt_cf != _cf) {
  174. if (_interrupt_cf != GPIO_NONE) _detach(_interrupt_cf);
  175. _attach(this, _cf, CHANGE);
  176. _interrupt_cf = _cf;
  177. }
  178. if (_interrupt_cf1 != _cf1) {
  179. if (_interrupt_cf1 != GPIO_NONE) _detach(_interrupt_cf1);
  180. _attach(this, _cf1, CHANGE);
  181. _interrupt_cf1 = _cf1;
  182. }
  183. } else {
  184. _detach(_cf);
  185. _detach(_cf1);
  186. _interrupt_cf = GPIO_NONE;
  187. _interrupt_cf1 = GPIO_NONE;
  188. }
  189. }
  190. // ---------------------------------------------------------------------
  191. unsigned char _sel;
  192. unsigned char _cf;
  193. unsigned char _cf1;
  194. bool _sel_current;
  195. HLW8012 * _hlw8012 = NULL;
  196. #if HLW8012_USE_INTERRUPTS == 0
  197. WiFiEventHandler _onconnect_handler;
  198. WiFiEventHandler _ondisconnect_handler;
  199. #endif
  200. };
  201. // -----------------------------------------------------------------------------
  202. // Interrupt helpers
  203. // -----------------------------------------------------------------------------
  204. HLW8012Sensor * _hlw8012_sensor_instance[10] = {NULL};
  205. void ICACHE_RAM_ATTR _hlw8012_sensor_isr(unsigned char gpio) {
  206. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  207. if (_hlw8012_sensor_instance[index]) {
  208. _hlw8012_sensor_instance[index]->handleInterrupt(gpio);
  209. }
  210. }
  211. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_0() { _hlw8012_sensor_isr(0); }
  212. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_1() { _hlw8012_sensor_isr(1); }
  213. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_2() { _hlw8012_sensor_isr(2); }
  214. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_3() { _hlw8012_sensor_isr(3); }
  215. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_4() { _hlw8012_sensor_isr(4); }
  216. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_5() { _hlw8012_sensor_isr(5); }
  217. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_12() { _hlw8012_sensor_isr(12); }
  218. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_13() { _hlw8012_sensor_isr(13); }
  219. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_14() { _hlw8012_sensor_isr(14); }
  220. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_15() { _hlw8012_sensor_isr(15); }
  221. static void (*_hlw8012_sensor_isr_list[10])() = {
  222. _hlw8012_sensor_isr_0, _hlw8012_sensor_isr_1, _hlw8012_sensor_isr_2,
  223. _hlw8012_sensor_isr_3, _hlw8012_sensor_isr_4, _hlw8012_sensor_isr_5,
  224. _hlw8012_sensor_isr_12, _hlw8012_sensor_isr_13, _hlw8012_sensor_isr_14,
  225. _hlw8012_sensor_isr_15
  226. };
  227. void HLW8012Sensor::_attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode) {
  228. if (!gpioValid(gpio)) return;
  229. _detach(gpio);
  230. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  231. _hlw8012_sensor_instance[index] = instance;
  232. attachInterrupt(gpio, _hlw8012_sensor_isr_list[index], mode);
  233. #if SENSOR_DEBUG
  234. DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, instance->description().c_str());
  235. #endif
  236. }
  237. void HLW8012Sensor::_detach(unsigned char gpio) {
  238. if (!gpioValid(gpio)) return;
  239. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  240. if (_hlw8012_sensor_instance[index]) {
  241. detachInterrupt(gpio);
  242. #if SENSOR_DEBUG
  243. DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, _hlw8012_sensor_instance[index]->description().c_str());
  244. #endif
  245. _hlw8012_sensor_instance[index] = NULL;
  246. }
  247. }