SCT-013 + Blynk

Hello, I am trying to integrate the SCT-013 sensor as Blynk, however I am only getting values ​​equal to 0.

When I use Sketch without blynk the sensor returns the correct values. When I write Blynk lines the sensor values ​​are always equal to 0.

I am using ESP32 card.

Can someone help me find the problem? Thank you.

//Carrega as bibliotecas
#include "EmonLib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h> // Blynk-ESP32
#include <WiFi.h> // Wi-Fi
#include <WiFiClient.h> // Wi-Fi client 
BlynkTimer timer;

//-------- Token de Autenticação -----------
char auth[] = "XXXXX";
//-------- Configurações de Wi-fi -----------
const char* ssid = "XXXXX";
const char* password = "XXXXX";

int lcdColumns = 16;
int lcdRows = 2;
long Cont;

LiquidCrystal_I2C lcd(0x3F, lcdColumns, lcdRows);

EnergyMonitor emon1;
EnergyMonitor emon2;

#define VOLT_CAL 127

//Pino do sensor SCT
#define ADC_INPUTV 15
#define ADC_INPUTC 2

void sendSensor()
{
  emon2.calcVI(20, 1000);
  float supplyVoltage = emon2.Vrms;
  if (supplyVoltage <= 50) {
    supplyVoltage = 0;
  }
  float Irms = emon1.calcIrms(1480);
  if (Irms <= 0.19) {
    Irms = 0;
  }
  float Pot = (supplyVoltage * Irms);

  if (Cont <= 2) {
    lcd.setCursor(4, 0);
    lcd.print("Test");
    lcd.setCursor(7, 1);
    lcd.print("EMS");
    if (Cont == 2) {
      lcd.clear();
    }
  }

  if (Cont > 2) {
    Blynk.virtualWrite(V1, supplyVoltage);
    Blynk.virtualWrite(V2, Irms);
    Blynk.virtualWrite(V3, Pot);
    lcd.setCursor(0, 0);
    lcd.print("V:");
    lcd.setCursor(2, 0);
    lcd.print(supplyVoltage, 0);
    lcd.setCursor(5, 0);
    lcd.print("Vac");
    Serial.print("Tensão: ");
    Serial.println(supplyVoltage);
    lcd.setCursor(9, 0);
    lcd.print("I:");
    lcd.setCursor(11, 0);
    lcd.print(Irms, 1);
    lcd.setCursor(14, 0);
    lcd.print("A");
    Serial.print("Corrente: ");
    Serial.println(Irms);
    lcd.setCursor(0, 1);
    lcd.print("Potencia:");
    lcd.setCursor(9, 1);
    lcd.print(Pot);
    lcd.setCursor(15, 1);
    lcd.print("W");
    Serial.print("Potencia: ");
    Serial.println(Pot);
  }
  Serial.print("Contador:");
  Serial.println(Cont);
  Cont++;
  delay(100);
}

void setup()
{
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();


  Blynk.begin(auth, ssid, password);

  emon2.voltage(ADC_INPUTV, VOLT_CAL, 1.7); //PASSA PARA A FUNÇÃO OS PARÂMETROS (PINO ANALÓGIO / VALOR DE CALIBRAÇÃO / MUDANÇA DE FASE)
  emon1.current(ADC_INPUTC, 7.5); // Initialize emon library (30 = calibration number)
  timer.setInterval(2000L, sendSensor);

}

void loop()
{
  timer.run();
  Blynk.run();
}

The problem might be you’re using Cont as long, and it can go negative for a long time without proper control.
I suggest you change the code as follows:

....

//long Cont;

....
void sendSensor()
{
  // Add this unsigned long to be always positive
  static unsigned long Cont = 0;
  
  ....
   // Change this line, although not necessary as we have unsigned long, but to be sure in the future
   //if (Cont <= 2) {
   if ( (Cont <= 2) && (Cont >= 0) ) {
   ....
  // Change these following lines
  #if 1   
    Serial.print("Contador:");
    Serial.println(Cont);
    // Wrap around to avoid showing 0
    if (++Cont == 0) Cont = 3;
  #else
    // These are original lines
    Serial.print("Contador:");
    Serial.println(Cont);
    Cont++;
    // Don't use delay if not absolutely necessary 
    delay(100);
  #endif
}

1 Like

Thanks for the reply Khoih. but if the problem is really the one you mentioned Skech shouldn’t present problems without using blynk?

Why don’t you try the fix first to know if the issue gone or not?

Sometimes, in my humble experience, a very small issue in logic, especially in this case, an uninitialized variable can be anything and @ any value, depending on the code / memory / compiler, etc. It can create a totally erratic, random behaviour no one can know and control.
You try to imagine if Cont was assigned / initialized a very negative value at the boot up, how long will it take to make Cont > 2 if the increment is only 1 per 2 seconds?

Another observation is that in the call to emon2.calcVI(20, 1000), where argument 1000 (= 1s) is the timeout; there is maximum 2-second loop in that function of EmonLib.cpp:

//-------------------------------------------------------------------------------------------------------------------------
// 1) Waits for the waveform to be close to 'zero' (mid-scale adc) part in sin curve.
//-------------------------------------------------------------------------------------------------------------------------
  boolean st=false;                                  //an indicator to exit the while loop

  unsigned long start = millis();    //millis()-start makes sure it doesnt get stuck in the loop if there is an error.

while(st==false)                                   //the while loop...
  {
    startV = analogRead(inPinV);                    //using the voltage waveform
    if ((startV < (ADC_COUNTS*0.55)) && (startV > (ADC_COUNTS*0.45))) st=true;  //check its within range
    if ((millis()-start)>timeout) st = true;
  }

and

//-------------------------------------------------------------------------------------------------------------------------
  // 2) Main measurement loop
  //-------------------------------------------------------------------------------------------------------------------------
start = millis();
while ((crossCount < crossings) && ((millis()-start)<timeout))
{
}

and sendSensor() was called every 2 secs

timer.setInterval(2000L, sendSensor);

It might possibly create some timing issue. You can try to increase the timer loop such as

timer.setInterval(5000L, sendSensor);

to see if the problem disappears.
Also place more Serial.print() debugging messages to know exactly where the problem is.

But it’s always better if you post full code of your original working version as @PeteKnight suggested, so that many other people can have a look.

GPIO2 isn’t usually a good pin to use for a sensor as its connected to the onboard LED on most ESP32 Dev boards.

I assume that you’ve changed the code quite a bit to make it Blynk friendly? Maybe you should post your original working code.

Pete.