[SOLVED] Indicator led for ESP8266 connecting to blynk

After quite a long time I finally have some time to use Blynk again. This also means that i havent programmed in a really long time, so maybe it is just a really stupid question :slight_smile: .

I have an ESP8266-12 hooked up to a relay switching my lights on my room. This all works perfectly, but i want something (a led) indicating if the ESP is not connected to the blynk server. I have tried a few different things, but i cannot get it right.

i have tried using while (Blynk.connect() == false) { but this only seems to stall the connection.

using BLYNK_CONNECTED() { worked, but this only does something when it connects.

is there some function like BLYNK_DISCONNECTED(){ or something?

many thanks!

Bas Peter

You could use a timed function with simple timer and do an if(blynk.connected == false) { led.on(); } and call that every 5 seconds or something? I’d be happy with that :slight_smile:

yes, that was my first thought, but when i try this:
void Connecting() { if (millis() > connectCount) { orangeState = !orangeState; digitalWrite(orange, orangeState); connectCount = connectCount + 1000; } } void loop() { Blynk.run(); if (Blynk.connect() == false) { Connecting(); } }

the ESP keeps connecting like this:
[209525] Connecting to cloud.blynk.cc:8442 [209704] Ready (ping: 1ms). [214705] Connecting to cloud.blynk.cc:8442 [214886] Ready (ping: 1ms). [219887] Connecting to cloud.blynk.cc:8442 [220101] Ready (ping: 0ms). [225102] Connecting to cloud.blynk.cc:8442 [225281] Ready (ping: 1ms).

for some reason i could not get the simple timer thing working. (it was also not included in the blynk library, i dont know is this is normal) so i just wrote the timer thing myself.

Bas Peter

Why not turn led on before connecting to Blynk then turn led off when connected?
Or better still turn led on once connected to show your connected seems more logical?

No matter how you do it, if it works, it works!

The SimpleTimer is indeed an external library and it is VERY handy. I suggest you download it from GitHub (I think it’s there) and read a bit into it. It save a bunch of headaches when working with timers. Especially with multiple timers it comes in very handy.

if (Blynk.connected()) { digitalWrite(LED1, HIGH); digitalWrite(LED0, LOW); } else { digitalWrite(LED0, HIGH); digitalWrite(LED1, LOW); }

This works but ideally it should be callback inside Blynk.run() function to check state. But this works it just returns connection status without pinging or anything like that so you can easily put it inside main loop.

Just be careful not too overload the processor. If you experience issues with Blynk connecting, it may be better to use the SimpleTimer solution.

Lol you are not overloading processor here look what this function (Blynk.connected()) is doing. Also digitalWrite in Arduino is so idiot proof so i doesn’t write anything to register if the state is the same.

99% of the cases it will work fine, but if you start fiddling with it it may introduce other symptoms. I’m still for simplicity of course, but I also like good code and from my experience it’s just best to leave the main loop alone and just initialize Blynk and timers.

It works, it wont cause issues. Read the blynk code. Developers use it to check status of the connection how the hell it’s suppose to introduce issues if they use it their own functions.

Even more check LighBlueBeanRGB in examples

You could act normal for one, this is not really nice.

What I mean to say is that introducing code in the main loop can cause issues with Blynk.

Well at the beginning I wrote to look at Blynk libraries you didn’t from what I see. Apart from that this code is used on official examples and I use it as well without issues for days.
Yet you are telling me it can cause issues what can I say…

@conkerkh please stay away from insulting other people. We are building a community of collaborators, where people have some common goals.

We all might know something and still might not know a lot of other things. So, when expressing your opinion - stay polite and respect other’s ones.

I don’t want to be a “nagging finger”, but you know what I mean. Use a different tone, pls. OK?


Sure sorry. We misunderstood each other.

Thank you.
Also, @vshymanskyy could solve your debate easily :slight_smile: I hope

Hi all, actually I think @conkerkh 's code is all right.
Yes, in general, putting arbitrary code in the loop is NOT OK, as far as it interferes with Blynk communication.
But it doesn’t look to be the case.

Np, I was just trying to help people build better code so they won’t run into issues later on. I’m trying to adhere to the best practices I learned from, amongst others, @vshymanskyy, and try to convey them to others :slight_smile: