Hello everyone! I am running a program on Arduino cloud which will allow me to monitor the electrical parameters energy, voltage, frequency, current with the Pezem004t module and I also use a DHT11 sensor for temperature and humidity. the project is intended for a cold room. I use the ESP32 device
My program works normally, I can read all the data in the arduino cloud serial monitor. But on the dashboard I only see temperature and humidity data. No energy sensor data is displayed on the dashboard. However, I see all their data displayed in the serial monitor.
I checked everything, even the variable assignments on the dashboard. But the data from the pzem004t module is not displayed in the dashboard . I checked the link of my variables in the Arduino Cloud but the problem persists. Can anyone help me. Attached views of my project:
The Cloud Variables are declared in thingProperties.h by these lines of code:
float current;
float energy;
float voltage;
But then in the loop function of your Projet1_jun30a.ino file you declare a set of local variables with the same names:
float voltage = pzem.voltage();
float current = pzem.current();
float energy = pzem.energy();
Even though they have the same names, they are completely different variables. So when you set the value of these local variables, it has no effect on your Cloud Variables.
This is known as "variable shadowing", and is the source of many a confusing bug.
The solution is to simply use the global variables instead of declaring "shadow" local variables. So change these declarations in your .ino file:
float voltage = pzem.voltage();
float current = pzem.current();
float energy = pzem.energy();
to this:
voltage = pzem.voltage();
current = pzem.current();
energy = pzem.energy();
If you look closely, you will see that you did not create "shadow variables" for the humi and temp Cloud Variables, and this is why they were working as you expected them to.