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.

330 lines
11 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && HLW8012_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <ESP8266WiFi.h>
  10. #include <HLW8012.h>
  11. class HLW8012Sensor : public BaseSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. HLW8012Sensor(): BaseSensor() {
  17. _count = 7;
  18. _sensor_id = SENSOR_HLW8012_ID;
  19. _hlw8012 = new HLW8012();
  20. }
  21. ~HLW8012Sensor() {
  22. _enableInterrupts(false);
  23. delete _hlw8012;
  24. }
  25. void expectedCurrent(double expected) {
  26. _hlw8012->expectedCurrent(expected);
  27. }
  28. void expectedVoltage(unsigned int expected) {
  29. _hlw8012->expectedVoltage(expected);
  30. }
  31. void expectedPower(unsigned int expected) {
  32. _hlw8012->expectedActivePower(expected);
  33. }
  34. void resetRatios() {
  35. _hlw8012->resetMultipliers();
  36. }
  37. void resetEnergy(double value = 0) {
  38. _energy_offset = value;
  39. _hlw8012->resetEnergy();
  40. }
  41. // ---------------------------------------------------------------------
  42. void setSEL(unsigned char sel) {
  43. if (_sel == sel) return;
  44. _sel = sel;
  45. _dirty = true;
  46. }
  47. void setCF(unsigned char cf) {
  48. if (_cf == cf) return;
  49. _cf = cf;
  50. _dirty = true;
  51. }
  52. void setCF1(unsigned char cf1) {
  53. if (_cf1 == cf1) return;
  54. _cf1 = cf1;
  55. _dirty = true;
  56. }
  57. void setSELCurrent(bool value) {
  58. _sel_current = value;
  59. }
  60. void setCurrentRatio(double value) {
  61. _hlw8012->setCurrentMultiplier(value);
  62. };
  63. void setVoltageRatio(double value) {
  64. _hlw8012->setVoltageMultiplier(value);
  65. };
  66. void setPowerRatio(double value) {
  67. _hlw8012->setPowerMultiplier(value);
  68. };
  69. // ---------------------------------------------------------------------
  70. unsigned char getSEL() {
  71. return _sel;
  72. }
  73. unsigned char getCF() {
  74. return _cf;
  75. }
  76. unsigned char getCF1() {
  77. return _cf1;
  78. }
  79. unsigned char getSELCurrent() {
  80. return _sel_current;
  81. }
  82. double getCurrentRatio() {
  83. return _hlw8012->getCurrentMultiplier();
  84. };
  85. double getVoltageRatio() {
  86. return _hlw8012->getVoltageMultiplier();
  87. };
  88. double getPowerRatio() {
  89. return _hlw8012->getPowerMultiplier();
  90. };
  91. // ---------------------------------------------------------------------
  92. // Sensors API
  93. // ---------------------------------------------------------------------
  94. // Initialization method, must be idempotent
  95. // Defined outside the class body
  96. void begin() {
  97. // Initialize HLW8012
  98. // 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);
  99. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  100. // * currentWhen is the value in sel_pin to select current sampling
  101. // * set use_interrupts to true to use interrupts to monitor pulse widths
  102. // * leave pulse_timeout to the default value, recommended when using interrupts
  103. #if HLW8012_USE_INTERRUPTS
  104. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, true);
  105. #else
  106. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, false, 1000000);
  107. #endif
  108. // These values are used to calculate current, voltage and power factors as per datasheet formula
  109. // These are the nominal values for the Sonoff POW resistors:
  110. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  111. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  112. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  113. _hlw8012->setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  114. // Handle interrupts
  115. #if HLW8012_USE_INTERRUPTS
  116. _enableInterrupts(true);
  117. #else
  118. _onconnect_handler = WiFi.onStationModeGotIP([this](WiFiEventStationModeGotIP ipInfo) {
  119. _enableInterrupts(true);
  120. });
  121. _ondisconnect_handler = WiFi.onStationModeDisconnected([this](WiFiEventStationModeDisconnected ipInfo) {
  122. _enableInterrupts(false);
  123. });
  124. #endif
  125. _ready = true;
  126. }
  127. // Descriptive name of the sensor
  128. String description() {
  129. char buffer[25];
  130. snprintf(buffer, sizeof(buffer), "HLW8012 @ GPIO(%u,%u,%u)", _sel, _cf, _cf1);
  131. return String(buffer);
  132. }
  133. // Descriptive name of the slot # index
  134. String slot(unsigned char index) {
  135. return description();
  136. };
  137. // Address of the sensor (it could be the GPIO or I2C address)
  138. String address(unsigned char index) {
  139. char buffer[10];
  140. snprintf(buffer, sizeof(buffer), "%u:%u:%u", _sel, _cf, _cf1);
  141. return String(buffer);
  142. }
  143. // Type for slot # index
  144. unsigned char type(unsigned char index) {
  145. if (index == 0) return MAGNITUDE_CURRENT;
  146. if (index == 1) return MAGNITUDE_VOLTAGE;
  147. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  148. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  149. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  150. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  151. if (index == 6) return MAGNITUDE_ENERGY;
  152. return MAGNITUDE_NONE;
  153. }
  154. // Current value for slot # index
  155. double value(unsigned char index) {
  156. if (index == 0) return _hlw8012->getCurrent();
  157. if (index == 1) return _hlw8012->getVoltage();
  158. if (index == 2) return _hlw8012->getActivePower();
  159. if (index == 3) return _hlw8012->getReactivePower();
  160. if (index == 4) return _hlw8012->getApparentPower();
  161. if (index == 5) return 100 * _hlw8012->getPowerFactor();
  162. if (index == 6) return (_energy_offset + _hlw8012->getEnergy());
  163. return 0;
  164. }
  165. // Toggle between current and voltage monitoring
  166. #if HLW8012_USE_INTERRUPTS == 0
  167. // Post-read hook (usually to reset things)
  168. void post() { _hlw8012->toggleMode(); }
  169. #endif // HLW8012_USE_INTERRUPTS == 0
  170. // Handle interrupt calls
  171. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  172. if (gpio == _cf) _hlw8012->cf_interrupt();
  173. if (gpio == _cf1) _hlw8012->cf1_interrupt();
  174. }
  175. protected:
  176. // ---------------------------------------------------------------------
  177. // Interrupt management
  178. // ---------------------------------------------------------------------
  179. void _attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode);
  180. void _detach(unsigned char gpio);
  181. void _enableInterrupts(bool value) {
  182. static unsigned char _interrupt_cf = GPIO_NONE;
  183. static unsigned char _interrupt_cf1 = GPIO_NONE;
  184. if (value) {
  185. if (_interrupt_cf != _cf) {
  186. if (_interrupt_cf != GPIO_NONE) _detach(_interrupt_cf);
  187. _attach(this, _cf, HLW8012_INTERRUPT_ON);
  188. _interrupt_cf = _cf;
  189. }
  190. if (_interrupt_cf1 != _cf1) {
  191. if (_interrupt_cf1 != GPIO_NONE) _detach(_interrupt_cf1);
  192. _attach(this, _cf1, HLW8012_INTERRUPT_ON);
  193. _interrupt_cf1 = _cf1;
  194. }
  195. } else {
  196. _detach(_cf);
  197. _detach(_cf1);
  198. _interrupt_cf = GPIO_NONE;
  199. _interrupt_cf1 = GPIO_NONE;
  200. }
  201. }
  202. // ---------------------------------------------------------------------
  203. unsigned char _sel = GPIO_NONE;
  204. unsigned char _cf = GPIO_NONE;
  205. unsigned char _cf1 = GPIO_NONE;
  206. bool _sel_current = true;
  207. double _energy_offset = 0;
  208. HLW8012 * _hlw8012 = NULL;
  209. #if HLW8012_USE_INTERRUPTS == 0
  210. WiFiEventHandler _onconnect_handler;
  211. WiFiEventHandler _ondisconnect_handler;
  212. #endif
  213. };
  214. // -----------------------------------------------------------------------------
  215. // Interrupt helpers
  216. // -----------------------------------------------------------------------------
  217. HLW8012Sensor * _hlw8012_sensor_instance[10] = {NULL};
  218. void ICACHE_RAM_ATTR _hlw8012_sensor_isr(unsigned char gpio) {
  219. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  220. if (_hlw8012_sensor_instance[index]) {
  221. _hlw8012_sensor_instance[index]->handleInterrupt(gpio);
  222. }
  223. }
  224. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_0() { _hlw8012_sensor_isr(0); }
  225. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_1() { _hlw8012_sensor_isr(1); }
  226. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_2() { _hlw8012_sensor_isr(2); }
  227. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_3() { _hlw8012_sensor_isr(3); }
  228. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_4() { _hlw8012_sensor_isr(4); }
  229. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_5() { _hlw8012_sensor_isr(5); }
  230. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_12() { _hlw8012_sensor_isr(12); }
  231. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_13() { _hlw8012_sensor_isr(13); }
  232. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_14() { _hlw8012_sensor_isr(14); }
  233. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_15() { _hlw8012_sensor_isr(15); }
  234. static void (*_hlw8012_sensor_isr_list[10])() = {
  235. _hlw8012_sensor_isr_0, _hlw8012_sensor_isr_1, _hlw8012_sensor_isr_2,
  236. _hlw8012_sensor_isr_3, _hlw8012_sensor_isr_4, _hlw8012_sensor_isr_5,
  237. _hlw8012_sensor_isr_12, _hlw8012_sensor_isr_13, _hlw8012_sensor_isr_14,
  238. _hlw8012_sensor_isr_15
  239. };
  240. void HLW8012Sensor::_attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode) {
  241. if (!gpioValid(gpio)) return;
  242. _detach(gpio);
  243. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  244. _hlw8012_sensor_instance[index] = instance;
  245. attachInterrupt(gpio, _hlw8012_sensor_isr_list[index], mode);
  246. #if SENSOR_DEBUG
  247. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt attached to %s\n"), gpio, instance->description().c_str());
  248. #endif
  249. }
  250. void HLW8012Sensor::_detach(unsigned char gpio) {
  251. if (!gpioValid(gpio)) return;
  252. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  253. if (_hlw8012_sensor_instance[index]) {
  254. detachInterrupt(gpio);
  255. #if SENSOR_DEBUG
  256. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt detached from %s\n"), gpio, _hlw8012_sensor_instance[index]->description().c_str());
  257. #endif
  258. _hlw8012_sensor_instance[index] = NULL;
  259. }
  260. }
  261. #endif // SENSOR_SUPPORT && HLW8012_SUPPORT