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