Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor overload for better ESP32 support #55

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions PZEM004Tv30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
*
Expand All @@ -114,8 +133,10 @@ PZEM004Tv30::PZEM004Tv30(HardwareSerial* port, uint8_t addr)
*/
PZEM004Tv30::~PZEM004Tv30()
{
#ifdef PZEM004_SOFTSERIAL
if(_isSoft)
delete this->_serial;
delete swserial;
#endif
}

/*!
Expand Down
10 changes: 10 additions & 0 deletions PZEM004Tv30.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand All @@ -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

Expand Down
61 changes: 61 additions & 0 deletions examples/ESP32HWSerial/esp32hwserial.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <Arduino.h>
#include <PZEM004Tv30.h>

/* 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);
}