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.

246 lines
7.2 KiB

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