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.

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