ESP32/ACS712 and ZMPT101B sensor can't read when connected to cayenne

Hello guys i have some problem with my IoT project, im not sure its a bug or my failure.
The problem is, when i write code for connected to cayenne platform at this part
"Cayenne.begin(username, password, clientID, ssid, wifiPassword);"
my sensor can’t read properly. but when i delete this code my sensor can read properly, can someone help me please :((
this is the full code from my project:

/***************************************************

  1. INCLUDE !!!
    ****************************************************/
    #include <SPI.h>
    #include “ZMPT101B.h”
    #include “ACS712.h”
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <CayenneMQTTESP32.h>

/***************************************************
2. DEFINE !!!
**************************************************/
/
Define OLED Display
/
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/* Define ZMPT101B */
ZMPT101B voltageSensor(14);

/* Define ACS712_30A */
ACS712 currentSensor(ACS712_30A, 27);

/* Define Cayenne */
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

/***************************************************
3. DECLARE !!!
****************************************************/

/* Declare ZMPT101B */
float VoltAC = 0;

/* Declare ACS712 */
float AmpAC = 0;

/* Declare Power */
float S = 0;
float P = 0;

/* Declare Cayenne */
char ssid = “777”;
char wifiPassword = “blackshot1212”;
char username = “595fe320-f9f9-11e9-84bb-8f71124cfdfb”;
char password = “ca1601088df82b4ba32cbda89696559250bf7743”;
char clientID = “74fc22c0-39e0-11ea-8221-599f77add412”;

/***************************************************
4. SETUP !!!
****************************************************/
void setup()
{

Serial.begin(115200);
voltageSensor.calibrate();
currentSensor.calibrate();
/* Setup Cayenne */

// /* Setup Sensor Calibrating */
// Serial.print(“Calibrating… Ensure that no current flows through the sensor at this moment”);
// // voltageSensor.calibrate();
// currentSensor.calibrate();
// Serial.println(“Done!”);

/* Setup Display */
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F(“SSD1306 allocation failed”));
for (;;);
}
delay(2000);

display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println(“MONITORING ARUS & kWh Meter”);
display.display();
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}

