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)) {
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() {
int sensorValue = analogRead(sensorPin);
client.publish("home-assistant/sensor01/brightness", itoa(analogRead(sensorPin), buf, 10)); //e'il sensore fumo
float e = pzem.energy(pz); //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 = pzempdc.energy(); //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);
Serial.begin(115200);
delay(100);
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(); }
client.loop();
if (millis()-mytime>updateInterval)
{ mytime=millis();
sensors();
}
}
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?