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.

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