Hi,
Blynk randomly get offline and about once a day won’t reconnect to online.
When I see such a problem, I restart power with sonoff and everything starts working normally.
But I would like to solve this problem.
After searching the forums, I wrote such a code
What am I doing wrong?
//blynk auth information
#define BLYNK_TEMPLATE_ID "xxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxx"
// Send Blynk user messages to the hardware serial port…
#define BLYNK_PRINT Serial
//blynk library
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//blynk auth
char auth[] = BLYNK_AUTH_TOKEN;
// wifi password, login
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";
char server[] = "blynk.cloud";
unsigned int port = 8080;
// Define your ESP8266 baud rate:
#define ESP8266_BAUD 115200
// Tell the Blynk library to use the Hardware Serial port 1 for WiFi..
ESP8266 wifi(&Serial3);
BlynkTimer timer;
//Offline status
int OfflineStatus =1;
bool FirstTimeIsEmpty=true;
int TryToConnectAgainCount=0;
void setup()
{
Serial.begin(115200);
// Initialise the Hardware serial1 port…
Serial3.begin(ESP8266_BAUD);
delay(10);
wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
Blynk.config(wifi, auth, server, port);
Blynk.connect();
}
void TryConnectServer()
{
if(Blynk.connected())
{
return;
}
wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
Blynk.config(wifi, auth, server, port);
while (Blynk.connectWiFi(ssid, pass)==false)
{
TryToConnectAgainCount++;
//delay(50);
Serial.println("tekrar yoxlanis");
if(TryToConnectAgainCount==3)
{
TryToConnectAgainCount=0;
break;
}
}
Blynk.connect();
}
void loop()
{
Blynk.run();
timer.run();
if(Blynk.connected()&& OfflineStatus==0)
{
OfflineStatus=1;
FirstTimeIsEmpty=true;
} else if (!Blynk.connected() ) i
{
if(FirstTimeIsEmpty==true)
{
FirstTimeDisconnection=millis();
FirstTimeIsEmpty=false;
}
if(FirstTimeIsEmpty==false)
{
if(millis()-FirstTimeDisconnection >=interval)
{
OfflineStatus=0;
TryConnectServer();
}
}
}
}
/*************************************************************
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();
}
}
Yes,
Because sometimes the server does not respond when the button is pressed. And sometimes on automation system doesn’t respond and this sync helps the system be more durable.
Hi,
Explanation :
I realized that sometimes the virtual pin gets defaults after disconnecting from the server. For example, the blynk automation activates some virtual pin, but after disconnecting (it can be a few seconds) from the server, the virtual pin is deactivating immediately. After reconnection to the server automatically that pin does not sync and automation is losing its act. This sync rechecks that pin every 11 seconds.
Have any other suggestions?
If have, please share.
In addition, it is suspected that the pin will not turn on or turn off after pressing the virtual button. But this synchronization makes the system more robust, and durable.
BLYNK_CONNECTED() and Blynk.connected() are very different and are used in very different ways.
You are doing the wrong thing with your code, you need to fix it.