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.

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