diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 7e7bdc520..49193dfbd 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,6 +1,7 @@ /* 6.2.1.2 20180906 * Fix KNX PA exception. Regression from 6.2.1 buffer overflow (#3700, #3710) * Add command SetOption52 to control display of optional time offset from UTC in JSON messages (#3629, #3711) + * Add experimental support for PZEM-003,014,016,017 Energy monitoring (#3694) * * 6.2.1.1 20180905 * Rewrite energy monitoring using energy sensor driver modules diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index b1087da97..3881f8c3e 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -120,6 +120,8 @@ enum UserSelectablePins { GPIO_CNTR2_NP, GPIO_CNTR3_NP, GPIO_CNTR4_NP, + GPIO_PZEM2_TX, // PZEM-003,014,016,017 Serial interface + GPIO_PZEM2_RX, // PZEM-003,014,016,017 Serial interface GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -169,7 +171,8 @@ const char kSensorNames[] PROGMEM = D_SENSOR_TM1638_CLK "|" D_SENSOR_TM1638_DIO "|" D_SENSOR_TM1638_STB "|" D_SENSOR_SWITCH "1n|" D_SENSOR_SWITCH "2n|" D_SENSOR_SWITCH "3n|" D_SENSOR_SWITCH "4n|" D_SENSOR_SWITCH "5n|" D_SENSOR_SWITCH "6n|" D_SENSOR_SWITCH "7n|" D_SENSOR_SWITCH "8n|" D_SENSOR_BUTTON "1n|" D_SENSOR_BUTTON "2n|" D_SENSOR_BUTTON "3n|" D_SENSOR_BUTTON "4n|" - D_SENSOR_COUNTER "1n|" D_SENSOR_COUNTER "2n|" D_SENSOR_COUNTER "3n|" D_SENSOR_COUNTER "4n|"; + D_SENSOR_COUNTER "1n|" D_SENSOR_COUNTER "2n|" D_SENSOR_COUNTER "3n|" D_SENSOR_COUNTER "4n|" + D_SENSOR_PZEM_TX "|" D_SENSOR_PZEM_RX "|"; /********************************************************************************************/ diff --git a/sonoff/user_config.h b/sonoff/user_config.h index ece854530..b742196a8 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -335,13 +335,16 @@ #define CO2_HIGH 1200 // Above this CO2 value show red light (needs PWM or WS2812 RG(B) led and enable with SetOption18 1) #define USE_PMS5003 // Add support for PMS5003 and PMS7003 particle concentration sensor (+1k3 code) #define USE_NOVA_SDS // Add support for SDS011 and SDS021 particle concentration sensor (+0k7 code) -#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code) #define USE_SERIAL_BRIDGE // Add support for software Serial Bridge (+0k8 code) //#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+1k7 code) #define SDM120_SPEED 9600 // SDM120-Modbus RS485 serial speed (default: 2400 baud) //#define USE_SDM630 // Add support for Eastron SDM630-Modbus energy meter (+2k code) #define SDM630_SPEED 9600 // SDM630-Modbus RS485 serial speed (default: 9600 baud) +// Power monitoring sensors ----------------------- +#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code) +#define USE_PZEM2 // Add support for PZEM003,014,016,017 Energy monitor (+2k code) + // -- Low level interface devices ----------------- #define USE_IR_REMOTE // Send IR remote commands using library IRremoteESP8266 and ArduinoJson (+4k code, 0k3 mem, 48 iram) // #define USE_IR_HVAC // Support for HVAC system using IR (+2k code) diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index 5c122c314..d9cfee8cb 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -44,6 +44,7 @@ const char kEnergyCommands[] PROGMEM = float energy_voltage = 0; // 123.1 V float energy_current = 0; // 123.123 A float energy_power = 0; // 123.1 W +float energy_frequency = 0; // 123.1 Hz float energy_power_factor = 0; // 0.12 float energy_daily = 0; // 123.123 kWh float energy_total = 0; // 12345.12345 kWh diff --git a/sonoff/xnrg_05_pzem2.ino b/sonoff/xnrg_05_pzem2.ino new file mode 100644 index 000000000..2b326f216 --- /dev/null +++ b/sonoff/xnrg_05_pzem2.ino @@ -0,0 +1,215 @@ +/* + xnrg_06_pzem2.ino - PZEM-003,017 and PZEM-014,016 Modbus energy sensor support for Sonoff-Tasmota + + Copyright (C) 2018 Theo Arends + + 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 . +*/ + +#ifdef USE_ENERGY_SENSOR +#ifdef USE_PZEM2 +/*********************************************************************************************\ + * PZEM-003 - DC 300V 10A Energy + * PZEM-014 - AC 220V 10A Energy + * PZEM-016 - AC 220V 100A Energy + * PZEM-017 - DC 300V 50A - 300A Energy + * + * Based on: + * PZEM-003,017 docs Https://pan.baidu.com/s/1V9bDWj3RK2u6_fbBJ3GtqQ password rq37 + * PZEM-014,016 docs https://pan.baidu.com/s/1B0MdMgURyjtO1oQa2lavKw password ytkv + * + * Hardware Serial will be selected if GPIO1 = [99 PZEM Rx] and GPIO3 = [98 PZEM Tx] +\*********************************************************************************************/ + +#define XNRG_05 5 + +#define PZEM2_TYPES_003_017 8 // Result 16 bit register count +#define PZEM2_TYPES_014_016 10 // Result 16 bit register count + +#define PZEM2_READ_RESULT 0x04 + +#include + +TasmotaSerial *Pzem2Serial; + +uint8_t pzem2_type = PZEM2_TYPES_014_016; + +/*********************************************************************************************/ + +uint16_t Pzem2ModbusCalculateCRC(uint8_t *frame, uint8_t num) +{ + uint16_t crc = 0xFFFF; + uint16_t flag; + + for (uint8_t i = 0; i < num; i++) { + crc ^= frame[i]; + for (uint8_t j = 8; j; j--) { + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else { // Else LSB is not set + crc >>= 1; // Just shift right + } + } + } + return crc; +} + +void Pzem2ModbusSend(uint8_t function_code, uint16_t start_address, uint16_t register_count) +{ + uint8_t frame[8]; + + frame[0] = 0xFE; // Any Address + frame[1] = function_code; + frame[2] = (uint8_t)(start_address >> 8); + frame[3] = (uint8_t)(start_address); + frame[4] = (uint8_t)(register_count >> 8); + frame[5] = (uint8_t)(register_count); + uint16_t crc = Pzem2ModbusCalculateCRC(frame, 6); + frame[6] = (uint8_t)((crc >> 8) & 0xFF); + frame[7] = (uint8_t)(crc & 0xFF); + + Pzem2Serial->flush(); + Pzem2Serial->write(frame, sizeof(frame)); +} + +bool Pzem2ModbusReceiveReady() +{ + return (Pzem2Serial->available() >= 5); // 5 - Error frame, 21 or 25 - Ok frame +} + +uint8_t Pzem2ModbusReceive() +{ + uint8_t buffer[26]; + +// 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 +// FE 04 14 08 98 03 E8 00 00 08 98 00 00 00 00 00 00 01 F4 00 64 00 00 HH LL = PZEM-014 +// Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc-- +// FE 04 10 27 10 00 64 03 E8 00 00 00 00 00 00 00 00 00 00 HH LL = PZEM-017 +// Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc-- + + uint8_t len = 0; + while (Pzem2Serial->available() > 0) { + buffer[len++] = (uint8_t)Pzem2Serial->read(); + if (3 == len) { + if (buffer[1] & 0x80) { // fe 84 02 f2 f1 + return buffer[2]; // 1 = Illegal Function, 2 = Illegal Address, 3 = Illegal Data, 4 = Slave Error + } + } + if (sizeof(buffer) == len) { break; } + } + + AddLogSerial(LOG_LEVEL_DEBUG_MORE, buffer, len); + + if (len < 5) { return 8; } // 8 = Not enough data + if (len != buffer[2] + 5) { return 9; } // 9 = Unexpected result + + uint16_t crc = (buffer[len -2] << 8) | buffer[len -1]; + if (Pzem2ModbusCalculateCRC(buffer, len -3) == crc) { + float energy = 0; + if (0x10 == buffer[2]) { // PZEM-003,017 + pzem2_type = PZEM2_TYPES_003_017; + energy_voltage = (float)((buffer[3] << 8) + buffer[4]) / 10.0; // 65535.x V + energy_current = (float)((buffer[5] << 8) + buffer[6]); // 65535.xx A + energy_power = (float)((uint32_t)buffer[9] << 24 + (uint32_t)buffer[10] << 16 + (uint32_t)buffer[7] << 8 + buffer[8]); // 65535 W + energy = (float)((uint32_t)buffer[13] << 24 + (uint32_t)buffer[14] << 16 + (uint32_t)buffer[11] << 8 + buffer[12]); // 65535 Wh + if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and hanlde roll-over if any + energy_kWhtoday += (energy - energy_start) * 100; + energy_start = energy; + EnergyUpdateToday(); + } + else if (0x14 == buffer[2]) { // PZEM-014,016 + pzem2_type = PZEM2_TYPES_014_016; + energy_voltage = (float)((buffer[3] << 8) + buffer[4]) / 10.0; // 65535.x V + energy_current = (float)((uint32_t)buffer[7] << 24 + (uint32_t)buffer[8] << 16 + (uint32_t)buffer[5] << 8 + buffer[6]); // 65535.xx A + energy_power = (float)((uint32_t)buffer[11] << 24 + (uint32_t)buffer[12] << 16 + (uint32_t)buffer[9] << 8 + buffer[10]); // 65535 W + energy_frequency = (float)((buffer[13] << 8) + buffer[14]) / 10.0; // 50.0 Hz + energy = (float)((uint32_t)buffer[15] << 24 + (uint32_t)buffer[16] << 16 + (uint32_t)buffer[13] << 8 + buffer[14]); // 65535 Wh + if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and hanlde roll-over if any + energy_kWhtoday += (energy - energy_start) * 100; + energy_start = energy; + EnergyUpdateToday(); + } + } else { + AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "Pzem2 crc error")); + } + + return 0; // 0 = No error +} + +/*********************************************************************************************/ + +uint8_t pzem2_sendRetry = 0; + +void Pzem2EverySecond() +{ + bool data_ready = Pzem2ModbusReceiveReady(); + + if (data_ready) { Pzem2ModbusReceive(); } + + if (0 == pzem2_sendRetry || data_ready) { + pzem2_sendRetry = 5; + Pzem2ModbusSend(PZEM2_READ_RESULT, 0, pzem2_type); + } + else { + pzem2_sendRetry--; + } +} + +void Pzem2SnsInit() +{ + // Software serial init needs to be done here as earlier (serial) interrupts may lead to Exceptions + Pzem2Serial = new TasmotaSerial(pin[GPIO_PZEM2_RX], pin[GPIO_PZEM2_TX], 1); + if (Pzem2Serial->begin(9600)) { + if (Pzem2Serial->hardwareSerial()) { ClaimSerial(); } + } else { + energy_flg = ENERGY_NONE; + } +} + +void Pzem2DrvInit() +{ + if (!energy_flg) { + if ((pin[GPIO_PZEM2_RX] < 99) && (pin[GPIO_PZEM2_TX] < 99)) { // Any device with a Pzem-003,014,016,017 + energy_flg = XNRG_05; + } + } +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +int Xnrg05(byte function) +{ + int result = 0; + + if (FUNC_PRE_INIT == function) { + Pzem2DrvInit(); + } + else if (XNRG_03 == energy_flg) { + switch (function) { + case FUNC_INIT: + Pzem2SnsInit(); + break; + case FUNC_EVERY_SECOND: + Pzem2EverySecond(); + break; + } + } + return result; +} + +#endif // USE_PZEM2 +#endif // USE_ENERGY_SENSOR