PZEM004T MQTT Ethernet - Float values not getting transmited to Home assistant

2022-12-17T00:00:00Z
I have a couple of PZEMs connected to a Nano with an Ethernet module, both are assigned with individual addresses 1 and 2. I have had to comment out a few lines to allow me to get a POWER reading from one PZEM and VOLTAGE, POWER from the other. I have tried delays here and there but they don't work. When I try to publish more than 3 TOPICS to Home-assistant, ALL readings go to zero. I have checked the MQTT receiving client when I publish more TOPICS and the output shows TOPICS being received but not the float values. Anyone had similar problem and any idea of a work arround?

#include <EthernetENC.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <PZEM004Tv30.h>

#define DHTPIN 5  
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
#define NUM_PZEMS 2
PZEM004Tv30 pzem1(7, 8, 0x1);  // (RX,TX) connect to TX,RX of PZEM
PZEM004Tv30 pzem2(7, 8, 0x2);

int sensorPin = A0; // Pin to which the sensor is connected to
unsigned long mytime = 0;
byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0x00, 0x00 }; //physical mac address
byte ip[] = {xxxxxxxxxxxxx }; // ip in lan
const char* mqtt_server = "xxxxxxxxxxxx";
const char* mqttUser = "xxxxxxxx";
const char* mqttPassword = "xxxxxxx";

char buf[4]; // Buffer to store the sensor value

int updateInterval = 2500; // 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)) {
     Serial.println("connected");
   } else {
     Serial.print("failed, rc=");
     Serial.print(client.state());
     Serial.println(" try again in 5 seconds");
     // Wait 5 seconds before retrying
     delay(5000);
   }
 }
}

void sensors() {
       
       float voltage1 = pzem1.voltage(); 
//       float current1 = pzem1.current(); 
       float power1 = pzem1.power();   
//       float energy1 = pzem1.energy(); 
//       delay(5000); 
                         
 //      client.publish("home-assistant/sensor04/voltage", String(voltage1).c_str(), true);// to include decimal figures
       client.publish("home-assistant/sensor04/voltage", (String(voltage1,2).c_str()), true);// for no decimal figures
 //      client.publish("home-assistant/sensor07/current", (String(current1,2).c_str()), true);
       client.publish("home-assistant/sensor06/power", (String(power1,2).c_str()), true);
  //     client.publish("home-assistant/sensor05/energy", (String(energy1,0).c_str()), true);
}       
void sensors2() { 
  //      float voltage2 = pzem2.voltage(); 
//       float current2 = pzem2.current(); 
       float power2 = pzem2.power();  
//       float energy2 = pzem2.energy();      

//       client.publish("home-assistant/sensor08/voltage", (String(voltage2,2).c_str()), true);// for no decimal figures
   //    client.publish("home-assistant/sensor11/current", (String(current2,0).c_str()), true);
       client.publish("home-assistant/sensor10/power", (String(power2,2).c_str()), true);
 //      client.publish("home-assistant/sensor09/energy", (String(energy2,0).c_str()), true);
}
void sensors3() {         
 //       int sensorValue = analogRead(sensorPin);
 //      float h = dht.readHumidity();
 //      float t = dht.readTemperature();
     
 //      client.publish("home-assistant/sensor01/brightness", itoa(analogRead(sensorPin), buf, 10));
 //      client.publish("home-assistant/sensor02/temperature", String(t*9/5 + 18).c_str(), true);
 //      client.publish("home-assistant/sensor03/humidity", String(h).c_str(), true);      
            
}

void setup()
{
// pzem.setAddress(i);
 Serial.begin(2400);
 delay(100);
 dht.begin();
 Ethernet.begin(mac,ip);
 client.setServer(mqtt_server, 1883);
}

void loop()
{
 if (!client.connected()) {    reconnect();  }
 client.loop();
 if (millis()-mytime>updateInterval)
     { mytime=millis(); 
       sensors();
       //delay(50); 
       sensors2();
       //delay(50);
       sensors3();
     }
}

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