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.

382 lines
12 KiB

  1. // -----------------------------------------------------------------------------
  2. // CSE7766 based power monitor
  3. // Copyright (C) 2018 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 = 6;
  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_APPARENT;
  126. if (index == 4) return MAGNITUDE_POWER_FACTOR;
  127. if (index == 5) return MAGNITUDE_ENERGY;
  128. return MAGNITUDE_NONE;
  129. }
  130. // Current value for slot # index
  131. double value(unsigned char index) {
  132. if (index == 0) return _current;
  133. if (index == 1) return _voltage;
  134. if (index == 2) return _active;
  135. if (index == 3) return _voltage * _current;
  136. if (index == 4) return ((_voltage > 0) && (_current > 0)) ? 100 * _active / _voltage / _current : 100;
  137. if (index == 5) return _energy;
  138. return 0;
  139. }
  140. protected:
  141. // ---------------------------------------------------------------------
  142. // Protected
  143. // ---------------------------------------------------------------------
  144. /**
  145. * "
  146. * Checksum is the sum of all data
  147. * except for packet header and packet tail lowering by 8bit (...)
  148. * "
  149. * @return bool
  150. */
  151. bool _checksum() {
  152. unsigned char checksum = 0;
  153. for (unsigned char i = 2; i < 23; i++) {
  154. checksum += _data[i];
  155. }
  156. return checksum == _data[23];
  157. }
  158. void _process() {
  159. // Sample data:
  160. // 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)
  161. // 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)
  162. #if SENSOR_DEBUG
  163. DEBUG_MSG("[SENSOR] CSE7766: _process: ");
  164. for (byte i=0; i<24; i++) DEBUG_MSG("%02X ", _data[i]);
  165. DEBUG_MSG("\n");
  166. #endif
  167. // Checksum
  168. if (!_checksum()) {
  169. _error = SENSOR_ERROR_CRC;
  170. #if SENSOR_DEBUG
  171. DEBUG_MSG("[SENSOR] CSE7766: Checksum error\n");
  172. #endif
  173. return;
  174. }
  175. // Calibration
  176. if (0xAA == _data[0]) {
  177. _error = SENSOR_ERROR_CALIBRATION;
  178. #if SENSOR_DEBUG
  179. DEBUG_MSG("[SENSOR] CSE7766: Chip not calibrated\n");
  180. #endif
  181. return;
  182. }
  183. if ((_data[0] & 0xFC) > 0xF0) {
  184. _error = SENSOR_ERROR_OTHER;
  185. #if SENSOR_DEBUG
  186. if (0xF1 == _data[0] & 0xF1) DEBUG_MSG("[SENSOR] CSE7766: Abnormal coefficient storage area\n");
  187. if (0xF2 == _data[0] & 0xF2) DEBUG_MSG("[SENSOR] CSE7766: Power cycle exceeded range\n");
  188. if (0xF4 == _data[0] & 0xF4) DEBUG_MSG("[SENSOR] CSE7766: Current cycle exceeded range\n");
  189. if (0xF8 == _data[0] & 0xF8) DEBUG_MSG("[SENSOR] CSE7766: Voltage cycle exceeded range\n");
  190. #endif
  191. return;
  192. }
  193. // Calibration coefficients
  194. unsigned long _coefV = (_data[2] << 16 | _data[3] << 8 | _data[4] ); // 190770
  195. unsigned long _coefC = (_data[8] << 16 | _data[9] << 8 | _data[10]); // 16030
  196. unsigned long _coefP = (_data[14] << 16 | _data[15] << 8 | _data[16]); // 5195000
  197. // Adj: this looks like a sampling report
  198. uint8_t adj = _data[20]; // F1 11110001
  199. // Calculate voltage
  200. _voltage = 0;
  201. if ((adj & 0x40) == 0x40) {
  202. unsigned long voltage_cycle = _data[5] << 16 | _data[6] << 8 | _data[7]; // 817
  203. _voltage = _ratioV * _coefV / voltage_cycle / CSE7766_V2R; // 190700 / 817 = 233.41
  204. }
  205. // Calculate power
  206. _active = 0;
  207. if ((adj & 0x10) == 0x10) {
  208. if ((_data[0] & 0xF2) != 0xF2) {
  209. unsigned long power_cycle = _data[17] << 16 | _data[18] << 8 | _data[19]; // 4709
  210. _active = _ratioP * _coefP / power_cycle / CSE7766_V1R / CSE7766_V2R; // 5195000 / 4709 = 1103.20
  211. }
  212. }
  213. // Calculate current
  214. _current = 0;
  215. if ((adj & 0x20) == 0x20) {
  216. if (_active > 0) {
  217. unsigned long current_cycle = _data[11] << 16 | _data[12] << 8 | _data[13]; // 3376
  218. _current = _ratioC * _coefC / current_cycle / CSE7766_V1R; // 16030 / 3376 = 4.75
  219. }
  220. }
  221. // Calculate energy
  222. unsigned int difference;
  223. static unsigned int cf_pulses_last = 0;
  224. unsigned int cf_pulses = _data[21] << 8 | _data[22];
  225. if (0 == cf_pulses_last) cf_pulses_last = cf_pulses;
  226. if (cf_pulses < cf_pulses_last) {
  227. difference = cf_pulses + (0xFFFF - cf_pulses_last) + 1;
  228. } else {
  229. difference = cf_pulses - cf_pulses_last;
  230. }
  231. _energy += difference * (float) _coefP / 1000000.0;
  232. cf_pulses_last = cf_pulses;
  233. }
  234. void _read() {
  235. _error = SENSOR_ERROR_OK;
  236. static unsigned char index = 0;
  237. static unsigned long last = millis();
  238. while (_serial_available()) {
  239. // A 24 bytes message takes ~55ms to go through at 4800 bps
  240. // Reset counter if more than 1000ms have passed since last byte.
  241. if (millis() - last > CSE7766_SYNC_INTERVAL) index = 0;
  242. last = millis();
  243. uint8_t byte = _serial_read();
  244. // first byte must be 0x55 or 0xF?
  245. if (0 == index) {
  246. if ((0x55 != byte) && (byte < 0xF0)) {
  247. continue;
  248. }
  249. // second byte must be 0x5A
  250. } else if (1 == index) {
  251. if (0x5A != byte) {
  252. index = 0;
  253. continue;
  254. }
  255. }
  256. _data[index++] = byte;
  257. if (index > 23) {
  258. _serial_flush();
  259. break;
  260. }
  261. }
  262. // Process packet
  263. if (24 == index) {
  264. _process();
  265. index = 0;
  266. }
  267. }
  268. // ---------------------------------------------------------------------
  269. bool _serial_available() {
  270. if (1 == _pin_rx) {
  271. return Serial.available();
  272. } else {
  273. return _serial->available();
  274. }
  275. }
  276. void _serial_flush() {
  277. if (1 == _pin_rx) {
  278. return Serial.flush();
  279. } else {
  280. return _serial->flush();
  281. }
  282. }
  283. uint8_t _serial_read() {
  284. if (1 == _pin_rx) {
  285. return Serial.read();
  286. } else {
  287. return _serial->read();
  288. }
  289. }
  290. // ---------------------------------------------------------------------
  291. unsigned int _pin_rx = CSE7766_PIN;
  292. bool _inverted = CSE7766_PIN_INVERSE;
  293. SoftwareSerial * _serial = NULL;
  294. double _active = 0;
  295. double _voltage = 0;
  296. double _current = 0;
  297. double _energy = 0;
  298. double _ratioV = 1.0;
  299. double _ratioC = 1.0;
  300. double _ratioP = 1.0;
  301. unsigned char _data[24];
  302. };
  303. #endif // SENSOR_SUPPORT && CSE7766_SUPPORT