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.

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