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.

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