mandulaj / PZEM-004T-v30

Arduino library for the Updated PZEM-004T v3.0 Power and Energy meter
MIT License
256 stars 108 forks source link

fix Hardware Serial for esp32 #31

Closed nguyenhuudamy closed 2 years ago

mandulaj commented 3 years ago

Hi @nguyenhuudamy Thanks for the PR. However I don't like the idea of changing an established API. It would for sure break my projects not to mention anyone who already uses this library... Perhaps we can overload the function or wrap it in a #if #else to fix it just for the esp32 platform while retaining the Arduino compatible API.

ManuelJimenezBuendia commented 3 years ago

What about something like this? .h

ifdef ESP32

PZEM004Tv30(HardwareSerial* port, int rx_pin, int tx_pin, uint8_t addr=PZEM_DEFAULT_ADDR);

endif

.cpp

ifdef ESP32

PZEM004Tv30::PZEM004Tv30(HardwareSerial* port, int rx_pin, int tx_pin, uint8_t addr) { port->begin(PZEM_BAUD_RATE, SERIAL_8N1, rx_pin, tx_pin); //Baud rate, parity mode, RX, TX this->_serial = port; this->_isSoft = false; init(addr); }

endif

loopyengineeringco commented 2 years ago

Thanks @mandulaj, looks great at a glance - I will check it out this evening, test with my ESP32 and report 👍

mandulaj commented 2 years ago

I managed to get hold of an ESP32 and it seems to work fine... So I am ready to merge right away.