hi community, I have connected Arduino UNO (serial USB with cayenne) with PZEM-004, all values are shown except current ones, can you help me?you will find attached the software that I used. Thank you.
#include <CayenneMQTTSerial.h>
//ajout
#include <PZEM004T.h> //https://github.com/olehs/PZEM004T
PZEM004T pzem(11,10); // RX,TX (D2, D1) on NodeMCU
IPAddress ip(192,168,1,1);
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
int voltage_cayenne=0;
int current_cayenne;
int power_cayenne=0;
int energy_cayenne=0;
int courant;
char username[] = "";
char password[] = "";
char clientID[] = "";
unsigned long lastMillis = 0;
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
pzem.setAddress(ip);
}
void loop() {
Cayenne.loop();
// Read meter PZEM
float v = pzem.voltage(ip);
if(v >= 0.0){ voltage_cayenne =v; } //V
float i = pzem.current(ip);
if(i >= 0.0){ current_cayenne=i; } //A convert A ---> mA
float p = pzem.power(ip);
if(p >= 0.0){ power_cayenne=p; } //W
float e = pzem.energy(ip);
if(e >= 0.0){ energy_cayenne =e; } ///Wh
delay(1000);.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Cayenne.virtualWrite(1,voltage_cayenne);
Cayenne.virtualWrite(2,current_cayenne*1000);
Cayenne.virtualWrite(3,power_cayenne);
Cayenne.virtualWrite(4, energy_cayenne);
Cayenne.virtualWrite(5, lastMillis);
}
}
hi @sara.loukili.lasri do you have a rthernet sield or esp8266. As it is difficult to debug when the USB serial library is used.
1 Like
Hello, yes I have esp8266 but I must add a resistance of 1kohm in power meter. I tried with Arduino UNO using the serial USB, even when I run in the serial monitor without using cayenne all the values are displayed. but when I try cayenne except the current that is not displayed. I don’t know where is the problem.
Thank you.
connect esp8266 as shield and use this code. follow the steps below. Once you connect add your sensor code to this.
/*
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected via a hardware serial to an Arduino.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries) to compile this example.
2. Connect the ESP8266 as a shield to your Arduino. This example uses the Serial hardware serial pins available on the UNO.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
NOTE: This code requires ESP8266 firmware version 1.0.0 (AT v0.22) or later.
*/
//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
// WiFi network info.
char ssid[] = "=";
char wifiPassword[] = "";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
ESP8266 wifi(&EspSerial);
void setup()
{
//Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}
void loop()
{
Cayenne.loop();
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
1 Like
Soukd I use ESP82 + Arduino with the power meter or just Esp82? If It just Esp82 can it work without welding the 1k resistance to the SDIC(the card which is inside the power meter?
yes. the PZEM-004 uses serial communication and you have two assign two pins for it from the arduino.