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.

257 lines
7.5 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // V9261F based power monitor
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && V9261F_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <SoftwareSerial.h>
  10. class V9261FSensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. V9261FSensor(): BaseSensor(), _data() {
  16. _count = 6;
  17. _sensor_id = SENSOR_V9261F_ID;
  18. }
  19. ~V9261FSensor() {
  20. if (_serial) delete _serial;
  21. }
  22. // ---------------------------------------------------------------------
  23. void setRX(unsigned char pin_rx) {
  24. if (_pin_rx == pin_rx) return;
  25. _pin_rx = pin_rx;
  26. _dirty = true;
  27. }
  28. void setInverted(bool inverted) {
  29. if (_inverted == inverted) return;
  30. _inverted = inverted;
  31. _dirty = true;
  32. }
  33. // ---------------------------------------------------------------------
  34. unsigned char getRX() {
  35. return _pin_rx;
  36. }
  37. bool getInverted() {
  38. return _inverted;
  39. }
  40. // ---------------------------------------------------------------------
  41. // Sensor API
  42. // ---------------------------------------------------------------------
  43. // Initialization method, must be idempotent
  44. void begin() {
  45. if (!_dirty) return;
  46. _dirty = false;
  47. if (_serial) delete _serial;
  48. _serial = new SoftwareSerial(_pin_rx, SW_SERIAL_UNUSED_PIN, _inverted, 32);
  49. _serial->enableIntTx(false);
  50. _serial->begin(V9261F_BAUDRATE);
  51. }
  52. // Descriptive name of the sensor
  53. String description() {
  54. char buffer[28];
  55. snprintf(buffer, sizeof(buffer), "V9261F @ SwSerial(%u,NULL)", _pin_rx);
  56. return String(buffer);
  57. }
  58. // Descriptive name of the slot # index
  59. String slot(unsigned char index) {
  60. return description();
  61. };
  62. // Address of the sensor (it could be the GPIO or I2C address)
  63. String address(unsigned char index) {
  64. return String(_pin_rx);
  65. }
  66. // Loop-like method, call it in your main loop
  67. void tick() {
  68. _read();
  69. }
  70. // Type for slot # index
  71. unsigned char type(unsigned char index) {
  72. if (index == 0) return MAGNITUDE_CURRENT;
  73. if (index == 1) return MAGNITUDE_VOLTAGE;
  74. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  75. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  76. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  77. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  78. return MAGNITUDE_NONE;
  79. }
  80. // Current value for slot # index
  81. double value(unsigned char index) {
  82. if (index == 0) return _current;
  83. if (index == 1) return _voltage;
  84. if (index == 2) return _active;
  85. if (index == 3) return _reactive;
  86. if (index == 4) return _apparent;
  87. if (index == 5) return _apparent > 0 ? 100 * _active / _apparent : 100;
  88. return 0;
  89. }
  90. protected:
  91. // ---------------------------------------------------------------------
  92. // Protected
  93. // ---------------------------------------------------------------------
  94. void _read() {
  95. static unsigned char state = 0;
  96. static unsigned long last = 0;
  97. static bool found = false;
  98. static unsigned char index = 0;
  99. if (state == 0) {
  100. while (_serial->available()) {
  101. _serial->flush();
  102. found = true;
  103. last = millis();
  104. }
  105. if (found && (millis() - last > V9261F_SYNC_INTERVAL)) {
  106. _serial->flush();
  107. index = 0;
  108. state = 1;
  109. }
  110. } else if (state == 1) {
  111. while (_serial->available()) {
  112. _serial->read();
  113. if (index++ >= 7) {
  114. _serial->flush();
  115. index = 0;
  116. state = 2;
  117. }
  118. }
  119. } else if (state == 2) {
  120. while (_serial->available()) {
  121. _data[index] = _serial->read();
  122. if (index++ >= 19) {
  123. _serial->flush();
  124. last = millis();
  125. state = 3;
  126. }
  127. }
  128. } else if (state == 3) {
  129. if (_checksum()) {
  130. _active = (double) (
  131. (_data[3]) +
  132. (_data[4] << 8) +
  133. (_data[5] << 16) +
  134. (_data[6] << 24)
  135. ) / _ratioP;
  136. _reactive = (double) (
  137. (_data[7]) +
  138. (_data[8] << 8) +
  139. (_data[9] << 16) +
  140. (_data[10] << 24)
  141. ) / _ratioR;
  142. _voltage = (double) (
  143. (_data[11]) +
  144. (_data[12] << 8) +
  145. (_data[13] << 16) +
  146. (_data[14] << 24)
  147. ) / _ratioV;
  148. _current = (double) (
  149. (_data[15]) +
  150. (_data[16] << 8) +
  151. (_data[17] << 16) +
  152. (_data[18] << 24)
  153. ) / _ratioC;
  154. if (_active < 0) _active = 0;
  155. if (_reactive < 0) _reactive = 0;
  156. if (_voltage < 0) _voltage = 0;
  157. if (_current < 0) _current = 0;
  158. _apparent = sqrt(_reactive * _reactive + _active * _active);
  159. }
  160. last = millis();
  161. index = 0;
  162. state = 4;
  163. } else if (state == 4) {
  164. while (_serial->available()) {
  165. _serial->flush();
  166. last = millis();
  167. }
  168. if (millis() - last > V9261F_SYNC_INTERVAL) {
  169. state = 1;
  170. }
  171. }
  172. }
  173. bool _checksum() {
  174. unsigned char checksum = 0;
  175. for (unsigned char i = 0; i < 19; i++) {
  176. checksum = checksum + _data[i];
  177. }
  178. checksum = ~checksum + 0x33;
  179. return checksum == _data[19];
  180. }
  181. // ---------------------------------------------------------------------
  182. unsigned int _pin_rx = V9261F_PIN;
  183. bool _inverted = V9261F_PIN_INVERSE;
  184. SoftwareSerial * _serial = NULL;
  185. double _active = 0;
  186. double _reactive = 0;
  187. double _voltage = 0;
  188. double _current = 0;
  189. double _apparent = 0;
  190. double _ratioP = V9261F_POWER_FACTOR;
  191. double _ratioC = V9261F_CURRENT_FACTOR;
  192. double _ratioV = V9261F_VOLTAGE_FACTOR;
  193. double _ratioR = V9261F_RPOWER_FACTOR;
  194. unsigned char _data[24];
  195. };
  196. #endif // SENSOR_SUPPORT && V9261F_SUPPORT