xnrg_06_pzem_dc.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. xnrg_06_pzem_dc.ino - PZEM-003,017 Modbus DC energy sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_ENERGY_SENSOR
  16. #ifdef USE_PZEM_DC
  17. /*********************************************************************************************\
  18. * PZEM-003 - DC 300V 10A Energy
  19. * PZEM-017 - DC 300V 50A - 300A Energy
  20. *
  21. * Based on:
  22. * PZEM-003,017 docs Https://pan.baidu.com/s/1V9bDWj3RK2u6_fbBJ3GtqQ password rq37
  23. *
  24. * Hardware Serial will be selected if GPIO1 = [99 PZEM017 Rx] and GPIO3 = [62 PZEM0XX Tx]
  25. \*********************************************************************************************/
  26. #define XNRG_06 6
  27. #define PZEM_DC_DEVICE_ADDRESS 0x01 // PZEM default address
  28. #include <TasmotaModbus.h>
  29. TasmotaModbus *PzemDcModbus;
  30. void PzemDcEverySecond(void)
  31. {
  32. static uint8_t send_retry = 0;
  33. bool data_ready = PzemDcModbus->ReceiveReady();
  34. if (data_ready) {
  35. uint8_t buffer[22];
  36. uint8_t error = PzemDcModbus->ReceiveBuffer(buffer, 8);
  37. AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, (buffer[2]) ? buffer[2] +5 : sizeof(buffer));
  38. if (error) {
  39. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "PzemDc response error %d"), error);
  40. AddLog(LOG_LEVEL_DEBUG);
  41. } else {
  42. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  43. // 01 04 10 05 40 00 0A 00 0D 00 00 00 02 00 00 00 00 00 00 D6 29
  44. // Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc--
  45. energy_voltage = (float)((buffer[3] << 8) + buffer[4]) / 100.0; // 655.00 V
  46. energy_current = (float)((buffer[5] << 8) + buffer[6]) / 100.0; // 655.00 A
  47. energy_active_power = (float)((buffer[9] << 24) + (buffer[10] << 16) + (buffer[7] << 8) + buffer[8]) / 10.0; // 429496729.0 W
  48. float energy = (float)((buffer[13] << 24) + (buffer[14] << 16) + (buffer[11] << 8) + buffer[12]); // 4294967295 Wh
  49. if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and handle roll-over if any
  50. if (energy != energy_start) {
  51. energy_kWhtoday += (unsigned long)((energy - energy_start) * 100);
  52. energy_start = energy;
  53. }
  54. EnergyUpdateToday();
  55. }
  56. }
  57. if (0 == send_retry || data_ready) {
  58. send_retry = 5;
  59. PzemDcModbus->Send(PZEM_DC_DEVICE_ADDRESS, 0x04, 0, 8);
  60. }
  61. else {
  62. send_retry--;
  63. }
  64. }
  65. void PzemDcSnsInit(void)
  66. {
  67. PzemDcModbus = new TasmotaModbus(pin[GPIO_PZEM017_RX], pin[GPIO_PZEM0XX_TX]);
  68. uint8_t result = PzemDcModbus->Begin(9600, 2); // Uses two stop bits!!
  69. if (result) {
  70. if (2 == result) { ClaimSerial(); }
  71. energy_type_dc = true;
  72. } else {
  73. energy_flg = ENERGY_NONE;
  74. }
  75. }
  76. void PzemDcDrvInit(void)
  77. {
  78. if (!energy_flg) {
  79. if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) {
  80. energy_flg = XNRG_06;
  81. }
  82. }
  83. }
  84. /*********************************************************************************************\
  85. * Interface
  86. \*********************************************************************************************/
  87. int Xnrg06(byte function)
  88. {
  89. int result = 0;
  90. if (FUNC_PRE_INIT == function) {
  91. PzemDcDrvInit();
  92. }
  93. else if (XNRG_06 == energy_flg) {
  94. switch (function) {
  95. case FUNC_INIT:
  96. PzemDcSnsInit();
  97. break;
  98. case FUNC_EVERY_SECOND:
  99. PzemDcEverySecond();
  100. break;
  101. }
  102. }
  103. return result;
  104. }
  105. #endif // USE_PZEM_DC
  106. #endif // USE_ENERGY_SENSOR