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.

259 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. if (_serial) delete _serial;
  47. _serial = new SoftwareSerial(_pin_rx, SW_SERIAL_UNUSED_PIN, _inverted, 32);
  48. _serial->enableIntTx(false);
  49. _serial->begin(V9261F_BAUDRATE);
  50. _ready = true;
  51. _dirty = false;
  52. }
  53. // Descriptive name of the sensor
  54. String description() {
  55. char buffer[28];
  56. snprintf(buffer, sizeof(buffer), "V9261F @ SwSerial(%u,NULL)", _pin_rx);
  57. return String(buffer);
  58. }
  59. // Descriptive name of the slot # index
  60. String slot(unsigned char index) {
  61. return description();
  62. };
  63. // Address of the sensor (it could be the GPIO or I2C address)
  64. String address(unsigned char index) {
  65. return String(_pin_rx);
  66. }
  67. // Loop-like method, call it in your main loop
  68. void tick() {
  69. _read();
  70. }
  71. // Type for slot # index
  72. unsigned char type(unsigned char index) {
  73. if (index == 0) return MAGNITUDE_CURRENT;
  74. if (index == 1) return MAGNITUDE_VOLTAGE;
  75. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  76. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  77. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  78. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  79. return MAGNITUDE_NONE;
  80. }
  81. // Current value for slot # index
  82. double value(unsigned char index) {
  83. if (index == 0) return _current;
  84. if (index == 1) return _voltage;
  85. if (index == 2) return _active;
  86. if (index == 3) return _reactive;
  87. if (index == 4) return _apparent;
  88. if (index == 5) return _apparent > 0 ? 100 * _active / _apparent : 100;
  89. return 0;
  90. }
  91. protected:
  92. // ---------------------------------------------------------------------
  93. // Protected
  94. // ---------------------------------------------------------------------
  95. void _read() {
  96. static unsigned char state = 0;
  97. static unsigned long last = 0;
  98. static bool found = false;
  99. static unsigned char index = 0;
  100. if (state == 0) {
  101. while (_serial->available()) {
  102. _serial->flush();
  103. found = true;
  104. last = millis();
  105. }
  106. if (found && (millis() - last > V9261F_SYNC_INTERVAL)) {
  107. _serial->flush();
  108. index = 0;
  109. state = 1;
  110. }
  111. } else if (state == 1) {
  112. while (_serial->available()) {
  113. _serial->read();
  114. if (index++ >= 7) {
  115. _serial->flush();
  116. index = 0;
  117. state = 2;
  118. }
  119. }
  120. } else if (state == 2) {
  121. while (_serial->available()) {
  122. _data[index] = _serial->read();
  123. if (index++ >= 19) {
  124. _serial->flush();
  125. last = millis();
  126. state = 3;
  127. }
  128. }
  129. } else if (state == 3) {
  130. if (_checksum()) {
  131. _active = (double) (
  132. (_data[3]) +
  133. (_data[4] << 8) +
  134. (_data[5] << 16) +
  135. (_data[6] << 24)
  136. ) / _ratioP;
  137. _reactive = (double) (
  138. (_data[7]) +
  139. (_data[8] << 8) +
  140. (_data[9] << 16) +
  141. (_data[10] << 24)
  142. ) / _ratioR;
  143. _voltage = (double) (
  144. (_data[11]) +
  145. (_data[12] << 8) +
  146. (_data[13] << 16) +
  147. (_data[14] << 24)
  148. ) / _ratioV;
  149. _current = (double) (
  150. (_data[15]) +
  151. (_data[16] << 8) +
  152. (_data[17] << 16) +
  153. (_data[18] << 24)
  154. ) / _ratioC;
  155. if (_active < 0) _active = 0;
  156. if (_reactive < 0) _reactive = 0;
  157. if (_voltage < 0) _voltage = 0;
  158. if (_current < 0) _current = 0;
  159. _apparent = sqrt(_reactive * _reactive + _active * _active);
  160. }
  161. last = millis();
  162. index = 0;
  163. state = 4;
  164. } else if (state == 4) {
  165. while (_serial->available()) {
  166. _serial->flush();
  167. last = millis();
  168. }
  169. if (millis() - last > V9261F_SYNC_INTERVAL) {
  170. state = 1;
  171. }
  172. }
  173. }
  174. bool _checksum() {
  175. unsigned char checksum = 0;
  176. for (unsigned char i = 0; i < 19; i++) {
  177. checksum = checksum + _data[i];
  178. }
  179. checksum = ~checksum + 0x33;
  180. return checksum == _data[19];
  181. }
  182. // ---------------------------------------------------------------------
  183. unsigned int _pin_rx = V9261F_PIN;
  184. bool _inverted = V9261F_PIN_INVERSE;
  185. SoftwareSerial * _serial = NULL;
  186. double _active = 0;
  187. double _reactive = 0;
  188. double _voltage = 0;
  189. double _current = 0;
  190. double _apparent = 0;
  191. double _ratioP = V9261F_POWER_FACTOR;
  192. double _ratioC = V9261F_CURRENT_FACTOR;
  193. double _ratioV = V9261F_VOLTAGE_FACTOR;
  194. double _ratioR = V9261F_RPOWER_FACTOR;
  195. unsigned char _data[24];
  196. };
  197. #endif // SENSOR_SUPPORT && V9261F_SUPPORT