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.

395 lines
12 KiB

  1. // -----------------------------------------------------------------------------
  2. // CSE7766 based power monitor
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // http://www.chipsea.com/UploadFiles/2017/08/11144342F01B5662.pdf
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && CSE7766_SUPPORT
  7. #pragma once
  8. #include "Arduino.h"
  9. #include "BaseSensor.h"
  10. #include <SoftwareSerial.h>
  11. class CSE7766Sensor : public BaseSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. CSE7766Sensor(): BaseSensor(), _data() {
  17. _count = 7;
  18. _sensor_id = SENSOR_CSE7766_ID;
  19. }
  20. ~CSE7766Sensor() {
  21. if (_serial) delete _serial;
  22. }
  23. // ---------------------------------------------------------------------
  24. void setRX(unsigned char pin_rx) {
  25. if (_pin_rx == pin_rx) return;
  26. _pin_rx = pin_rx;
  27. _dirty = true;
  28. }
  29. void setInverted(bool inverted) {
  30. if (_inverted == inverted) return;
  31. _inverted = inverted;
  32. _dirty = true;
  33. }
  34. // ---------------------------------------------------------------------
  35. unsigned char getRX() {
  36. return _pin_rx;
  37. }
  38. bool getInverted() {
  39. return _inverted;
  40. }
  41. // ---------------------------------------------------------------------
  42. void expectedCurrent(double expected) {
  43. if ((expected > 0) && (_current > 0)) {
  44. _ratioC = _ratioC * (expected / _current);
  45. }
  46. }
  47. void expectedVoltage(unsigned int expected) {
  48. if ((expected > 0) && (_voltage > 0)) {
  49. _ratioV = _ratioV * (expected / _voltage);
  50. }
  51. }
  52. void expectedPower(unsigned int expected) {
  53. if ((expected > 0) && (_active > 0)) {
  54. _ratioP = _ratioP * (expected / _active);
  55. }
  56. }
  57. void setCurrentRatio(double value) {
  58. _ratioC = value;
  59. };
  60. void setVoltageRatio(double value) {
  61. _ratioV = value;
  62. };
  63. void setPowerRatio(double value) {
  64. _ratioP = value;
  65. };
  66. double getCurrentRatio() {
  67. return _ratioC;
  68. };
  69. double getVoltageRatio() {
  70. return _ratioV;
  71. };
  72. double getPowerRatio() {
  73. return _ratioP;
  74. };
  75. void resetRatios() {
  76. _ratioC = _ratioV = _ratioP = 1.0;
  77. }
  78. void resetEnergy(double value = 0) {
  79. _energy = value;
  80. }
  81. // ---------------------------------------------------------------------
  82. // Sensor API
  83. // ---------------------------------------------------------------------
  84. // Initialization method, must be idempotent
  85. void begin() {
  86. if (!_dirty) return;
  87. if (_serial) delete _serial;
  88. if (1 == _pin_rx) {
  89. Serial.begin(CSE7766_BAUDRATE);
  90. } else {
  91. _serial = new SoftwareSerial(_pin_rx, SW_SERIAL_UNUSED_PIN, _inverted, 32);
  92. _serial->enableIntTx(false);
  93. _serial->begin(CSE7766_BAUDRATE);
  94. }
  95. _ready = true;
  96. _dirty = false;
  97. }
  98. // Descriptive name of the sensor
  99. String description() {
  100. char buffer[28];
  101. if (1 == _pin_rx) {
  102. snprintf(buffer, sizeof(buffer), "CSE7766 @ HwSerial");
  103. } else {
  104. snprintf(buffer, sizeof(buffer), "CSE7766 @ SwSerial(%u,NULL)", _pin_rx);
  105. }
  106. return String(buffer);
  107. }
  108. // Descriptive name of the slot # index
  109. String slot(unsigned char index) {
  110. return description();
  111. };
  112. // Address of the sensor (it could be the GPIO or I2C address)
  113. String address(unsigned char index) {
  114. return String(_pin_rx);
  115. }
  116. // Loop-like method, call it in your main loop
  117. void tick() {
  118. _read();
  119. }
  120. // Type for slot # index
  121. unsigned char type(unsigned char index) {
  122. if (index == 0) return MAGNITUDE_CURRENT;
  123. if (index == 1) return MAGNITUDE_VOLTAGE;
  124. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  125. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  126. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  127. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  128. if (index == 6) return MAGNITUDE_ENERGY;
  129. return MAGNITUDE_NONE;
  130. }
  131. // Current value for slot # index
  132. double value(unsigned char index) {
  133. if (index == 0) return _current;
  134. if (index == 1) return _voltage;
  135. if (index == 2) return _active;
  136. if (index == 3) return _reactive;
  137. if (index == 4) return _voltage * _current;
  138. if (index == 5) return ((_voltage > 0) && (_current > 0)) ? 100 * _active / _voltage / _current : 100;
  139. if (index == 6) return _energy;
  140. return 0;
  141. }
  142. protected:
  143. // ---------------------------------------------------------------------
  144. // Protected
  145. // ---------------------------------------------------------------------
  146. /**
  147. * "
  148. * Checksum is the sum of all data
  149. * except for packet header and packet tail lowering by 8bit (...)
  150. * "
  151. * @return bool
  152. */
  153. bool _checksum() {
  154. unsigned char checksum = 0;
  155. for (unsigned char i = 2; i < 23; i++) {
  156. checksum += _data[i];
  157. }
  158. return checksum == _data[23];
  159. }
  160. void _process() {
  161. // Sample data:
  162. // 55 5A 02 E9 50 00 03 31 00 3E 9E 00 0D 30 4F 44 F8 00 12 65 F1 81 76 72 (w/ load)
  163. // F2 5A 02 E9 50 00 03 2B 00 3E 9E 02 D7 7C 4F 44 F8 CF A5 5D E1 B3 2A B4 (w/o load)
  164. #if SENSOR_DEBUG
  165. DEBUG_MSG("[SENSOR] CSE7766: _process: ");
  166. for (byte i=0; i<24; i++) DEBUG_MSG("%02X ", _data[i]);
  167. DEBUG_MSG("\n");
  168. #endif
  169. // Checksum
  170. if (!_checksum()) {
  171. _error = SENSOR_ERROR_CRC;
  172. #if SENSOR_DEBUG
  173. DEBUG_MSG("[SENSOR] CSE7766: Checksum error\n");
  174. #endif
  175. return;
  176. }
  177. // Calibration
  178. if (0xAA == _data[0]) {
  179. _error = SENSOR_ERROR_CALIBRATION;
  180. #if SENSOR_DEBUG
  181. DEBUG_MSG("[SENSOR] CSE7766: Chip not calibrated\n");
  182. #endif
  183. return;
  184. }
  185. if ((_data[0] & 0xFC) > 0xF0) {
  186. _error = SENSOR_ERROR_OTHER;
  187. #if SENSOR_DEBUG
  188. if (0xF1 == _data[0] & 0xF1) DEBUG_MSG("[SENSOR] CSE7766: Abnormal coefficient storage area\n");
  189. if (0xF2 == _data[0] & 0xF2) DEBUG_MSG("[SENSOR] CSE7766: Power cycle exceeded range\n");
  190. if (0xF4 == _data[0] & 0xF4) DEBUG_MSG("[SENSOR] CSE7766: Current cycle exceeded range\n");
  191. if (0xF8 == _data[0] & 0xF8) DEBUG_MSG("[SENSOR] CSE7766: Voltage cycle exceeded range\n");
  192. #endif
  193. return;
  194. }
  195. // Calibration coefficients
  196. unsigned long _coefV = (_data[2] << 16 | _data[3] << 8 | _data[4] ); // 190770
  197. unsigned long _coefC = (_data[8] << 16 | _data[9] << 8 | _data[10]); // 16030
  198. unsigned long _coefP = (_data[14] << 16 | _data[15] << 8 | _data[16]); // 5195000
  199. // Adj: this looks like a sampling report
  200. uint8_t adj = _data[20]; // F1 11110001
  201. // Calculate voltage
  202. _voltage = 0;
  203. if ((adj & 0x40) == 0x40) {
  204. unsigned long voltage_cycle = _data[5] << 16 | _data[6] << 8 | _data[7]; // 817
  205. _voltage = _ratioV * _coefV / voltage_cycle / CSE7766_V2R; // 190700 / 817 = 233.41
  206. }
  207. // Calculate power
  208. _active = 0;
  209. if ((adj & 0x10) == 0x10) {
  210. if ((_data[0] & 0xF2) != 0xF2) {
  211. unsigned long power_cycle = _data[17] << 16 | _data[18] << 8 | _data[19]; // 4709
  212. _active = _ratioP * _coefP / power_cycle / CSE7766_V1R / CSE7766_V2R; // 5195000 / 4709 = 1103.20
  213. }
  214. }
  215. // Calculate current
  216. _current = 0;
  217. if ((adj & 0x20) == 0x20) {
  218. if (_active > 0) {
  219. unsigned long current_cycle = _data[11] << 16 | _data[12] << 8 | _data[13]; // 3376
  220. _current = _ratioC * _coefC / current_cycle / CSE7766_V1R; // 16030 / 3376 = 4.75
  221. }
  222. }
  223. // Calculate reactive power
  224. _reactive = 0;
  225. unsigned int active = _active;
  226. unsigned int apparent = _voltage * _current;
  227. if (apparent > active) {
  228. _reactive = sqrt(apparent * apparent - active * active);
  229. } else {
  230. _reactive = 0;
  231. }
  232. // Calculate energy
  233. unsigned int difference;
  234. static unsigned int cf_pulses_last = 0;
  235. unsigned int cf_pulses = _data[21] << 8 | _data[22];
  236. if (0 == cf_pulses_last) cf_pulses_last = cf_pulses;
  237. if (cf_pulses < cf_pulses_last) {
  238. difference = cf_pulses + (0xFFFF - cf_pulses_last) + 1;
  239. } else {
  240. difference = cf_pulses - cf_pulses_last;
  241. }
  242. _energy += difference * (float) _coefP / 1000000.0;
  243. cf_pulses_last = cf_pulses;
  244. }
  245. void _read() {
  246. _error = SENSOR_ERROR_OK;
  247. static unsigned char index = 0;
  248. static unsigned long last = millis();
  249. while (_serial_available()) {
  250. // A 24 bytes message takes ~55ms to go through at 4800 bps
  251. // Reset counter if more than 1000ms have passed since last byte.
  252. if (millis() - last > CSE7766_SYNC_INTERVAL) index = 0;
  253. last = millis();
  254. uint8_t byte = _serial_read();
  255. // first byte must be 0x55 or 0xF?
  256. if (0 == index) {
  257. if ((0x55 != byte) && (byte < 0xF0)) {
  258. continue;
  259. }
  260. // second byte must be 0x5A
  261. } else if (1 == index) {
  262. if (0x5A != byte) {
  263. index = 0;
  264. continue;
  265. }
  266. }
  267. _data[index++] = byte;
  268. if (index > 23) {
  269. _serial_flush();
  270. break;
  271. }
  272. }
  273. // Process packet
  274. if (24 == index) {
  275. _process();
  276. index = 0;
  277. }
  278. }
  279. // ---------------------------------------------------------------------
  280. bool _serial_available() {
  281. if (1 == _pin_rx) {
  282. return Serial.available();
  283. } else {
  284. return _serial->available();
  285. }
  286. }
  287. void _serial_flush() {
  288. if (1 == _pin_rx) {
  289. return Serial.flush();
  290. } else {
  291. return _serial->flush();
  292. }
  293. }
  294. uint8_t _serial_read() {
  295. if (1 == _pin_rx) {
  296. return Serial.read();
  297. } else {
  298. return _serial->read();
  299. }
  300. }
  301. // ---------------------------------------------------------------------
  302. unsigned int _pin_rx = CSE7766_PIN;
  303. bool _inverted = CSE7766_PIN_INVERSE;
  304. SoftwareSerial * _serial = NULL;
  305. double _active = 0;
  306. double _reactive = 0;
  307. double _voltage = 0;
  308. double _current = 0;
  309. double _energy = 0;
  310. double _ratioV = 1.0;
  311. double _ratioC = 1.0;
  312. double _ratioP = 1.0;
  313. unsigned char _data[24];
  314. };
  315. #endif // SENSOR_SUPPORT && CSE7766_SUPPORT