So I’m working on integrating Blynk for monitoring a high-reliability system (aeroponics) and I’m trying to figure out how to keep the timer running and keep the control loop working during internet outages.
All the sensor data is local and Blynk is just being used for remote monitoring and variable adjustment. With legacy Blynk the whole system would stall until reconnection, posing a hazard to the plants in the system.
Is there a connection management routine in new Blynk for dropped connections or should I implement the system controller on another microcontroller and link the two with hardware serial?
Blink Legacy and Blynk IoT are both capable of working well in offline e mode, if you write your code well.
The primary starting point is to use Blynk.config and Blynk.connect rather than Blynk.begin. This is because Blynk.begin is a blocking function, and code execution will cease if a connection to WiFi or the Blynk server can’t be established.
It’s something that’s been discussed many times on this forum, you just need to do a quick search.
Ah, I’d seen something to that effect but couldn’t get it working and wondered if anything had changed with the new version. Documentation between versions is confusing me a lot, but I’ll keep trying. I’m guessing Blynk timers are entirely OTA and I’ll need to use hardware timers to achieve an offline mode?
I’m meeting the same problem as I had on old Blynk, though. I have the routine configured like such, but when I intentionally put a wrong ssid in, the Blynk.connectWiFi() command will block the loop forever trying to connect…
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example shows how to keep WiFi connection on ESP8266.
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "YourTemplateID"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
// check WiFi connection:
if (WiFi.status() != WL_CONNECTED)
{
// (optional) "offline" part of code
// check delay:
if (millis() - lastConnectionAttempt >= connectionDelay)
{
lastConnectionAttempt = millis();
// attempt to connect to Wifi network:
if (pass && strlen(pass))
{
WiFi.begin((char*)ssid, (char*)pass);
}
else
{
WiFi.begin((char*)ssid);
}
}
}
else
{
Blynk.run();
}
}
No, not on its own. You need to do a loop (but not a while loop) that tries to connect maybe 20 times then sets a flag for online/offline mode depending on the outcome.
That’s not a very neat or efficient way to do it, and the logic is flawed because you are assuming that a WiFi connection automatically equals a Blynk server connection.
You should be using timers and two flags to test your online status - set when WL_CONNECTED and Blynk.connected are true.
I don’t believe in explaining how to re-invent the wheel when this topic has been discussed in great detail numerous times before, and I’m not going to spend time doing your searches for you!
From a logic perspective it’s better, but I’d do it with a timer rather than a millis comparison, and as I said earlier it’s better to use a for loop rather than a while loop.