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.

360 lines
12 KiB

  1. // -----------------------------------------------------------------------------
  2. // ECH1560 based power monitor
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && ECH1560_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "../debug.h"
  9. #include "BaseSensor.h"
  10. #include "BaseEmonSensor.h"
  11. class ECH1560Sensor : public BaseEmonSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. ECH1560Sensor(): _data() {
  17. _count = 3;
  18. _sensor_id = SENSOR_ECH1560_ID;
  19. }
  20. ~ECH1560Sensor() {
  21. _enableInterrupts(false);
  22. }
  23. // ---------------------------------------------------------------------
  24. void setCLK(unsigned char clk) {
  25. if (_clk == clk) return;
  26. _clk = clk;
  27. _dirty = true;
  28. }
  29. void setMISO(unsigned char miso) {
  30. if (_miso == miso) return;
  31. _miso = miso;
  32. _dirty = true;
  33. }
  34. void setInverted(bool inverted) {
  35. _inverted = inverted;
  36. }
  37. // ---------------------------------------------------------------------
  38. unsigned char getCLK() {
  39. return _clk;
  40. }
  41. unsigned char getMISO() {
  42. return _miso;
  43. }
  44. bool getInverted() {
  45. return _inverted;
  46. }
  47. // ---------------------------------------------------------------------
  48. // Sensor API
  49. // ---------------------------------------------------------------------
  50. // Initialization method, must be idempotent
  51. void begin() {
  52. if (!_dirty) return;
  53. pinMode(_clk, INPUT);
  54. pinMode(_miso, INPUT);
  55. _enableInterrupts(true);
  56. _dirty = false;
  57. _ready = true;
  58. }
  59. // Loop-like method, call it in your main loop
  60. void tick() {
  61. if (_dosync) _sync();
  62. }
  63. // Descriptive name of the sensor
  64. String description() {
  65. char buffer[35];
  66. snprintf(buffer, sizeof(buffer), "ECH1560 (CLK,SDO) @ GPIO(%u,%u)", _clk, _miso);
  67. return String(buffer);
  68. }
  69. // Descriptive name of the slot # index
  70. String description(unsigned char index) {
  71. return description();
  72. };
  73. // Address of the sensor (it could be the GPIO or I2C address)
  74. String address(unsigned char index) {
  75. char buffer[6];
  76. snprintf(buffer, sizeof(buffer), "%u:%u", _clk, _miso);
  77. return String(buffer);
  78. }
  79. // Type for slot # index
  80. unsigned char type(unsigned char index) {
  81. if (index == 0) return MAGNITUDE_CURRENT;
  82. if (index == 1) return MAGNITUDE_VOLTAGE;
  83. if (index == 2) return MAGNITUDE_POWER_APPARENT;
  84. if (index == 3) return MAGNITUDE_ENERGY;
  85. return MAGNITUDE_NONE;
  86. }
  87. // Current value for slot # index
  88. double value(unsigned char index) {
  89. if (index == 0) return _current;
  90. if (index == 1) return _voltage;
  91. if (index == 2) return _apparent;
  92. if (index == 3) return getEnergy();
  93. return 0;
  94. }
  95. void ICACHE_RAM_ATTR handleInterrupt(unsigned char) {
  96. // if we are trying to find the sync-time (CLK goes high for 1-2ms)
  97. if (_dosync == false) {
  98. _clk_count = 0;
  99. // register how long the ClkHigh is high to evaluate if we are at the part where clk goes high for 1-2 ms
  100. while (digitalRead(_clk) == HIGH) {
  101. _clk_count += 1;
  102. delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
  103. }
  104. // if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
  105. if (_clk_count >= 33 && _clk_count <= 67) {
  106. _dosync = true;
  107. }
  108. // we are in sync and logging CLK-highs
  109. } else {
  110. // increment an integer to keep track of how many bits we have read.
  111. _bits_count += 1;
  112. _nextbit = true;
  113. }
  114. }
  115. protected:
  116. // ---------------------------------------------------------------------
  117. // Interrupt management
  118. // ---------------------------------------------------------------------
  119. void _attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode);
  120. void _detach(unsigned char gpio);
  121. void _enableInterrupts(bool value) {
  122. static unsigned char _interrupt_clk = GPIO_NONE;
  123. if (value) {
  124. if (_interrupt_clk != _clk) {
  125. if (_interrupt_clk != GPIO_NONE) _detach(_interrupt_clk);
  126. _attach(this, _clk, RISING);
  127. _interrupt_clk = _clk;
  128. }
  129. } else if (_interrupt_clk != GPIO_NONE) {
  130. _detach(_interrupt_clk);
  131. _interrupt_clk = GPIO_NONE;
  132. }
  133. }
  134. // ---------------------------------------------------------------------
  135. // Protected
  136. // ---------------------------------------------------------------------
  137. void _sync() {
  138. unsigned int byte1 = 0;
  139. unsigned int byte2 = 0;
  140. unsigned int byte3 = 0;
  141. _bits_count = 0;
  142. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  143. _bits_count = 0;
  144. while (_bits_count < 24) { // loop through the next 3 Bytes (6-8) and save byte 6 and 7 in byte1 and byte2
  145. if (_nextbit) {
  146. if (_bits_count < 9) { // first Byte/8 bits in byte1
  147. byte1 = byte1 << 1;
  148. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  149. _nextbit = false;
  150. } else if (_bits_count < 17) { // bit 9-16 is byte 7, store in byte2
  151. byte2 = byte2 << 1;
  152. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  153. _nextbit = false;
  154. }
  155. }
  156. }
  157. if (byte2 != 3) { // if bit byte2 is not 3, we have reached the important part, U is allready in byte1 and byte2 and next 8 Bytes will give us the Power.
  158. // voltage = 2 * (byte1 + byte2 / 255)
  159. _voltage = 2.0 * ((float) byte1 + (float) byte2 / 255.0);
  160. // power:
  161. _bits_count = 0;
  162. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  163. _bits_count = 0;
  164. byte1 = 0;
  165. byte2 = 0;
  166. byte3 = 0;
  167. while (_bits_count < 24) { //store byte 6, 7 and 8 in byte1 and byte2 & byte3.
  168. if (_nextbit) {
  169. if (_bits_count < 9) {
  170. byte1 = byte1 << 1;
  171. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  172. _nextbit = false;
  173. } else if (_bits_count < 17) {
  174. byte2 = byte2 << 1;
  175. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  176. _nextbit = false;
  177. } else {
  178. byte3 = byte3 << 1;
  179. if (digitalRead(_miso) == HIGH) byte3 |= 1;
  180. _nextbit = false;
  181. }
  182. }
  183. }
  184. if (_inverted) {
  185. byte1 = 255 - byte1;
  186. byte2 = 255 - byte2;
  187. byte3 = 255 - byte3;
  188. }
  189. // power = (byte1*255+byte2+byte3/255)/2
  190. _apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
  191. _current = _apparent / _voltage;
  192. static unsigned long last = 0;
  193. if (last > 0) {
  194. _energy[0] += sensor::Ws {
  195. static_cast<uint32_t>(_apparent * (millis() - last) / 1000)
  196. };
  197. }
  198. last = millis();
  199. _dosync = false;
  200. }
  201. // If byte2 is not 3 or something else than 0, something is wrong!
  202. if (byte2 == 0) {
  203. _dosync = false;
  204. #if SENSOR_DEBUG
  205. DEBUG_MSG_P(PSTR("Nothing connected, or out of sync!\n"));
  206. #endif
  207. }
  208. }
  209. // ---------------------------------------------------------------------
  210. unsigned char _clk = 0;
  211. unsigned char _miso = 0;
  212. bool _inverted = false;
  213. volatile long _bits_count = 0;
  214. volatile long _clk_count = 0;
  215. volatile bool _dosync = false;
  216. volatile bool _nextbit = true;
  217. double _apparent = 0;
  218. double _voltage = 0;
  219. double _current = 0;
  220. unsigned char _data[24];
  221. };
  222. // -----------------------------------------------------------------------------
  223. // Interrupt helpers
  224. // -----------------------------------------------------------------------------
  225. ECH1560Sensor * _ech1560_sensor_instance[10] = {NULL};
  226. void ICACHE_RAM_ATTR _ech1560_sensor_isr(unsigned char gpio) {
  227. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  228. if (_ech1560_sensor_instance[index]) {
  229. _ech1560_sensor_instance[index]->handleInterrupt(gpio);
  230. }
  231. }
  232. void ICACHE_RAM_ATTR _ech1560_sensor_isr_0() { _ech1560_sensor_isr(0); }
  233. void ICACHE_RAM_ATTR _ech1560_sensor_isr_1() { _ech1560_sensor_isr(1); }
  234. void ICACHE_RAM_ATTR _ech1560_sensor_isr_2() { _ech1560_sensor_isr(2); }
  235. void ICACHE_RAM_ATTR _ech1560_sensor_isr_3() { _ech1560_sensor_isr(3); }
  236. void ICACHE_RAM_ATTR _ech1560_sensor_isr_4() { _ech1560_sensor_isr(4); }
  237. void ICACHE_RAM_ATTR _ech1560_sensor_isr_5() { _ech1560_sensor_isr(5); }
  238. void ICACHE_RAM_ATTR _ech1560_sensor_isr_12() { _ech1560_sensor_isr(12); }
  239. void ICACHE_RAM_ATTR _ech1560_sensor_isr_13() { _ech1560_sensor_isr(13); }
  240. void ICACHE_RAM_ATTR _ech1560_sensor_isr_14() { _ech1560_sensor_isr(14); }
  241. void ICACHE_RAM_ATTR _ech1560_sensor_isr_15() { _ech1560_sensor_isr(15); }
  242. static void (*_ech1560_sensor_isr_list[10])() = {
  243. _ech1560_sensor_isr_0, _ech1560_sensor_isr_1, _ech1560_sensor_isr_2,
  244. _ech1560_sensor_isr_3, _ech1560_sensor_isr_4, _ech1560_sensor_isr_5,
  245. _ech1560_sensor_isr_12, _ech1560_sensor_isr_13, _ech1560_sensor_isr_14,
  246. _ech1560_sensor_isr_15
  247. };
  248. void ECH1560Sensor::_attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode) {
  249. if (!gpioValid(gpio)) return;
  250. _detach(gpio);
  251. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  252. _ech1560_sensor_instance[index] = instance;
  253. attachInterrupt(gpio, _ech1560_sensor_isr_list[index], mode);
  254. #if SENSOR_DEBUG
  255. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  256. #endif
  257. }
  258. void ECH1560Sensor::_detach(unsigned char gpio) {
  259. if (!gpioValid(gpio)) return;
  260. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  261. if (_ech1560_sensor_instance[index]) {
  262. detachInterrupt(gpio);
  263. #if SENSOR_DEBUG
  264. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _ech1560_sensor_instance[index]->description().c_str());
  265. #endif
  266. _ech1560_sensor_instance[index] = NULL;
  267. }
  268. }
  269. #endif // SENSOR_SUPPORT && ECH1560_SUPPORT