2018-10-17 11:27:05 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xnrg_05_pzem_ac.ino - PZEM-014,016 Modbus AC 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_AC
|
|
|
|
/*********************************************************************************************\
|
2019-10-21 09:27:36 +01:00
|
|
|
* PZEM-004T V3 - AC 220V 10/100A Energy
|
|
|
|
* PZEM-014 - AC 220V 10A Energy
|
|
|
|
* PZEM-016 - AC 220V 100A Energy
|
2018-10-17 11:27:05 +01:00
|
|
|
*
|
|
|
|
* Based on:
|
|
|
|
* PZEM-014,016 docs https://pan.baidu.com/s/1B0MdMgURyjtO1oQa2lavKw password ytkv
|
|
|
|
*
|
2019-10-21 09:27:36 +01:00
|
|
|
* Hardware Serial will be selected if GPIO1 = [62 PZEM0XX Tx] and GPIO3 = [98 PZEM016 Rx]
|
2018-10-17 11:27:05 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_05 5
|
|
|
|
|
2019-10-20 14:48:14 +01:00
|
|
|
const uint8_t PZEM_AC_DEVICE_ADDRESS = 0x01; // PZEM default address
|
|
|
|
const uint32_t PZEM_AC_STABILIZE = 30; // Number of seconds to stabilize configuration
|
2018-10-17 11:27:05 +01:00
|
|
|
|
|
|
|
#include <TasmotaModbus.h>
|
|
|
|
TasmotaModbus *PzemAcModbus;
|
|
|
|
|
2019-09-15 14:19:19 +01:00
|
|
|
struct PZEMAC {
|
|
|
|
float energy = 0;
|
2019-10-20 12:27:48 +01:00
|
|
|
float last_energy = 0;
|
2019-09-15 14:19:19 +01:00
|
|
|
uint8_t send_retry = 0;
|
|
|
|
uint8_t phase = 0;
|
2019-09-16 15:56:16 +01:00
|
|
|
uint8_t address = 0;
|
|
|
|
uint8_t address_step = ADDR_IDLE;
|
2019-09-15 14:19:19 +01:00
|
|
|
} PzemAc;
|
2019-09-10 14:14:51 +01:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemAcEverySecond(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
|
|
|
bool data_ready = PzemAcModbus->ReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
2019-09-10 14:14:51 +01:00
|
|
|
uint8_t buffer[30]; // At least 5 + (2 * 10) = 25
|
2018-10-17 11:27:05 +01:00
|
|
|
|
2019-09-16 15:56:16 +01:00
|
|
|
uint8_t registers = 10;
|
|
|
|
if (ADDR_RECEIVE == PzemAc.address_step) {
|
|
|
|
registers = 2; // Need 1 byte extra as response is F8 06 00 02 00 01 FD A3
|
|
|
|
PzemAc.address_step--;
|
|
|
|
}
|
|
|
|
uint8_t error = PzemAcModbus->ReceiveBuffer(buffer, registers);
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, PzemAcModbus->ReceiveCount());
|
2018-10-17 11:27:05 +01:00
|
|
|
|
|
|
|
if (error) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("PAC: PzemAc %d error %d"), PZEM_AC_DEVICE_ADDRESS + PzemAc.phase, error);
|
2018-10-17 11:27:05 +01:00
|
|
|
} else {
|
2019-09-21 16:10:52 +01:00
|
|
|
Energy.data_valid[PzemAc.phase] = 0;
|
2019-09-16 15:56:16 +01:00
|
|
|
if (10 == registers) {
|
|
|
|
|
2019-10-20 12:27:48 +01:00
|
|
|
// 0 1 2 3 4 5 6 7 8 9 = ModBus register
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 = Buffer index
|
2019-09-16 15:56:16 +01:00
|
|
|
// 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34
|
|
|
|
// Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc--
|
|
|
|
Energy.voltage[PzemAc.phase] = (float)((buffer[3] << 8) + buffer[4]) / 10.0; // 6553.0 V
|
|
|
|
Energy.current[PzemAc.phase] = (float)((buffer[7] << 24) + (buffer[8] << 16) + (buffer[5] << 8) + buffer[6]) / 1000.0; // 4294967.000 A
|
|
|
|
Energy.active_power[PzemAc.phase] = (float)((buffer[11] << 24) + (buffer[12] << 16) + (buffer[9] << 8) + buffer[10]) / 10.0; // 429496729.0 W
|
|
|
|
Energy.frequency[PzemAc.phase] = (float)((buffer[17] << 8) + buffer[18]) / 10.0; // 50.0 Hz
|
|
|
|
Energy.power_factor[PzemAc.phase] = (float)((buffer[19] << 8) + buffer[20]) / 100.0; // 1.00
|
|
|
|
|
|
|
|
PzemAc.energy += (float)((buffer[15] << 24) + (buffer[16] << 16) + (buffer[13] << 8) + buffer[14]); // 4294967295 Wh
|
|
|
|
if (PzemAc.phase == Energy.phase_count -1) {
|
2019-10-20 12:27:48 +01:00
|
|
|
if (PzemAc.energy > PzemAc.last_energy) { // Handle missed phase
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime > PZEM_AC_STABILIZE) {
|
2019-10-20 14:48:14 +01:00
|
|
|
EnergyUpdateTotal(PzemAc.energy, false);
|
|
|
|
}
|
2019-10-20 12:27:48 +01:00
|
|
|
PzemAc.last_energy = PzemAc.energy;
|
|
|
|
}
|
2019-09-16 15:56:16 +01:00
|
|
|
PzemAc.energy = 0;
|
|
|
|
}
|
2019-10-20 12:27:48 +01:00
|
|
|
|
2019-09-15 14:19:19 +01:00
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 14:19:19 +01:00
|
|
|
if (0 == PzemAc.send_retry || data_ready) {
|
2019-10-08 12:18:10 +01:00
|
|
|
if (0 == PzemAc.phase) {
|
|
|
|
PzemAc.phase = Energy.phase_count -1;
|
|
|
|
} else {
|
|
|
|
PzemAc.phase--;
|
2019-09-15 14:19:19 +01:00
|
|
|
}
|
|
|
|
PzemAc.send_retry = ENERGY_WATCHDOG;
|
2019-09-16 15:56:16 +01:00
|
|
|
if (ADDR_SEND == PzemAc.address_step) {
|
|
|
|
PzemAcModbus->Send(0xF8, 0x06, 0x0002, (uint16_t)PzemAc.address);
|
|
|
|
PzemAc.address_step--;
|
|
|
|
} else {
|
|
|
|
PzemAcModbus->Send(PZEM_AC_DEVICE_ADDRESS + PzemAc.phase, 0x04, 0, 10);
|
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-09-15 14:19:19 +01:00
|
|
|
PzemAc.send_retry--;
|
2020-10-28 16:32:07 +00:00
|
|
|
if ((Energy.phase_count > 1) && (0 == PzemAc.send_retry) && (TasmotaGlobal.uptime < PZEM_AC_STABILIZE)) {
|
2019-09-15 15:02:00 +01:00
|
|
|
Energy.phase_count--; // Decrement phases if no response after retry within 30 seconds after restart
|
2019-09-15 14:19:19 +01:00
|
|
|
}
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemAcSnsInit(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
2020-04-27 10:54:23 +01:00
|
|
|
PzemAcModbus = new TasmotaModbus(Pin(GPIO_PZEM016_RX), Pin(GPIO_PZEM0XX_TX));
|
2018-10-17 11:27:05 +01:00
|
|
|
uint8_t result = PzemAcModbus->Begin(9600);
|
|
|
|
if (result) {
|
|
|
|
if (2 == result) { ClaimSerial(); }
|
2021-04-06 14:23:07 +01:00
|
|
|
Energy.phase_count = ENERGY_MAX_PHASES; // Start off with three phases
|
2019-10-08 12:18:10 +01:00
|
|
|
PzemAc.phase = 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 PzemAcDrvInit(void)
|
2018-10-17 11:27:05 +01:00
|
|
|
{
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_PZEM016_RX) && PinUsed(GPIO_PZEM0XX_TX)) {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_05;
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:56:16 +01:00
|
|
|
bool PzemAcCommand(void)
|
|
|
|
{
|
|
|
|
bool serviced = true;
|
|
|
|
|
|
|
|
if (CMND_MODULEADDRESS == Energy.command_code) {
|
|
|
|
PzemAc.address = XdrvMailbox.payload; // Valid addresses are 1, 2 and 3
|
|
|
|
PzemAc.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 Xnrg05(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) { PzemAcEverySecond(); } // 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 = PzemAcCommand();
|
|
|
|
break;
|
2019-09-08 15:57:56 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
PzemAcSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
PzemAcDrvInit();
|
|
|
|
break;
|
2018-10-17 11:27:05 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PZEM_AC
|
|
|
|
#endif // USE_ENERGY_SENSOR
|