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.

344 lines
12 KiB

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