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

Compatibility with Arduino nano 33 IoT #53

Open zygisjas opened 3 years ago

zygisjas commented 3 years ago

I was trying to connect PZEM004 to Arduino nano 33 IoT board, and this library is using SofwareSerial.h, I tried to dig deeper, and Nano 33 IoT does not use SoftwareSerial anymore, so I'm not sure how to make PZEM004 compatible with Nano 33 IoT board, could not find anything in the internet about this issue. Maybe someone has an idea how to solve this?

This is from arduino official site:

Serial ports on the Arduino NANO 33 IoT

The USB connector of the board is directly connected to the USB host pins of the SAMD21. This routing enables you to use the Arduino NANO 33 IoT as a client USB peripheral (acting as a mouse or a keyboard connected to the computer) or as a USB host device so that devices like a mouse, keyboard, or an Android phone can be connected to the Arduino NANO 33 IoT. This port can also be used as a virtual serial port using the Serial object in the Arduino programming language. The RX0 and TX1 pins are a second serial port available as Serial1

So I've tried to simply write the code like this:

#include <PZEM004Tv30.h>

PZEM004Tv30 pzem(Serial1);

void setup() {
  Serial.begin(115200);

  Serial.print("Reset Energy");
  pzem.resetEnergy();

  Serial.print("Set address to 0x42");
  pzem.setAddress(0x42);
}

void loop() {
  float volt = pzem.voltage();
  float cur = pzem.current();
  float powe = pzem.power();
  float ener = pzem.energy();
  float freq = pzem.frequency();
  float pf = pzem.pf();

  Serial.print(volt);
  Serial.print(" ");
  Serial.print(cur);
  Serial.print(" ");
  Serial.print(powe);
  Serial.print(" ");
  Serial.print(ener,3);
  Serial.print(" ");
  Serial.print(freq);
  Serial.print(" ");
  Serial.println(pf);

  delay(1000);
}

But getting error output in aruino IDE:

error: no matching function for call to 'PZEM004Tv30::PZEM004Tv30(Uart&)'

PZEM004Tv30 pzem(Serial1);

Documents\Arduino\libraries\PZEM-004T-v30/PZEM004Tv30.h:65:5: note: candidate: PZEM004Tv30::PZEM004Tv30(arduino::HardwareSerial*, uint8_t)

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

^~~

Documents\Arduino\libraries\PZEM-004T-v30/PZEM004Tv30.h:59:7: note: candidate: constexpr PZEM004Tv30::PZEM004Tv30(const PZEM004Tv30&)

class PZEM004Tv30

^~~

exit status 1 no matching function for call to 'PZEM004Tv30::PZEM004Tv30(Uart&)'

sergiocntr commented 3 years ago

Ciao Jasiunas, I'm sorry I don't own a board like yours, seems that it doesn't need software serial because you can use different UART ( notice CAPS) interfaces, much better explained here : https://stackoverflow.com/questions/57175348/softwareserial-for-arduino-nano-33-iot/57541921#57541921

Let us know your progress. Sergio.

Il mar 16 mar 2021, 19:54 Žygimantas Jasiūnas @.***> ha scritto:

I was trying to connect PZEM004 to Arduino nano 33 IoT board, and this library is using SofwareSerial.h, I tried to dig deeper, and Nano 33 IoT does not use SoftwareSerial anymore, so I'm not sure how to make PZEM004 compatible with Nano 33 IoT board, could not find anything in the internet about this issue. Maybe someone has an idea how to solve this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mandulaj/PZEM-004T-v30/issues/53, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABW4XQ7DGBDIZDG4ZXQRFC3TD6SPBANCNFSM4ZJEFLVA .

zygisjas commented 3 years ago

It was complaining about the Usb Serial conflict in the CPP file, I commented line 54 in CPP.

#define UPDATE_TIME     200

#define RESPONSE_SIZE 32
#define READ_TIMEOUT 100

#define PZEM_BAUD_RATE 9600

**//extern HardwareSerial Serial;**

#define DEBUG

// Debugging function;
void printBuf(uint8_t* buffer, uint16_t len){

And passing Serial to PZEM by using this line:

PZEM004Tv30 pzem(&Serial1);

Now code compiles with no problem, but... when I install this code to Arduino nano 33 IoT, the board simply crashes, and I need to do hard reset. Still no luck.

Update:

If I don't comment HardwareSerial Serial I get this error now:

from C:\Users\Zygimantas Jasiunas\Documents\Arduino\libraries\PZEM-004T-v30\PZEM004Tv30.h:41,

                 from C:\Users\Zygimantas Jasiunas\Documents\Arduino\libraries\PZEM-004T-v30\PZEM004Tv30.cpp:23:

C:\Users\Zygimantas Jasiunas\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\variants\nano_33_iot/variant.h:240:37: error: conflicting declaration 'arduino::HardwareSerial SerialUSB'

 #define Serial                      SerialUSB
zygisjas commented 3 years ago

Update:

I followed suggestions of this thread: https://github.com/mandulaj/PZEM-004T-v30/issues/43 And seems that Arduino Nano 33 IoT started to communicate with PZEM, but I still not getting any output

Any advice how could I debug it?

mandulaj commented 2 years ago

Unfortunately I don't have a Nano 33 IoT. Could you try with the new version v1.1.1 of the library to see if you still face the same issues.

robertoperuzzo commented 1 year ago

For anyone using the same processor's family (Cortex-M0 32-bit SAMD21) the library PZEM-004T-V30-SAMD21 works. I tested with MKR WAN 1310.

Thanks to @zygisjas