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.

272 lines
7.9 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // V9261F based power monitor
  3. // Copyright (C) 2017-2019 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 <SoftwareSerial.h>
  9. #include "BaseEmonSensor.h"
  10. extern "C" {
  11. #include "../libs/fs_math.h"
  12. }
  13. class V9261FSensor : public BaseEmonSensor {
  14. public:
  15. // ---------------------------------------------------------------------
  16. // Public
  17. // ---------------------------------------------------------------------
  18. V9261FSensor(): _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, -1, _inverted);
  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 description(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. if (index == 6) return MAGNITUDE_ENERGY;
  83. return MAGNITUDE_NONE;
  84. }
  85. // Current value for slot # index
  86. double value(unsigned char index) {
  87. if (index == 0) return _current;
  88. if (index == 1) return _voltage;
  89. if (index == 2) return _active;
  90. if (index == 3) return _reactive;
  91. if (index == 4) return _apparent;
  92. if (index == 5) return _apparent > 0 ? 100 * _active / _apparent : 100;
  93. if (index == 6) return _energy[0].asDouble();
  94. return 0;
  95. }
  96. protected:
  97. // ---------------------------------------------------------------------
  98. // Protected
  99. // ---------------------------------------------------------------------
  100. void _read() {
  101. static unsigned char state = 0;
  102. static unsigned long last = 0;
  103. static unsigned long ts = 0;
  104. static bool found = false;
  105. static unsigned char index = 0;
  106. if (state == 0) {
  107. while (_serial->available()) {
  108. _serial->flush();
  109. found = true;
  110. ts = millis();
  111. }
  112. if (found && (millis() - ts > V9261F_SYNC_INTERVAL)) {
  113. _serial->flush();
  114. index = 0;
  115. state = 1;
  116. }
  117. } else if (state == 1) {
  118. while (_serial->available()) {
  119. _serial->read();
  120. if (index++ >= 7) {
  121. _serial->flush();
  122. index = 0;
  123. state = 2;
  124. }
  125. }
  126. } else if (state == 2) {
  127. while (_serial->available()) {
  128. _data[index] = _serial->read();
  129. if (index++ >= 19) {
  130. _serial->flush();
  131. ts = millis();
  132. state = 3;
  133. }
  134. }
  135. } else if (state == 3) {
  136. if (_checksum()) {
  137. _active = (double) (
  138. (_data[3]) +
  139. (_data[4] << 8) +
  140. (_data[5] << 16) +
  141. (_data[6] << 24)
  142. ) / _ratioP;
  143. _reactive = (double) (
  144. (_data[7]) +
  145. (_data[8] << 8) +
  146. (_data[9] << 16) +
  147. (_data[10] << 24)
  148. ) / _ratioR;
  149. _voltage = (double) (
  150. (_data[11]) +
  151. (_data[12] << 8) +
  152. (_data[13] << 16) +
  153. (_data[14] << 24)
  154. ) / _ratioV;
  155. _current = (double) (
  156. (_data[15]) +
  157. (_data[16] << 8) +
  158. (_data[17] << 16) +
  159. (_data[18] << 24)
  160. ) / _ratioC;
  161. if (_active < 0) _active = 0;
  162. if (_reactive < 0) _reactive = 0;
  163. if (_voltage < 0) _voltage = 0;
  164. if (_current < 0) _current = 0;
  165. _apparent = fs_sqrt(_reactive * _reactive + _active * _active);
  166. if (last > 0) {
  167. _energy[0] += sensor::Ws {
  168. static_cast<uint32_t>(_active * (millis() / last) / 1000)
  169. };
  170. }
  171. last = millis();
  172. }
  173. ts = millis();
  174. index = 0;
  175. state = 4;
  176. } else if (state == 4) {
  177. while (_serial->available()) {
  178. _serial->flush();
  179. ts = millis();
  180. }
  181. if (millis() - ts > V9261F_SYNC_INTERVAL) {
  182. state = 1;
  183. }
  184. }
  185. }
  186. bool _checksum() {
  187. unsigned char checksum = 0;
  188. for (unsigned char i = 0; i < 19; i++) {
  189. checksum = checksum + _data[i];
  190. }
  191. checksum = ~checksum + 0x33;
  192. return checksum == _data[19];
  193. }
  194. // ---------------------------------------------------------------------
  195. unsigned int _pin_rx = V9261F_PIN;
  196. bool _inverted = V9261F_PIN_INVERSE;
  197. SoftwareSerial * _serial = NULL;
  198. double _active = 0;
  199. double _reactive = 0;
  200. double _voltage = 0;
  201. double _current = 0;
  202. double _apparent = 0;
  203. double _ratioP = V9261F_POWER_FACTOR;
  204. double _ratioC = V9261F_CURRENT_FACTOR;
  205. double _ratioV = V9261F_VOLTAGE_FACTOR;
  206. double _ratioR = V9261F_RPOWER_FACTOR;
  207. unsigned char _data[24];
  208. };
  209. #endif // SENSOR_SUPPORT && V9261F_SUPPORT