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.

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