void loop()
{

Cayenne.virtualWrite (1, VoltAC);
/Loop ZMPT101B/
VoltAC = abs(voltageSensor.getVoltageAC() - 6) * 1.8;

/Loop ACS712/
Cayenne.virtualWrite (2, AmpAC);
AmpAC = abs(currentSensor.getCurrentAC() - 0.1) / 2.5;

//perhitungan daya Semu
S = VoltAC * AmpAC;

//perhitungan daya Aktif
P = S * 0.8;

Cayenne.loop();
display.clearDisplay();
display.setTextSize(1.5);
display.setTextColor(WHITE);

display.setCursor(0, 0);
display.println(“TEGANGAN:”);
display.setCursor(60, 0);
display.println(VoltAC);
display.setCursor(100, 0);
display.println(“VOLT”);

display.setCursor(127, 0);
display.println(“ARUS :”);
display.setCursor(60, 8);
display.println(AmpAC);
display.setCursor(100, 8);
display.println(“AMP”);

display.setCursor(254, 16);
display.println(“DAYA AKTIF:”);
display.setCursor(381, 24);
display.println§;
display.setCursor(100, 32);
display.println(“WATT”);
display.display();

Serial.print(String("U = ") + VoltAC + " V ");
Serial.print(String("I = ") + AmpAC + " A ");
Serial.print(String("S = ") + S + " VA ");
Serial.println(String("P = “) + P + " Watts”);
delay(1000);

}

This is the fail sensor

And this is the good one

can you share the code that worked without cayenne code added.

Here the code withouth cayenne mqtt, i was change the esp32 but still the sensor cant read, its just like stuck in somewhere. But when i using esp8266 the sensor can work properly with cayenne mqtt.

/***************************************************

  1. INCLUDE !!!
    ****************************************************/
    #include <SPI.h>
    #include “ZMPT101B.h”
    #include “ACS712.h”
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>

/***************************************************
2. DEFINE !!!
**************************************************/
/
Define OLED Display
/
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/* Define ZMPT101B */
ZMPT101B voltageSensor(14);

/* Define ACS712_30A */
ACS712 currentSensor(ACS712_30A, 27);

/***************************************************
3. DECLARE !!!
****************************************************/

/* Declare ZMPT101B */
float VoltAC = 0;

/* Declare ACS712 */
float AmpAC = 0;

/* Declare Power */
float S = 0;
float P = 0;
float E = 0;

/***************************************************
4. SETUP !!!
****************************************************/
void setup()
{

Serial.begin(115200);

// /* Setup Sensor Calibrating /
// Serial.print(“Calibrating… Ensure that no current flows through the sensor at this moment”);
// // voltageSensor.calibrate();
// currentSensor.calibrate();
// Serial.println(“Done!”);
voltageSensor.calibrate();
currentSensor.calibrate();
/
Setup Display */
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F(“SSD1306 allocation failed”));
for (;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println(“MONITORING ARUS & kWh Meter”);
display.display();
delay(2000);

}

void loop()
{

/Loop ZMPT101B/
VoltAC = abs((voltageSensor.getVoltageAC() - 2) * 1.8);

/Loop ACS712/

AmpAC = abs((currentSensor.getCurrentAC() - 0.1) / 2.5);

if (isnan(VoltAC) || isnan(AmpAC)) {  // Condition to check whether the sensor reading was successful or not
Serial.println("Failed to read from DHT sensor!");
return;
}

//perhitungan daya Semu
S = VoltAC * AmpAC;

//perhitungan daya Aktif
P = S * 0.8;

//perhitungan daya kW
E = P / 1000;

display.clearDisplay();
display.setTextSize(1.5);
display.setTextColor(WHITE);

display.setCursor(0, 0);
display.println(“TEGANGAN:”);
display.setCursor(60, 0);
display.println(VoltAC);
display.setCursor(100, 0);
display.println(“VOLT”);

display.setCursor(127, 0);
display.println(“ARUS :”);
display.setCursor(60, 8);
display.println(AmpAC);
display.setCursor(100, 8);
display.println(“AMP”);

display.setCursor(254, 16);
display.println(“DAYA AKTIF:”);
display.setCursor(381, 24);
display.println§;
display.setCursor(100, 32);
display.println(“WATT”);
display.display();

Serial.print(String("U = ") + VoltAC + " V ");
Serial.print(String("I = ") + AmpAC + " A ");
Serial.print(String("S = ") + S + " VA ");
//Serial.println(String("P = “) + P + " Watts”);
Serial.println(String("E = “) + E + " kW”);
delay(1000);
}

try this code:

/***************************************************

INCLUDE !!!
****************************************************/
#include <SPI.h>
#include “ZMPT101B.h”
#include “ACS712.h”
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/***************************************************
2. DEFINE !!!
**************************************************/
/ Define OLED Display/
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/* Define ZMPT101B */
ZMPT101B voltageSensor(14);

/* Define ACS712_30A */
ACS712 currentSensor(ACS712_30A, 27);

/***************************************************
3. DECLARE !!!
****************************************************/

/* Declare ZMPT101B */
float VoltAC = 0;

/* Declare ACS712 */
float AmpAC = 0;

/* Declare Power */
float S = 0;
float P = 0;
float E = 0;
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP32.h>

char wifiPassword = “blackshot1212”;
char username = “595fe320-f9f9-11e9-84bb-8f71124cfdfb”;
char password = “ca1601088df82b4ba32cbda89696559250bf7743”;
char clientID = “74fc22c0-39e0-11ea-8221-599f77add412”;

/***************************************************
4. SETUP !!!
****************************************************/
void setup()
{

Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

// /* Setup Sensor Calibrating /
// Serial.print(“Calibrating… Ensure that no current flows through the sensor at this moment”);
// // voltageSensor.calibrate();
// currentSensor.calibrate();
// Serial.println(“Done!”);
voltageSensor.calibrate();
currentSensor.calibrate();
/ Setup Display */
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F(“SSD1306 allocation failed”));
for (;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println(“MONITORING ARUS & kWh Meter”);
display.display();
delay(2000);

}

void loop()
{
Cayenne.loop();
/Loop ZMPT101B/
VoltAC = abs((voltageSensor.getVoltageAC() - 2) * 1.8);

/Loop ACS712/

AmpAC = abs((currentSensor.getCurrentAC() - 0.1) / 2.5);

if (isnan(VoltAC) || isnan(AmpAC)) {  // Condition to check whether the sensor reading was successful or not
Serial.println("Failed to read from DHT sensor!");
return;
}
//perhitungan daya Semu
S = VoltAC * AmpAC;

//perhitungan daya Aktif
P = S * 0.8;

//perhitungan daya kW
E = P / 1000;

display.clearDisplay();
display.setTextSize(1.5);
display.setTextColor(WHITE);

display.setCursor(0, 0);
display.println(“TEGANGAN:”);
display.setCursor(60, 0);
display.println(VoltAC);
display.setCursor(100, 0);
display.println(“VOLT”);

display.setCursor(127, 0);
display.println(“ARUS :”);
display.setCursor(60, 8);
display.println(AmpAC);
display.setCursor(100, 8);
display.println(“AMP”);

display.setCursor(254, 16);
display.println(“DAYA AKTIF:”);
display.setCursor(381, 24);
display.println§;
display.setCursor(100, 32);
display.println(“WATT”);
display.display();

Serial.print(String("U = ") + VoltAC + " V ");
Serial.print(String("I = ") + AmpAC + " A ");
Serial.print(String("S = ") + S + " VA ");
//Serial.println(String("P = “) + P + " Watts”);
Serial.println(String("E = “) + E + " kW”);
delay(1000);
}

CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite (1, VoltAC);
Cayenne.virtualWrite (2, AmpAC);
}

sorry, i was found the fault line sir, the pin ADC 14-27 from esp32 not work for API, and i change to pin 32-33 for sensor output, its work. but the other problem is the data not showing at widget dashboard, just appear in timestamp database


the number not change and still zero

Which code are you using. The one i gave? If it is yours then you are sending data to Cayenne from main loop. Cross check with my code. Look how i have used inbuilt CAYENNE_OUT_DEFAULT function to send data to cayenne.
For not showing data in widget, remove it and let it re add it automatically.

1 Like

ah i see
Thanks for help, the problem fixed now
Thank you very much @shramik_salgaonkar

2 Likes