"Device was disconnected" is keep showing while i add in more code

I used WeMos to do a monitoring and controlling utilities system. i used a TA12-100 non-invasive current sensor to detect the AC current and do some calculation to obtain the power consumption in kWh. First, i thought my WeMos have problem because the "Device was disconnected " is keep appearing and cannot giving the output through pin. After that i start from the simplest Blynk esp8266 standalone example again, and it works well with instant reaction, hence it prove this is not hardware problem.

Then, i try and error by adding some code lines in a time, and found that whenever the code “nVPP=getVPP” to call the function, it cause the "Device was disconnected " to appear, and blynk cannot function. Hence, i guess the coding in Arduino have cause the problem, and maybe is the limitation or restriction from Blynk system? Please help me.

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "7485d0cf1db745cfaba289405943f9a0";
char ssid[] = "SNex-3N";
char pass[] = "XingNanChuFang";


const int currentsensor = A0;    // pin that the sensor is attached to

float nVPP;
float nCurrThruResistorPP; // Peak Current Measured Through Resistor
float nCurrThruResistorRMS; // RMS current through Resistor
float nCurrentThruWire;     // Actual RMS current in Wire
float powerConsp;         //power consumption in kWh
float ElecCharge;         // electricity charged in RM

void setup() {

  Blynk.begin(auth,ssid,pass);
  Serial.begin(115200);
  pinMode(currentsensor, INPUT);
}

void loop() {

  Blynk.run();
  Blynk.virtualWrite(0,powerConsp);
  Blynk.virtualWrite(1,ElecCharge);
  Blynk.notify("Mind your budget!");  
    
  nVPP=getVPP();    //call function to read analog value

  nCurrThruResistorPP = (nVPP/200.0);    //convert peak voltage to peak current(A)
  nCurrThruResistorRMS = nCurrThruResistorPP*0.707;  //convert peak current to current RMS
  nCurrentThruWire = nCurrThruResistorRMS*1000;   //convert to true current RMS with the ratio 1000:1

  powerConsp = nCurrentThruWire*240/1000;   
  //calculate power consumption.Assume V=240rms.divide 1000, to convert W to kW.
  
  ElecCharge = powerConsp*0.218;
  
  Serial.print("Current through wire : ");
  Serial.print(nCurrentThruWire,5);
  Serial.println("A RMS");

  Serial.print("Power consumption : ");
  Serial.print(powerConsp,3);
  Serial.println("kWh");

  Serial.print("Electricity charged : RM ");
  Serial.println(ElecCharge,2);

  Serial.println();

}

float getVPP()
{
  float result;
  float readValue;
  float maxValue = 0;
    uint32_t start_time = millis();
    while ((millis()-start_time)<1000)
    {
      readValue = analogRead(currentsensor);
      if (readValue>maxValue)
      {
        maxValue=readValue;
        }
      }

    
     result = (maxValue*3.3)/1024;

     return result;
}

Most sketches for Blynk have just 2 lines in the loop() and this topic has been covered many times on the forum.

Your loop() is dreadful as you are trying to send thousands of commands per second to the Blynk server and to Serial Monitor.

One of the 6 libraries you installed to use Blynk is SimpleTimer and must be used to time events.

1 Like

Thanks @Costas for your instant reply. I have solved the problem.