2018-09-04 15:22:34 +01:00
|
|
|
/*
|
|
|
|
xnrg_03_pzem004t.ino - PZEM004T energy sensor support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2018-09-04 15:22:34 +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_PZEM004T
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* PZEM004T - Energy
|
|
|
|
*
|
|
|
|
* Source: Victor Ferrer https://github.com/vicfergar/Sonoff-MQTT-OTA-Arduino
|
|
|
|
* Based on: PZEM004T library https://github.com/olehs/PZEM004T
|
|
|
|
*
|
2018-10-17 11:27:05 +01:00
|
|
|
* Hardware Serial will be selected if GPIO1 = [63 PZEM004 Rx] and GPIO3 = [62 PZEM0XX Tx]
|
2018-09-04 15:22:34 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XNRG_03 3
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
2019-03-26 17:26:50 +00:00
|
|
|
TasmotaSerial *PzemSerial = nullptr;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
#define PZEM_VOLTAGE (uint8_t)0xB0
|
|
|
|
#define RESP_VOLTAGE (uint8_t)0xA0
|
|
|
|
|
|
|
|
#define PZEM_CURRENT (uint8_t)0xB1
|
|
|
|
#define RESP_CURRENT (uint8_t)0xA1
|
|
|
|
|
|
|
|
#define PZEM_POWER (uint8_t)0xB2
|
|
|
|
#define RESP_POWER (uint8_t)0xA2
|
|
|
|
|
|
|
|
#define PZEM_ENERGY (uint8_t)0xB3
|
|
|
|
#define RESP_ENERGY (uint8_t)0xA3
|
|
|
|
|
|
|
|
#define PZEM_SET_ADDRESS (uint8_t)0xB4
|
|
|
|
#define RESP_SET_ADDRESS (uint8_t)0xA4
|
|
|
|
|
|
|
|
#define PZEM_POWER_ALARM (uint8_t)0xB5
|
|
|
|
#define RESP_POWER_ALARM (uint8_t)0xA5
|
|
|
|
|
|
|
|
#define PZEM_DEFAULT_READ_TIMEOUT 500
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
struct PZEMCommand {
|
|
|
|
uint8_t command;
|
|
|
|
uint8_t addr[4];
|
|
|
|
uint8_t data;
|
|
|
|
uint8_t crc;
|
|
|
|
};
|
|
|
|
|
|
|
|
IPAddress pzem_ip(192, 168, 1, 1);
|
|
|
|
|
|
|
|
uint8_t PzemCrc(uint8_t *data)
|
|
|
|
{
|
|
|
|
uint16_t crc = 0;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < sizeof(PZEMCommand) -1; i++) crc += *data++;
|
2018-09-04 15:22:34 +01:00
|
|
|
return (uint8_t)(crc & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PzemSend(uint8_t cmd)
|
|
|
|
{
|
|
|
|
PZEMCommand pzem;
|
|
|
|
|
|
|
|
pzem.command = cmd;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < sizeof(pzem.addr); i++) pzem.addr[i] = pzem_ip[i];
|
2018-09-04 15:22:34 +01:00
|
|
|
pzem.data = 0;
|
|
|
|
|
|
|
|
uint8_t *bytes = (uint8_t*)&pzem;
|
|
|
|
pzem.crc = PzemCrc(bytes);
|
|
|
|
|
|
|
|
PzemSerial->flush();
|
|
|
|
PzemSerial->write(bytes, sizeof(pzem));
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
bool PzemReceiveReady(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
return PzemSerial->available() >= (int)sizeof(PZEMCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PzemRecieve(uint8_t resp, float *data)
|
|
|
|
{
|
|
|
|
// 0 1 2 3 4 5 6
|
|
|
|
// A4 00 00 00 00 00 A4 - Set address
|
|
|
|
// A0 00 D4 07 00 00 7B - Voltage (212.7V)
|
|
|
|
// A1 00 00 0A 00 00 AB - Current (0.1A)
|
|
|
|
// A1 00 00 00 00 00 A1 - No current
|
|
|
|
// A2 00 16 00 00 00 B8 - Power (22W)
|
|
|
|
// A2 00 00 00 00 00 A2 - No power
|
|
|
|
// A3 00 08 A4 00 00 4F - Energy (2.212kWh)
|
|
|
|
// A3 01 86 9F 00 00 C9 - Energy (99.999kWh)
|
|
|
|
|
|
|
|
uint8_t buffer[sizeof(PZEMCommand)] = { 0 };
|
|
|
|
|
|
|
|
unsigned long start = millis();
|
|
|
|
uint8_t len = 0;
|
|
|
|
while ((len < sizeof(PZEMCommand)) && (millis() - start < PZEM_DEFAULT_READ_TIMEOUT)) {
|
|
|
|
if (PzemSerial->available() > 0) {
|
|
|
|
uint8_t c = (uint8_t)PzemSerial->read();
|
|
|
|
if (!c && !len) {
|
|
|
|
continue; // skip 0 at startup
|
|
|
|
}
|
|
|
|
if ((1 == len) && (buffer[0] == c)) {
|
|
|
|
len--;
|
|
|
|
continue; // fix skewed data
|
|
|
|
}
|
|
|
|
buffer[len++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:48:34 +00:00
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, buffer, len);
|
2018-09-04 15:22:34 +01:00
|
|
|
|
|
|
|
if (len != sizeof(PZEMCommand)) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem comms timeout"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (buffer[6] != PzemCrc(buffer)) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem crc error"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (buffer[0] != resp) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem bad response"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resp) {
|
|
|
|
case RESP_VOLTAGE:
|
|
|
|
*data = (float)(buffer[1] << 8) + buffer[2] + (buffer[3] / 10.0); // 65535.x V
|
|
|
|
break;
|
|
|
|
case RESP_CURRENT:
|
|
|
|
*data = (float)(buffer[1] << 8) + buffer[2] + (buffer[3] / 100.0); // 65535.xx A
|
|
|
|
break;
|
|
|
|
case RESP_POWER:
|
|
|
|
*data = (float)(buffer[1] << 8) + buffer[2]; // 65535 W
|
|
|
|
break;
|
|
|
|
case RESP_ENERGY:
|
|
|
|
*data = (float)((uint32_t)buffer[1] << 16) + ((uint16_t)buffer[2] << 8) + buffer[3]; // 16777215 Wh
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
const uint8_t pzem_commands[] { PZEM_SET_ADDRESS, PZEM_VOLTAGE, PZEM_CURRENT, PZEM_POWER, PZEM_ENERGY };
|
|
|
|
const uint8_t pzem_responses[] { RESP_SET_ADDRESS, RESP_VOLTAGE, RESP_CURRENT, RESP_POWER, RESP_ENERGY };
|
|
|
|
|
|
|
|
uint8_t pzem_read_state = 0;
|
|
|
|
uint8_t pzem_sendRetry = 0;
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemEvery200ms(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
bool data_ready = PzemReceiveReady();
|
|
|
|
|
|
|
|
if (data_ready) {
|
|
|
|
float value = 0;
|
|
|
|
if (PzemRecieve(pzem_responses[pzem_read_state], &value)) {
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.data_valid = 0;
|
2018-09-04 15:22:34 +01:00
|
|
|
switch (pzem_read_state) {
|
|
|
|
case 1: // Voltage as 230.2V
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.voltage = value;
|
2018-09-04 15:22:34 +01:00
|
|
|
break;
|
|
|
|
case 2: // Current as 17.32A
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.current = value;
|
2018-09-04 15:22:34 +01:00
|
|
|
break;
|
|
|
|
case 3: // Power as 20W
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.active_power = value;
|
2018-09-04 15:22:34 +01:00
|
|
|
break;
|
|
|
|
case 4: // Total energy as 99999Wh
|
2019-09-03 20:53:20 +01:00
|
|
|
EnergyUpdateTotal(value, false);
|
2018-09-04 15:22:34 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pzem_read_state++;
|
|
|
|
if (5 == pzem_read_state) pzem_read_state = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == pzem_sendRetry || data_ready) {
|
|
|
|
pzem_sendRetry = 5;
|
|
|
|
PzemSend(pzem_commands[pzem_read_state]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pzem_sendRetry--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemSnsInit(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
|
|
|
// Software serial init needs to be done here as earlier (serial) interrupts may lead to Exceptions
|
2018-10-17 11:27:05 +01:00
|
|
|
PzemSerial = new TasmotaSerial(pin[GPIO_PZEM004_RX], pin[GPIO_PZEM0XX_TX], 1);
|
2018-09-04 15:22:34 +01:00
|
|
|
if (PzemSerial->begin(9600)) {
|
|
|
|
if (PzemSerial->hardwareSerial()) { ClaimSerial(); }
|
|
|
|
} else {
|
|
|
|
energy_flg = ENERGY_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void PzemDrvInit(void)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
2019-09-08 15:57:56 +01:00
|
|
|
if ((pin[GPIO_PZEM004_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { // Any device with a Pzem004T
|
|
|
|
energy_flg = XNRG_03;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
bool Xnrg03(uint8_t function)
|
2018-09-04 15:22:34 +01:00
|
|
|
{
|
2019-09-08 15:57:56 +01:00
|
|
|
bool result = false;
|
2018-09-04 15:22:34 +01:00
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_200_MSECOND:
|
|
|
|
if (PzemSerial) { PzemEvery200ms(); }
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
PzemSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
PzemDrvInit();
|
|
|
|
break;
|
2018-09-04 15:22:34 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PZEM004T
|
|
|
|
#endif // USE_ENERGY_SENSOR
|