diff --git a/PZEM004Tv30.cpp b/PZEM004Tv30.cpp index 2856d6e..7ca370a 100644 --- a/PZEM004Tv30.cpp +++ b/PZEM004Tv30.cpp @@ -82,9 +82,9 @@ void printBuf(uint8_t* buffer, uint16_t len){ #if defined(PZEM004_SOFTSERIAL) PZEM004Tv30::PZEM004Tv30(uint8_t receivePin, uint8_t transmitPin, uint8_t addr) { - SoftwareSerial *port = new SoftwareSerial(receivePin, transmitPin); - port->begin(PZEM_BAUD_RATE); - this->_serial = port; + SoftwareSerial *swserial = new SoftwareSerial(receivePin, transmitPin); + swserial->begin(PZEM_BAUD_RATE); + this->_serial = swserial; this->_isSoft = true; init(addr); } @@ -106,6 +106,25 @@ PZEM004Tv30::PZEM004Tv30(HardwareSerial* port, uint8_t addr) init(addr); } +#ifdef ESP32 +/*! + * PZEM004Tv30::PZEM004Tv30 + * + * Hardware serial constructor with custom pin mapping (esp32) + * + * @param port Hardware serial to use + * @param rxpin gpio pin to map rx + * @param txpin gpio pin to map tx + * @param addr Slave address of device + */ +PZEM004Tv30::PZEM004Tv30(HardwareSerial* port, uint8_t rxpin, uint8_t txpin, uint8_t addr){ + port->begin(PZEM_BAUD_RATE, SERIAL_8N1, rxpin, txpin); + this->_serial = port; + this->_isSoft = false; + init(addr); +} +#endif + /*! * PZEM004Tv30::~PZEM004Tv30 * @@ -114,8 +133,10 @@ PZEM004Tv30::PZEM004Tv30(HardwareSerial* port, uint8_t addr) */ PZEM004Tv30::~PZEM004Tv30() { +#ifdef PZEM004_SOFTSERIAL if(_isSoft) - delete this->_serial; + delete swserial; +#endif } /*! diff --git a/PZEM004Tv30.h b/PZEM004Tv30.h index 0eb7c5f..e43ad60 100644 --- a/PZEM004Tv30.h +++ b/PZEM004Tv30.h @@ -63,6 +63,11 @@ class PZEM004Tv30 PZEM004Tv30(uint8_t receivePin, uint8_t transmitPin, uint8_t addr=PZEM_DEFAULT_ADDR); #endif PZEM004Tv30(HardwareSerial* port, uint8_t addr=PZEM_DEFAULT_ADDR); + +#ifdef ESP32 + PZEM004Tv30(HardwareSerial* port, uint8_t rxpin, uint8_t txpin, uint8_t addr=PZEM_DEFAULT_ADDR); +#endif + ~PZEM004Tv30(); @@ -86,6 +91,11 @@ class PZEM004Tv30 private: + #ifdef PZEM004_SOFTSERIAL + // Stream has no virtual dtor, so keep the pointer + SoftwareSerial *swserial = nullptr; + #endif + Stream* _serial; // Serial interface bool _isSoft; // Is serial interface software diff --git a/examples/ESP32HWSerial/esp32hwserial.ino b/examples/ESP32HWSerial/esp32hwserial.ino new file mode 100644 index 0000000..9b49361 --- /dev/null +++ b/examples/ESP32HWSerial/esp32hwserial.ino @@ -0,0 +1,61 @@ +#include +#include + +/* Hardware Serial1 is usually occupied by USB2Serial converter + * so we will use Serial2 +*/ +HardwareSerial PzemSerial(2); // HW Serial UART2 default mapping is at pins IO-16 (RX2) and IO-17 (TX2) + +// But just for example we will remap it to pins 21 (rx) and 22 (tx) +PZEM004Tv30 pzem(&PzemSerial, 21, 22); + +void setup() { + Serial.begin(115200); // Serial1 is used for console output +} + +void loop() { + float voltage = pzem.voltage(); + if(!isnan(voltage)){ + Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); + } else { + Serial.println("Error reading voltage"); + } + + float current = pzem.current(); + if(!isnan(current)){ + Serial.print("Current: "); Serial.print(current); Serial.println("A"); + } else { + Serial.println("Error reading current"); + } + + float power = pzem.power(); + if(!isnan(power)){ + Serial.print("Power: "); Serial.print(power); Serial.println("W"); + } else { + Serial.println("Error reading power"); + } + + float energy = pzem.energy(); + if(!isnan(energy)){ + Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh"); + } else { + Serial.println("Error reading energy"); + } + + float frequency = pzem.frequency(); + if(!isnan(frequency)){ + Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz"); + } else { + Serial.println("Error reading frequency"); + } + + float pf = pzem.pf(); + if(!isnan(pf)){ + Serial.print("PF: "); Serial.println(pf); + } else { + Serial.println("Error reading power factor"); + } + Serial.printf("End: %u\n", millis()); + + delay(5000); +}