Disconnect after 1 cycle setup sending 8 value of data

i have tried to connect my arduino mega and esp8266-01 to my blynk, it work and can connect, but after 1 cycle of setup sending 8 value of sensor reading it automatically disconnect and then connect again, thats happen again and again.

I saw the serial monitor, written heartbeat timeout when it disconnect, and my ping reach up to 8000, i think there is something wrong with my code in timer.setinterval, but i dont know how to fix it, Please write me any tips.
Thank you

this is my code,

#include <ESP8266_HardSer.h> 
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#define EspSerial Serial
#define ESP8266_BAUD 115200
#include <SimpleTimer.h>

ESP8266 wifi(EspSerial);

char auth[] = "xxx"; // Put your Auth Token here. (see Step 3 above)
int offset =0.1;// set the correction offset value
int volt1; //PIEZO
double voltage1;
int volt2; //WIND
double voltage2;
int volt3; //SOLAR
double voltage3;
int volt4; //BATERAI
double voltage4;

SimpleTimer timer;

void setup()
{
 Blynk.virtualWrite(V0, HIGH);
 Serial1.begin(9600); // See the connection status in Serial Monitor
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, "xxx", "xxx");
  
  timer.setInterval(1000L, PiezoVoltageRead);
  timer.setInterval(1000L, WindVoltageRead);
  timer.setInterval(1000L, SolarVoltageRead);
  timer.setInterval(1000L, PiezoCurrentRead);
  timer.setInterval(1000L, WindCurrentRead);
  timer.setInterval(1000L, SolarCurrentRead);
  timer.setInterval(1000L, sumvoltage);
  timer.setInterval(1000L, SumCurrent);
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer
}

void PiezoVoltageRead() 
{
  volt1 = analogRead(A1);// read the input
  double voltage1 = map(volt1,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltage1 /=100;// divide by 100 to get the decimal values

  Blynk.virtualWrite(V1, voltage1);
  delay(500);
  //int callFunction2 = timer.setTimeout(1000, WindVoltageRead);
}
void WindVoltageRead() 
{
  volt2 = analogRead(A2);// read the input
  double voltage2 = map(volt2,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltage2 /=100;// divide by 100 to get the decimal values

  Blynk.virtualWrite(V2, voltage2);
  delay(500);
  //int callFunction3 = timer.setTimeout(1000, SolarVoltageRead);
}
void SolarVoltageRead() 
{
  volt3 = analogRead(A3);// read the input
  double voltage3 = map(volt3,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltage3 /=100;// divide by 100 to get the decimal values

  Blynk.virtualWrite(V3, voltage3);
  delay(500);
  //int callFunction4 = timer.setTimeout(1000, PiezoCurrentRead);
}
void sumvoltage() 
{
  volt4 = analogRead(A4);// read the input
  double voltage4 = map(volt3,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltage4 /=100;// divide by 100 to get the decimal values

  Blynk.virtualWrite(V4, voltage4);
  delay(500);
  //int callFunction8 = timer.setTimeout(1000, SumCurrent);  

}
void PiezoCurrentRead() 
{
float current1 = 0;
for(int i = 0; i < 1000; i++) {
current1 = current1 + (.0264864864864865 * analogRead(A5) - 13.5135135135135);
delay(1);
}
  Blynk.virtualWrite(V5, current1);
  delay(500);
  //int callFunction5 = timer.setTimeout(1000, WindCurrentRead);  
}
void WindCurrentRead() 
{
float current2 = 0;
for(int i = 0; i < 1000; i++) {
current2 = current2 + (.049 * analogRead(A6) - 25);
delay(1);
}
  Blynk.virtualWrite(V6, current2);
  delay(500);
  //int callFunction6 = timer.setTimeout(1000, SolarCurrentRead);  

}
void SolarCurrentRead() 
{
float current3 = 0;
for(int i = 0; i < 1000; i++) {
current3 = current3 + (.049 * analogRead(A7) - 25);
delay(1);
}
  Blynk.virtualWrite(V7, current3);
  delay(500);
  //int callFunction7 = timer.setTimeout(1000, sumvoltage);  

}
void SumCurrent() 
{
float current4 = 0;
for(int i = 0; i < 1000; i++) {
current4 = current4 + (.049 * analogRead(A8) - 25);
delay(1);
}
  Blynk.virtualWrite(V8, current4);
  delay(500);
}

All your timed functions are trying to start at the exact same time… why?

First… you could easily run all 8 functions, sequentially, on one timer if you really want to.

Second… when tying to run seperate functions simultaneously, you are causing the processor to get confused and drop the connection.

Stagger the timers so that each one starts a little later then the prior one… but also giving its function time to complete.

Also try not to have timers match on alternating cycles… like a timer every 1 second another every 5 seconds and a third every 60 seconds… as some and/or all will eventually try to run at the same time every few cycles.

For example, running all timers within 200 ms from start to finish, and repeating the process every second, try this… assuming 20 ms is enough time for each function to complete its task before the next one runs…

  timer.setInterval(1000L, PiezoVoltageRead);
  timer.setInterval(1020L, WindVoltageRead);
  timer.setInterval(1040L, SolarVoltageRead);
  timer.setInterval(1080L, PiezoCurrentRead);
  timer.setInterval(1100L, WindCurrentRead);
  timer.setInterval(1120L, SolarCurrentRead);
  timer.setInterval(1140L, sumvoltage);
  timer.setInterval(1160L, SumCurrent);

You can test if 20ms is enough by having print statements… in your case Serial1.println() statements, at the beginning and end of each function… then if one function starts before the other ends, you will see the print statements out of order. Be aware that the print statements themselves will take a small amount of time to process.

i have tried the code you given to me, and it still same happen, after 2 cycle of setup, my blynk will disconnect, and my ping is about 8600.

if i try your first statement run all 8 functios on one timer, how the code it will be ??

and now i get new problem, that my virtual pin 4 display the value exaclty same with my virtual pin 3, i am very sure that my labeled value on blynk i set diferrent, V3 and V4.

You could also try spacing the timing out more than 20ms for each :wink:

There are a few ways… one is adding in another function that the timer calls…

timer.setInterval(1000L, groupedFunctions);
void groupedFunctions(){  // A group of functions running sequentially 
PiezoVoltageRead();
WindVoltageRead();
SolarVoltageRead();
PiezoCurrentRead();
WindCurrentRead();
SolarCurrentRead();
sumvoltage();
SumCurrent();
}

Or what I call a Lambda timer function… I like this one for some timers as it keeps everything, timer, functions, commands, loops, etc. ‘grouped’ into one lump of code. EDIT - I had mistakenly left the groupedFunctions() in there… not needed with the Lambda :stuck_out_tongue: so I removed it.

timer.setInterval(1000L, []() {  // Run this Lambda function every second
PiezoVoltageRead();
WindVoltageRead();
SolarVoltageRead();
PiezoCurrentRead();
WindCurrentRead();
SolarCurrentRead();
sumvoltage();
SumCurrent();
});

By the way… these must be old libraries… as I don’t recall seeing them before… My Mega/ESP-01 combo uses these…

#include <ESP8266_Lib.h>  // ESP-01 Link
#include <BlynkSimpleShieldEsp8266.h>  // ESP-01 Link

I highly recommend you update your sketch, libraries, App, etc. to the latest current versions.

And less critical, but BlynkTimer has superseded SimpleTimer with some added features and is now built into the Blynk libraries.