Arduino nano mqtt

hi all, I created this code to sending the value of two pzem by mqtt (the pzem are connected on an arduino nano with ethernet board):

#include <EthernetENC.h> //più leggera rispetto UIPEthernet
//#include <UIPEthernet.h>
#include <PubSubClient.h>   
#define DEBUG 0// Debug output to serial console
#include <PZEM004T.h>
#include <PZEM004Tv30.h>

PZEM004Tv30 pzempdc(4,5); // (RX,TX) connect to TX,RX of PZEM
PZEM004T pzem(2,3);  // (RX,TX) connect to TX,RX of PZEM
IPAddress pz(10,10,1,1);

int sensorPin = A0; // Pin to which the sensor is connected to
unsigned long mytime = 0;

byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0x20, 0xC8 }; //physical mac address
byte mqtt_ip[] = {ip }; // ip in lan
const char* mqttUser = "user";
const char* mqttPassword = "pass";

char buf[4]; // Buffer to store the sensor value
int updateInterval = 20000; // Interval in milliseconds
EthernetClient espClient;
PubSubClient client(espClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClientSuperior",mqttUser, mqttPassword)) {
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void sensors() {
        int sensorValue = analogRead(sensorPin);
        client.publish("home-assistant/sensor01/brightness", itoa(analogRead(sensorPin), buf, 10));   //e'il sensore fumo

        float e =;                                          //energia     
        float p = pzem.power(pz);                                           //potenza 
        float c = pzem.current(pz);                                          //energia     
        float v = pzem.voltage(pz);                                           //potenza

        client.publish("home-assistant/sensor03/voltage", (String(v).c_str()), true);
        client.publish("home-assistant/sensor03/energy", String(e).c_str(), true);
        client.publish("home-assistant/sensor03/power", String(p).c_str(), true);
  //    client.publish("home-assistant/sensor03/current", String(c).c_str(), true);
        float epdc =;                                          //energia     
        float ppdc = pzempdc.power();                                           //potenza 
        float cpdc = pzempdc.current();                                          //energia     
        float vpdc = pzempdc.voltage();                                           //potenza
        client.publish("home-assistant/sensor04/voltage", (String(vpdc).c_str()), true);
        client.publish("home-assistant/sensor04/energy", String(epdc).c_str(), true);
        client.publish("home-assistant/sensor04/power", String(ppdc).c_str(), true);
     //   client.publish("home-assistant/sensor04/current", String(cpdc).c_str(), true);  
void setup()
//  pzem.setAddress(pz);
//  pzempdc.setAddress(pz);
  Ethernet.begin(mac,{192, 168, 0, 15 },{192, 168, 0, 1 },{192, 168, 0, 1 }); //Ethernet.begin(mac, ip, dns, gateway);
  client.setServer(mqtt_ip, 1883);
void loop()
  if (!client.connected()) {    reconnect();  }
  if (millis()-mytime>updateInterval)
      { mytime=millis(); 

Each 20 seconds the function sensors() reads the value of two pzem and sends the value by mqtt…
The problem is: I can’t send all 4 values of the each pzem. If I try that (sending 8 values) arduino doesn’t show errors but home assistant doesn’t receive the values…

If I send only 3 values of each pzem (6 in total) everything works…

Could someone help to send all the values?

Can you send the current readings if you comment one of the others out?

Floats and MQTT can be something of an issue. I had an issue where I would send float values but not all of them were received. One thing I did that helped was to add a delay between each publish of 10ms.

What I ended up doing was to combine all my float values into a comma delimited string, rounding the float values to either 2 or 3 or 4 decimal places. On the receive side I parse out the string.

 if ( count >= 30 )
      MQTTinfo.concat( String(kph, 2) );
      MQTTinfo.concat( ",");
      MQTTinfo.concat( windDirection );
      MQTTinfo.concat( ",");
      MQTTinfo.concat( String(rain, 2) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicWSWDRF, MQTTinfo.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );
      count = 0;

Example of rounding and sending float values as a comma delimited payload.

Why are you trying to use two different PZEM libraries, especially since PZEM004Tv30 is an updated version of PZEM004T?

so, if no decimals are needed, I can write:

client.publish("home-assistant/sensor03/voltage", (String(v,0).c_str()), true);

could this help?

because one pzem is v2, the other is v3....

yes, it seems that I sends too much values, I tried to insert some delay between one publish and other but doesn't change

I'd try to send the values alternately. You already have the twenty second interval set up. Use a boolean flag to decide which pzem to send each time.

I can try...
I used this code with only one pzem for months and everything was ok...

with this solution I have no more problems... Thanks...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.