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.

319 lines
8.8 KiB

  1. /*
  2. V9261F MODULE
  3. Support for V9261D-based power monitors
  4. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. /*
  7. #if V9261F_SUPPORT
  8. #include <SoftwareSerial.h>
  9. #include <ArduinoJson.h>
  10. SoftwareSerial * _v9261f_uart;
  11. bool _v9261f_enabled = false;
  12. bool _v9261f_ready = false;
  13. bool _v9261f_newdata = false;
  14. int _v9261f_power = 0;
  15. int _v9261f_rpower = 0;
  16. int _v9261f_voltage = 0;
  17. double _v9261f_current = 0;
  18. unsigned char _v9261f_data[24];
  19. // -----------------------------------------------------------------------------
  20. // PRIVATE
  21. // -----------------------------------------------------------------------------
  22. void v9261fRead() {
  23. static unsigned char state = 0;
  24. static unsigned long last = 0;
  25. static bool found = false;
  26. static unsigned char index = 0;
  27. if (state == 0) {
  28. while (_v9261f_uart->available()) {
  29. _v9261f_uart->flush();
  30. found = true;
  31. last = millis();
  32. }
  33. if (found && (millis() - last > V9261F_SYNC_INTERVAL)) {
  34. _v9261f_uart->flush();
  35. index = 0;
  36. state = 1;
  37. }
  38. } else if (state == 1) {
  39. while (_v9261f_uart->available()) {
  40. _v9261f_uart->read();
  41. if (index++ >= 7) {
  42. _v9261f_uart->flush();
  43. index = 0;
  44. state = 2;
  45. }
  46. }
  47. } else if (state == 2) {
  48. while (_v9261f_uart->available()) {
  49. _v9261f_data[index] = _v9261f_uart->read();
  50. if (index++ >= 19) {
  51. _v9261f_uart->flush();
  52. last = millis();
  53. state = 3;
  54. }
  55. }
  56. } else if (state == 3) {
  57. /*
  58. for (unsigned char i=0; i<index;i++) {
  59. DEBUG_MSG("%02X ", _v9261f_data[i]);
  60. }
  61. DEBUG_MSG("\n");
  62. */
  63. if (checksumOK()) {
  64. _v9261f_power = (double) (
  65. (_v9261f_data[3]) +
  66. (_v9261f_data[4] << 8) +
  67. (_v9261f_data[5] << 16) +
  68. (_v9261f_data[6] << 24)
  69. ) / V9261F_POWER_FACTOR;
  70. _v9261f_rpower = (double) (
  71. (_v9261f_data[7]) +
  72. (_v9261f_data[8] << 8) +
  73. (_v9261f_data[9] << 16) +
  74. (_v9261f_data[10] << 24)
  75. ) / V9261F_RPOWER_FACTOR;
  76. _v9261f_voltage = (double) (
  77. (_v9261f_data[11]) +
  78. (_v9261f_data[12] << 8) +
  79. (_v9261f_data[13] << 16) +
  80. (_v9261f_data[14] << 24)
  81. ) / V9261F_VOLTAGE_FACTOR;
  82. _v9261f_current = (double) (
  83. (_v9261f_data[15]) +
  84. (_v9261f_data[16] << 8) +
  85. (_v9261f_data[17] << 16) +
  86. (_v9261f_data[18] << 24)
  87. ) / V9261F_CURRENT_FACTOR;
  88. _v9261f_newdata = true;
  89. /*
  90. DEBUG_MSG_P(PSTR("[V9261F] W = %lu\n"), _v9261f_power);
  91. DEBUG_MSG_P(PSTR("[V9261F] R = %lu\n"), _v9261f_rpower);
  92. DEBUG_MSG_P(PSTR("[V9261F] V = %lu\n"), _v9261f_voltage);
  93. DEBUG_MSG_P(PSTR("[V9261F] C = %lu\n"), _v9261f_current);
  94. */
  95. }
  96. last = millis();
  97. index = 0;
  98. state = 4;
  99. } else if (state == 4) {
  100. while (_v9261f_uart->available()) {
  101. _v9261f_uart->flush();
  102. last = millis();
  103. }
  104. if (millis() - last > V9261F_SYNC_INTERVAL) {
  105. state = 1;
  106. }
  107. }
  108. }
  109. boolean checksumOK() {
  110. unsigned char checksum = 0;
  111. for (unsigned char i = 0; i < 19; i++) {
  112. checksum = checksum + _v9261f_data[i];
  113. }
  114. checksum = ~checksum + 0x33;
  115. return checksum == _v9261f_data[19];
  116. }
  117. // -----------------------------------------------------------------------------
  118. // HAL
  119. // -----------------------------------------------------------------------------
  120. unsigned int getActivePower() {
  121. return _v9261f_power;
  122. }
  123. unsigned int getReactivePower() {
  124. return _v9261f_rpower;
  125. }
  126. unsigned int getApparentPower() {
  127. return sqrt(_v9261f_rpower * _v9261f_rpower + _v9261f_power * _v9261f_power);
  128. }
  129. unsigned int getVoltage() {
  130. return _v9261f_voltage;
  131. }
  132. double getCurrent() {
  133. return _v9261f_current;
  134. }
  135. double getPowerFactor() {
  136. unsigned int apparent = getApparentPower();
  137. if (apparent > 0) return getActivePower() / getApparentPower();
  138. return 1;
  139. }
  140. // -----------------------------------------------------------------------------
  141. void v9261fSetup() {
  142. _v9261f_uart = new SoftwareSerial(V9261F_PIN, SW_SERIAL_UNUSED_PIN, V9261F_PIN_INVERSE, 256);
  143. _v9261f_uart->begin(V9261F_BAUDRATE);
  144. // API definitions
  145. #if WEB_SUPPORT
  146. apiRegister(HLW8012_POWER_TOPIC, HLW8012_POWER_TOPIC, [](char * buffer, size_t len) {
  147. snprintf_P(buffer, len, PSTR("%d"), _v9261f_power);
  148. });
  149. apiRegister(HLW8012_CURRENT_TOPIC, HLW8012_CURRENT_TOPIC, [](char * buffer, size_t len) {
  150. dtostrf(_v9261f_current, len-1, 3, buffer);
  151. });
  152. apiRegister(HLW8012_VOLTAGE_TOPIC, HLW8012_VOLTAGE_TOPIC, [](char * buffer, size_t len) {
  153. snprintf_P(buffer, len, PSTR("%d"), _v9261f_voltage);
  154. });
  155. #endif // WEB_SUPPORT
  156. }
  157. void v9261fLoop() {
  158. static int sum_power = 0;
  159. static int sum_rpower = 0;
  160. static int sum_voltage = 0;
  161. static double sum_current = 0;
  162. static int count = 0;
  163. // Sniff data in the UART interface
  164. v9261fRead();
  165. // Do we have new data?
  166. if (_v9261f_newdata) {
  167. _v9261f_newdata = false;
  168. sum_power += getActivePower();
  169. sum_rpower += getReactivePower();
  170. sum_voltage += getVoltage();
  171. sum_current += getCurrent();
  172. count++;
  173. #if WEB_SUPPORT
  174. {
  175. DynamicJsonBuffer jsonBuffer;
  176. JsonObject& root = jsonBuffer.createObject();
  177. char buf_current[10];
  178. dtostrf(getCurrent(), 6, 3, buf_current);
  179. root["powVisible"] = 1;
  180. root["powActivePower"] = getActivePower();
  181. root["powCurrent"] = String(ltrim(buf_current));
  182. root["powVoltage"] = getVoltage();
  183. root["powApparentPower"] = getApparentPower();
  184. root["powReactivePower"] = getReactivePower();
  185. root["powPowerFactor"] = 100 * getPowerFactor();
  186. String output;
  187. root.printTo(output);
  188. wsSend(output.c_str());
  189. }
  190. #endif
  191. }
  192. // Do we have to report?
  193. static unsigned long last = 0;
  194. if ((count == 0) || (millis() - last < V9261F_REPORT_INTERVAL)) return;
  195. last = millis();
  196. {
  197. unsigned int power = sum_power / count;
  198. unsigned int reactive = sum_rpower / count;
  199. unsigned int voltage = sum_voltage / count;
  200. double current = sum_current / count;
  201. char buf_current[10];
  202. dtostrf(current, 6, 3, buf_current);
  203. unsigned int apparent = sqrt(power * power + reactive * reactive);
  204. double energy_delta = (double) power * V9261F_REPORT_INTERVAL / 1000.0 / 3600.0;
  205. char buf_energy[10];
  206. dtostrf(energy_delta, 6, 3, buf_energy);
  207. unsigned int factor = 100 * ((double) power / apparent);
  208. // Report values to MQTT broker
  209. mqttSend(HLW8012_POWER_TOPIC, String(power).c_str());
  210. mqttSend(HLW8012_CURRENT_TOPIC, buf_current);
  211. mqttSend(HLW8012_VOLTAGE_TOPIC, String(voltage).c_str());
  212. mqttSend(HLW8012_ENERGY_TOPIC, buf_energy);
  213. mqttSend(HLW8012_APOWER_TOPIC, String(apparent).c_str());
  214. mqttSend(HLW8012_RPOWER_TOPIC, String(reactive).c_str());
  215. mqttSend(HLW8012_PFACTOR_TOPIC, String(factor).c_str());
  216. // Report values to Domoticz
  217. #if DOMOTICZ_SUPPORT
  218. {
  219. char buffer[20];
  220. snprintf_P(buffer, sizeof(buffer), PSTR("%d;%s"), power, buf_energy);
  221. domoticzSend("dczPowIdx", 0, buffer);
  222. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), buf_energy);
  223. domoticzSend("dczEnergyIdx", 0, buffer);
  224. snprintf_P(buffer, sizeof(buffer), PSTR("%d"), voltage);
  225. domoticzSend("dczVoltIdx", 0, buffer);
  226. snprintf_P(buffer, sizeof(buffer), PSTR("%s"), buf_current);
  227. domoticzSend("dczCurrentIdx", 0, buffer);
  228. }
  229. #endif
  230. #if INFLUXDB_SUPPORT
  231. {
  232. influxDBSend(HLW8012_POWER_TOPIC, String(power).c_str());
  233. influxDBSend(HLW8012_CURRENT_TOPIC, buf_current);
  234. influxDBSend(HLW8012_VOLTAGE_TOPIC, String(voltage).c_str());
  235. influxDBSend(HLW8012_ENERGY_TOPIC, buf_energy);
  236. influxDBSend(HLW8012_APOWER_TOPIC, String(apparent).c_str());
  237. influxDBSend(HLW8012_RPOWER_TOPIC, String(reactive).c_str());
  238. influxDBSend(HLW8012_PFACTOR_TOPIC, String(factor).c_str());
  239. }
  240. #endif
  241. // Reset counters
  242. sum_power = sum_rpower = sum_voltage = sum_current = count = 0;
  243. }
  244. }
  245. #endif
  246. */