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();
}