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.

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