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.

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