I have been having issues with the connection of my Arduino to Blynk. Can I run the function Blynk.connected() in the mainloop or would this result in a high amount of requests to the Blynk server and subsequently the ban of my account? I couldn’t find how the connection status is determined by the function. Thank you!
Actually running this in the void loop() is fine, it responds with a basic boolean result and is a common method for initiating reconnection routines as needed. But there is more to it then simply checking for connection. Checking for connection and running reconnection repeatedly can get your device stuck in a loop while waiting for router reboot or something… and that is not good if you also want to run other control stuff, even when NOT connected. That is where Blynk.config() comes into play.
And running a delay() command is ALWAYS a BAD thing with a Blynk sketch… and that is where BlynkTimer comes into play.
Search this forum for reconnection routines to see how others do it.
I have a simplistic one here…
Your account doesn’t get banned… worst case it would have stopped accepting connections for a short time (a few - 24 hours??). However, Blynk has since made some changes last year that should basically prevent any such flooding anymore anyhow, at expense of void loop() cycles… kind of like a governor that slows things down when it gets too racy.
Please note that pin 3 is connected to the reset pin and used to reset my Arduino. So if this part of the code is executed, the delay statement doesn’t interfere with Blynk.
Based on your suggestions and searching the forum, I came up with the following code. Is there anything I could do better?
Is there any way to track the on- and offline time of the Arduino to get an idea how reliable it is?
I have considered tracking it on a microSD card with the Blynk.connected() method, but my Arduino Nano doesn’t have enough memory to do this at the same time with running Blynk and many of the pins are already in use for my ethernet module and a 433 MHz transmitter.
You can also look at the connection time on the app to see when the last connection was made. If you have connection problems you can use the timer to count the uptime and send it to the serial.
I think this will probably lock the void loop() into another if not connected, reconnect loop that can prevent other background code from running, or at least efficiently, while awaiting said reconnection.
Thank you! I think I’m pretty satisfied with the complexity right now. And I’m worried setting Blynk.connect to 1000l couldn’t be sufficient to establish a connection.
I wish the app would provide more precise data on this. I already check the app for the last connection, but unfortunately, this doesn’t provide me with any info on how long the connection wasn’t working before it was finally able to reconnect. Tracking this over serial is a good idea, but I assume I would have to keep a computer or phone running for a week or so to track enough data.I’ll look into this, but I would also appreciate alternative suggestions.
My example actually tracked connection attempt counts. You could easily do a little math and pass that info onto , SD card, EEPROM, array, etc. even Widget once connection was established.
Your post inspired me to look into EEPROM, which I had already forgotten about. I track the offline time now and write it to EEPROM every 12 hours or when my Arduino resets. In addition, the offline time is shown on a LCD in my Blynk app now. I’m also looking for ways on how to optimize my code to save memory (I’m using 75% rn). Thank you for your help!
it would not matter if it did not connect, The code would get a chance to run once every second and the connection would be reattempted each time that it was found to be not connected.