Board: Arduino Uno
Connection : Ethernet Shield 5100
Blynk: V 0.6.0 and Iphone
I need some help in my below sketch. It is simple sync physical button example which work with and without internet and can reconnect again if internet available.
If I run it first time without internet it did not connect again but if first time run with internet and than it can reconnect after disconnect.
Second I want to reduce the waiting time to run offline when there is no internet.
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
BlynkTimer timer;
#define W5100_CS 10
#define SDCARD_CS 4
int DeviceLED = 11;
int ReCnctFlag; // Reconnection Flag
int ReCnctCount = 0; // Reconnection counter
const int ledPin1 = 2;
const int btnPin1 = A1;
void checkPhysicalButton();
int ledState1 = LOW;
int btnState1 = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V1);
// Alternatively, you could override server state using:
// Blynk.virtualWrite(V1, ledState1);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
ledState1 = param.asInt();
digitalWrite(ledPin1, ledState1);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin1) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState1 != LOW) {
// Toggle LED state
ledState1 = !ledState1;
digitalWrite(ledPin1, ledState1);
// Update Button Widget
Blynk.virtualWrite(V1, ledState1);
}
btnState1 = LOW;
} else {
btnState1 = HIGH;
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
Ethernet.begin(mac);
Blynk.config(auth);
pinMode(ledPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(ledPin1, ledState1);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
Blynk.connect();
timer.setInterval(1000L, UpTime);
}
void UpTime() {
Blynk.virtualWrite(V0, millis() / 1000); // Send UpTime seconds to App
Serial.print("UpTime: ");
Serial.println(millis() / 1000); // Send UpTime seconds to Serial
digitalWrite(DeviceLED, !digitalRead(DeviceLED)); // Blink onboard LED
}
void loop() {
timer.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (ReCnctFlag == 0) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Blynk.connect(); // Try to reconnect to the server
}); // END Timer Function
}
}
Serial output when run offline
[60731]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on Arduino Uno
[60824] Connecting to blynk-cloud.com:80
[65824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 70
UpTime: 71
UpTime: 72
UpTime: 73
UpTime: 74
// Cut , but actually continue
UpTime: 99
Attempting reconnection #1
[99823] Connecting to blynk-cloud.com:80
[104824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 108
UpTime: 109
// Cut , but actually continue
Attempting reconnection #2
[138823] Connecting to blynk-cloud.com:80
[143824] Connecting to blynk-cloud.com:80
Starting reconnection timer in 30 seconds...
UpTime: 147
Should I use like this Blynk.begin(auth, server_ip, 8080, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
in this way Blynk.begin() will stop off line working.
My old sketch is working fine when connect to internet
If I disconnect lan cable / without internet my physical button should work which working now but once internet available it should connect again.
but it is not connecting when I run without internet.
But if first time I run the sketch with lan cable connected it will work without any problem and during this time many time I disconnect and reconnect with internet and working fine.
My target is:
Physical push button work with internet and without internet.
Also get connect / reconnect with server automatically when internet available.
Thanks for your time.
I use same think with esp8266 it work perfect but with ethernet shield I change these
Ethernet.begin(mac);
Blynk.config(auth);
to work but I not succeed as esp.
It is not enough to use just Blynk.config(), you also need to isolate Blynk.run() so that it only executes when there is a Server connection, else it will stall out each loop cycle while trying to reconnect.
void loop() {
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
}
// do all other loop stuff
}
I got the solution with improve in loop logic now its working and serial output is below. Only 10 seconds delay in offline sketch due to Blynk.config(auth);. Second thing. once I connect the lan cable it pass the logic but did not connect (ref. first attempt) But connect in second attempt.
If someone can help to reduce the timeout time in first bootup so my offline sketch start to work without 60 second delay and alternate of Blynk.config(auth); if possible.
void loop() {
timer.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (!Blynk.connected() && (ReCnctFlag == 0)) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Blynk.connect(); // Try to reconnect to the server
if (Ethernet.begin(mac) == 1) {
Blynk.config(auth);
Serial.println("Successfull reconnect");
}
});
}
}
And other side when Lan cable is connected than disconnect and reconnect logic also working fine as below
You’re always going to get inconsistent results with these Ethernet shields, they are generally very poor quality and don’t behave the way you expect.
You’ll generally get faster connections if you define a static IP address for your device, as then you don’t have to wait for the DHCP server to assign an IP address and do all the handshaking stuff. It can knock a few seconds (sometimes more) off the process.
Without knowing what you’re currently doing in your void setup, it’s difficult to say what could be done to improve the connection process to Blynk, but I’m sure there are some dramatic improvements available if the logic is improved.
Personally, when I do this stuff I take a different approach.
On startup I attempt to connect, and if that fails I set a flag to show that we’re running in standalone mode.
If the flag indicates if we’re in standalone mode then a simplified void loop logic path is used, then I attempt to re-connect every x seconds. In some cases this is a variable timing system, where y number of failed attempts drops the re-connect timing to a lower frequency, and z number of attempts extends the reconnection attempt timing even further.
Thanks PeteKnight for your time. I already set the flag in my loop as you say. And definitely need to improve the logic.
I add Blynk.connect(1000); instead of Blynk.connect(); so my attempting time is reduced from 10 sec to 01 sec.
Now I want to edit the Ethernet.h that if internet is not available it should not wait for 60 seconds to time out. I see this topic few days before and trying to search.
No, you’re not using a flag to know whether Blynk is connected or not, you’re calling the Blynk.connected() function to discover that each time. You do use a flag called ReCnctFlag, but in my mind it’s use is somewhat dubious, as although Blynk.connect() is non blocking in the sense that it won’t totally prevent further code execution if it can’t connect, I don’t think that void loop execution will continue uninterrupted while the Blynk.connect() function attempts a connection (but I may be wrong about this).
Does your project actually require the use of an Arduino Uno and Ethernet shield, could a NodeMCU with Wi-Fi connection be used instead? You’d certainly get better reliability and wouldn’t have to mess around hacking Ethernet libraries.
Ethernet.begin(mac); is blocking it should run once than Blynk.config(auth); will work.
So when I run without internet it didn’t execute thats why attempting to connect is fail but if I run with internet Ethernet.begin(mac); executed in setup so reconnection attempt is succeeded with only Blynk.config(auth);. Can you explain about Ethernet.begin() .
I need more than 10 GPIOs and future expansion also need that’s why I select arduino. With Nodemcu I already get the results but maximum 05 buttons.