Disconnection Caused by For Cycle

Hi, i am new to Blynk and i am trying to solve flood errors caused by for cycle. Is there a way to change the for cycle and obtain the same cycle but using other way?

Your question is too generic… a for cycle can do very little or very much, depending on usage, and is NOT a Blynk function. Thus any flooding is based on what Blynk functions/commands you are using and how frequently.

Do you have some actual code that you are working on?

Sure,the code is very long but i can post the part of it causing the problem.

void accendiLedCamera()

  modgiorno = digitalRead(MODGIORNO);
  modnotte = digitalRead(MODNOTTE);
  pirCamera = digitalRead(PIRCAMERAPIN);
  if (modnotte == HIGH && modgiorno == LOW)
    if (frvalperc < 50 && pirCamera == HIGH && cont==1) fotoresistenza rileva un valore di luce basso inferiore al 50%
      Serial.println("ACCENDO LED CAMERA");
      for (int i = 0; i <= NUM_LEDS; i++)
        leds[i] = CHSV(32, 255, 255);
    else if(cont == 0 || pirCamera == LOW || frvalperc >= 50 )
       Serial.println("SPENGO LED CAMERA");
      for (int i = 0; i <= NUM_LEDS; i++)
        leds[i] = CRGB::Black;

In the main loop i called only Blynk.run() and timer.run(). I called the function accendiLedCamera by using timer.setInterval(250L, accendiLedCamera) in the setup() function.

Aside from calling Blynk.run(); I don’t see any other Blynk commands or functions… are you sure you are getting Blynk flooding errors?, or perhaps just connection timeouts?

I just called Blynk.run() in that part of code trying a stupid solution. When the program execute the for cycle the Blynk App send me “Your hardware went offline” and then reconnect fast. How can i avoid disconnection?

I just have connection timeouts Is there a way to solve this problem?

I corrected your title for relevancy of issue.

Perhaps calling the Blynk.run(); inside of the for cycle?

I just tried to run the code without Blynk.run() inside the for cycle and the problem persist.

Try putting a Blynk.run(); call at the beginning of the loop, as well as in BOTH of the for cycles.

Basically it comes down to trial and error in getting the timing just right.

You can also make some adjustments to the heartbeat timeout in your sketch, but I suspect you must be using Local Server for that to work?



I just tried both of this solutions but any of them solved the problem. But i am not using a Local Server.Need i to create a Local Server?

I am unsure of the need for limited heartbeat control… but a Local Server will be totally in your control and within your network… thus potentially faster throughput and less timeouts.

I have installed a Local Blynk Server and I have tried to run the same sketch as posted previously also running it with Blynk.run() in the main loop and in both for cycle but I still have timeout problem. Is there a way to solve this problem?

To confirm,

You have Cloned (not shared) any relevant projects from your Cloud account…

Created a new account on your Local Server (you can use same account info if you want - but it will still be independent of your Cloud Account)…

Switched both your App and the sketch over to your Local Servers IP…

Loaded cloned projects into your Local Server account…

Reissued new Auth codes from the project(s) now in your Local Server account and updated them in the now Local Server referenced sketch…

And you are still getting the exact issue??

No I haven’t shared any relevant project from my Cloud account

Yes I have created a new account on my Local Server

Yes I have switched both my app and sketch over my Local Server IP and I have also addes port forwarding rules to make the Local Server reachacle from the outside world

Yes I have cloned the same project also in Blynk App

If i upload the entire scketch I still have the same problem. But if I upload only the small part of program posted it works perfectly without problems. Can timeout problems be caused by many timer.setIntterval() functions?

As I understand it, that can be caused by just about anything that simply takes long enough for the heartbeat to fail.

I guess that even with the lowered latency of the Local server, your sketch is simply doing something it shouldn’t… at least if it expects to stay connected to Blynk :wink:

There is more flexibility with heartbeat settings and such in the Advanced local server setup, so you could try playing around with those and see what happens.

And while fixing code is not really this forum’s purpose, perhaps if you post the entire code someone just might get adventurous enough to look through it and offer further suggestions.

May I ask what MCU you are using?

1 Like

I am using an Arduino Mega 2560 with an Ethernet Shield W5100

Unfortunately anything “arduino shield”-like is beyond the scope of my expertise since I wouldn’t for the life of me toy with it over ESP8266 :stuck_out_tongue:

But in regards to the above quoted bit, that for loop will call Blynk.run way to often and fast (dont know your NUM_LEDS size) since the second operation is a quick and easy one. ( leds[i] = CHSV(32, 255, 255); )

It most likely wont hurt but I think its bad practice since it makes plenty of unnessecary calls.

I would change it to the following

for (int i = 0; i &lt;= NUM_LEDS; i++)
        if ( i % 10 = 0)
        leds[i] = CHSV(32, 255, 255);

so it only calls Blynk.run every 10th iterration,
@ 10
@ 20
@ 30
@ 40
and so on…

Or one of your choosing. Depending on the size of NUM_LEDS.
Also sprinkle your code with some more Serial.println in order to debugg when and where it failed.
Perhaps by checking if blynk is connected and just post the result and millis