Energy monitoring

Hi everyone, Now i try to connect my esp8266 to Blynk 2.0. After I run the code and Blynk completely connect to the server, the first reading do not follow the code, the second and third reading follow the code. The reading must be in between (0.1 - 0.36) A, if not, the reading must show zero.

I using Lolin new NodeMCU V3 microcontroller.

Here is my code.



// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_DEVICE_NAME "***"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"
#include "EmonLib.h"

BlynkTimer timer;
EnergyMonitor emon1;

int PinA= A0; //sct pins
float Power;
double Current;
float Energy = 0;
float Voltage = 240;
double noise1 = 0.10; //0.26
double noise2 = 0.36; //0.36

unsigned long lastmillis=millis();
long prevMillis=0;

void setup()
{
  Serial.begin(9600);
  emon1.current(PinA, 100);             // Current: input pin, calibration.

  timer.setInterval(1000L,measureCurrent);
  BlynkEdgent.begin();
  //delay (10000);
}

void measureCurrent()
{
 if (millis()-prevMillis>15000) // Every 5 seconds it measure the current
    {
      Current = emon1.calcIrms(5000);  // Calculate Irms only=1480
      double calcurrent=Current*0.7071;

    if (noise1<calcurrent<noise2)
   {
    calcurrent=0; 
   }
    Serial.print(calcurrent,4);             // Irms
    Serial.println("A");
    Power = calcurrent*Voltage;
    Serial.print(Power,4);
    Serial.println("W");// Apparent power
    
    Energy = Energy + Power*(millis()-lastmillis)/36000000.0;
    Serial.print(Energy,5);
    Serial.println("wh");
    lastmillis=millis();
    prevMillis=millis();
    Serial.println(" ");
    
    Blynk.virtualWrite(V0,calcurrent);
    Blynk.virtualWrite(V1,Power);
    Blynk.virtualWrite(V2,Energy);
  }
}

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

Have you tried

if ( noise1<calcurrent && calcurrent<noise2 )
1 Like

I’d use else with the if statement if I were you.

1 Like

A few tips…

When you’re posting serial monitor output, copy the data in your serial monitor (highlight the text you want and use CTRL - C to copy) and paste it into your post between triple backticks.

If you want to take a reading every 5 seconds then call your measureCurrent function once every 5 seconds, rather than calling it every 1 second then having some clunky (and actually incorrect) code to see if it’s 5 seconds since you took your last reading…

The sensible way to do that is…

if(reading <0.1 || reading >0.36)
{
  reading = 0;
}

I’d handle the process of connecting to WiFi and then to the Blynk server before starting the timer that takes thye readings, rathert than the other way around…

This:

  BlynkEdgent.begin();  
  timer.setInterval(1000L,measureCurrent);

rather than this:

Pete.

1 Like