2018-10-17 11:27:05 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xnrg_06_pzem_dc.ino - PZEM-003,017 Modbus DC energy sensor support for Tasmota
|
2018-10-17 11:27:05 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2018-10-17 11:27:05 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_ENERGY_SENSOR
|
|
|
|
#ifdef USE_PZEM_DC
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* PZEM-003 - DC 300V 10A Energy
|
|
|
|
* PZEM-017 - DC 300V 50A - 300A Energy
|
|
|
|
*
|
|
|
|
* Based on:
|
|
|
|
* PZEM-003,017 docs Https://pan.baidu.com/s/1V9bDWj3RK2u6_fbBJ3GtqQ password rq37
|
|
|
|
*
|
2019-10-21 09:27:36 +01:00
|
|
|
* Hardware Serial will be selected if GPIO1 = [62 PZEM0XX Tx] and GPIO3 = [99 PZEM017 Rx]
|
2018-10-17 11:27:05 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_06 6
|
|
|
|
|
2019-10-20 14:48:14 +01:00
|
|
|
const uint8_t PZEM_DC_DEVICE_ADDRESS = 0x01; // PZEM default address
|
|
|
|
const uint32_t PZEM_DC_STABILIZE = 30; // Number of seconds to stabilize configuration
|
2018-10-17 11:27:05 +01:00
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *PzemDcModbus;
|
|
|
|
|
2019-09-15 15:02:00 +01:00
|
|
|
struct PZEMDC {
|
|
|
|
float energy = 0;
|
2019-10-20 12:27:48 +01:00
|
|
|
float last_energy = 0;
|
2019-09-15 15:02:00 +01:00
|
|
|
uint8_t send_retry = 0;
|
|
|
|
uint8_t channel = 0;
|
2019-09-16 15:56:16 +01:00
|
|
|
uint8_t address = 0;
|
|
|
|
uint8_t address_step = ADDR_IDLE;
|
2019-09-15 15:02:00 +01:00
|
|
|
} PzemDc;
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemDcEverySecond(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
|
|
|
bool data_ready = PzemDcModbus->ReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
2019-09-10 14:14:51 +01:00
|
|
|
uint8_t buffer[26]; // At least 5 + (2 * 8) = 21
|
2018-10-17 11:27:05 +01:00
|
|
|
|
2019-09-16 15:56:16 +01:00
|
|
|
uint8_t registers = 8;
|
|
|
|
if (ADDR_RECEIVE == PzemDc.address_step) {
|
|
|
|
registers = 2; // Need 1 byte extra as response is F8 06 00 02 00 01 FD A3
|
|
|
|
PzemDc.address_step--;
|
|
|
|
}
|
|
|
|
uint8_t error = PzemDcModbus->ReceiveBuffer(buffer, registers);
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, PzemDcModbus->ReceiveCount());
|
2018-10-17 11:27:05 +01:00
|
|
|
|
|
|
|
if (error) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("PDC: PzemDc %d error %d"), PZEM_DC_DEVICE_ADDRESS + PzemDc.channel, error);
|
2018-10-17 11:27:05 +01:00
|
|
|
} else {
|
2019-09-21 16:10:52 +01:00
|
|
|
Energy.data_valid[PzemDc.channel] = 0;
|
2019-09-16 15:56:16 +01:00
|
|
|
if (8 == registers) {
|
|
|
|
|
2019-10-20 12:27:48 +01:00
|
|
|
// 0 1 2 3 4 5 6 7 = ModBus register
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 = Buffer index
|
2019-09-16 15:56:16 +01:00
|
|
|
// 01 04 10 05 40 00 0A 00 0D 00 00 00 02 00 00 00 00 00 00 D6 29
|
|
|
|
// Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc--
|
|
|
|
Energy.voltage[PzemDc.channel] = (float)((buffer[3] << 8) + buffer[4]) / 100.0; // 655.00 V
|
|
|
|
Energy.current[PzemDc.channel] = (float)((buffer[5] << 8) + buffer[6]) / 100.0; // 655.00 A
|
|
|
|
Energy.active_power[PzemDc.channel] = (float)((buffer[9] << 24) + (buffer[10] << 16) + (buffer[7] << 8) + buffer[8]) / 10.0; // 429496729.0 W
|
|
|
|
|
|
|
|
PzemDc.energy += (float)((buffer[13] << 24) + (buffer[14] << 16) + (buffer[11] << 8) + buffer[12]); // 4294967295 Wh
|
|
|
|
if (PzemDc.channel == Energy.phase_count -1) {
|
2019-10-20 12:27:48 +01:00
|
|
|
if (PzemDc.energy > PzemDc.last_energy) { // Handle missed channel
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime > PZEM_DC_STABILIZE) {
|
2019-10-20 14:48:14 +01:00
|
|
|
EnergyUpdateTotal(PzemDc.energy, false);
|
|
|
|
}
|
2019-10-20 12:27:48 +01:00
|
|
|
PzemDc.last_energy = PzemDc.energy;
|
|
|
|
}
|
2019-09-16 15:56:16 +01:00
|
|
|
PzemDc.energy = 0;
|
|
|
|
}
|
2019-09-15 15:02:00 +01:00
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:02:00 +01:00
|
|
|
if (0 == PzemDc.send_retry || data_ready) {
|
2019-10-08 12:18:10 +01:00
|
|
|
if (0 == PzemDc.channel) {
|
|
|
|
PzemDc.channel = Energy.phase_count -1;
|
|
|
|
} else {
|
|
|
|
PzemDc.channel--;
|
2019-09-15 15:02:00 +01:00
|
|
|
}
|
|
|
|
PzemDc.send_retry = ENERGY_WATCHDOG;
|
2019-09-16 15:56:16 +01:00
|
|
|
if (ADDR_SEND == PzemDc.address_step) {
|
|
|
|
PzemDcModbus->Send(0xF8, 0x06, 0x0002, (uint16_t)PzemDc.address);
|
|
|
|
PzemDc.address_step--;
|
|
|
|
} else {
|
|
|
|
PzemDcModbus->Send(PZEM_DC_DEVICE_ADDRESS + PzemDc.channel, 0x04, 0, 8);
|
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-09-15 15:02:00 +01:00
|
|
|
PzemDc.send_retry--;
|
2020-10-28 16:32:07 +00:00
|
|
|
if ((Energy.phase_count > 1) && (0 == PzemDc.send_retry) && (TasmotaGlobal.uptime < PZEM_DC_STABILIZE)) {
|
2019-09-15 15:02:00 +01:00
|
|
|
Energy.phase_count--; // Decrement channels if no response after retry within 30 seconds after restart
|
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemDcSnsInit(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
2020-04-27 10:54:23 +01:00
|
|
|
PzemDcModbus = new TasmotaModbus(Pin(GPIO_PZEM017_RX), Pin(GPIO_PZEM0XX_TX));
|
2018-10-17 11:27:05 +01:00
|
|
|
uint8_t result = PzemDcModbus->Begin(9600, 2); // Uses two stop bits!!
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.type_dc = true;
|
2021-04-06 14:23:07 +01:00
|
|
|
Energy.phase_count = ENERGY_MAX_PHASES; // Start off with three channels
|
2019-10-08 12:18:10 +01:00
|
|
|
PzemDc.channel = 0;
|
2018-10-17 11:27:05 +01:00
|
|
|
} else {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemDcDrvInit(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_PZEM017_RX) && PinUsed(GPIO_PZEM0XX_TX)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_06;
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:56:16 +01:00
|
|
|
bool PzemDcCommand(void)
|
|
|
|
{
|
|
|
|
bool serviced = true;
|
|
|
|
|
|
|
|
if (CMND_MODULEADDRESS == Energy.command_code) {
|
|
|
|
PzemDc.address = XdrvMailbox.payload; // Valid addresses are 1, 2 and 3
|
|
|
|
PzemDc.address_step = ADDR_SEND;
|
|
|
|
}
|
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2018-10-17 11:27:05 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
bool Xnrg06(uint8_t function)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
2019-09-08 15:57:56 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_ENERGY_EVERY_SECOND:
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875
|
2019-09-08 15:57:56 +01:00
|
|
|
break;
|
2019-09-16 15:56:16 +01:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = PzemDcCommand();
|
|
|
|
break;
|
2019-09-08 15:57:56 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
PzemDcSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
PzemDcDrvInit();
|
|
|
|
break;
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PZEM_DC
|
|
|
|
#endif // USE_ENERGY_SENSOR
|