To run Blynk.run(); only when wifi connection is established…
Check if wifi connection is established and if not try to establish it manually (with some timeout).
Check if blynk is connected to cloud…
For my Mega and ESP-01, I use the following… It may not be perfect, but it works (I think I got it all here… … very tired )
char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx"; // For Local Server
//Blynk.begin(auth, wifi, ssid, pass, server, 8442); // For Local Server
Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442); // For Cloud Server
BLYNK_CONNECTED() {
Blynk.syncAll(); // runs whenever Blynk reconnects to server.
}
void loop()
{
timer.run();
if (Blynk.connected()) {
Blynk.run();
} else { // if not connected
reconnectBlynk();
}
}
void reconnectBlynk() {
Blynk.connect(60); // reconnect to server
if (Blynk.connected()) {
} else {
softReset(); // Should? work with most Arduinos.
}
}
void softReset() {
asm volatile (" jmp 0"); // resets Arduino
}
I also tie my Arduino reset pin to the ESP reset… not sure how much that is required… but I do it.
What do you expect? This whole IoT thing is predated by the basic Arduino by a few years or so
In AT mode the WiFi portion is just a simple WiFi to Serial adapter hacked onto the Arduino brains, so any “Am I connected? What do I do?” process happens in the Arduino.
Of course… use an ESP as a standalone device Full control over the Wifi interface and running sketch.
There is always the possibility of other hacking methods… someone just sent a car into space, so anything is possible with enough effort But I suspect you have only the two Blynk based choices as mentioned before…
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MyAuth";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MyNetwork";
char pass[] = "MyPass";
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
//SoftwareSerial ESPserial(2, 3); // RX | TX
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate: 115200
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
timer.setInterval(11000L, CheckConnection); // check if still connected every 11s and reconnect if not
}
void loop()
{
if(Blynk.connected()){
Blynk.run();
}
timer.run();
}
void CheckConnection(){
if (!Blynk.connected()) {
// if not connected
if(WiFiStatus())
Blynk.connect(60);
else{
Blynk.connectWiFi(ssid, pass);
Blynk.connect(5000);
}
}
}
bool WiFiStatus()
{ String response = "";
EspSerial.print("AT+CIPSTATUS");
long int time = millis();
while( (time+2000) > millis())
{
if(EspSerial.available()) // check if the esp is sending a message
{
char c = EspSerial.read(); // read the next character.
response+=c;
}
}
Serial.print(response);
if(response.indexOf("STATUS:5")>-1){ Serial.println ("Not Connected -> false");return false;}
if(response.indexOf("STATUS:2")>-1){ Serial.println ("Connected -> true");return true;}
}
I don’t think one has any normal access to WiFi control with an ESP in AT mode… which is part of the whole issue.
But I also wonder if processing AT commands, while in script, over the same serial line that Blynk uses for link and communication, will interfere… as it can with USB-link and serial prints to the IDE monitor.
It shouldn’t. I haven’t tried it with Blynk, but many, many days ago (or years now…) I did it with a plain UNO cloe and a BT receiver. No issue on serial comm. But it MIGHT depend on paired Serial companion. BT was working, while ESP doesn’t have to…
No, it doesn’t work as expected…
But the problem is not in AT commands,or part of the code which is sending them,Esp responds on them as expected…
First problem is that Blynk.begin(auth, wifi, ssid, pass); if there is no network available,or router is turned down,or non existing ssid, halts,freaze arduino.
2.Second problem is that Blynk.connectWiFi(ssid, pass); doesn’t work.
I’m trying to make work arround but the problem i encountered is that
compiler dont accept Blynk.config(auth);…
no matching function for call to ‘BlynkWifi::config(const char [33])’
But those libraries are meant to run ON an ESP, not just communicate to one via serial (AT mode)
Again, when in AT mode, an ESP is just a rather dumbed down WiFi to serial adapter. So if you want full control, you need to compile a sketch ON it with with the full Arduino Core for ESP… as a standalone device.
Gunner I’am aware of this, but that is not option for this project i’am building,
because main processor on pcb ic Arduino and another ESP-s with more pins are not drop in replacement for Arduino.
As I can see they are not pin compactible and question is if they are also code compactible with the rest of my Arduino code.
So i should rewrite code and reroute ad make new pcbs…(maby, in future but for now i’m trying to find best working situation…)
To be precise for my code (Arduino + ESP) I’am using: #include <ESP8266_Lib.h> #include <BlynkSimpleShieldEsp8266.h>
ESP8266_Lib.h is completly designed to work with AT commands, as I can see.
And BlynkSimpleShieldEsp8266.h is using calls from another Blynk libraries…
But in this mode, the ESP is running AT firmware and this will not respond (as far I a have tested) with certain commands and methods from Blynk’s connection management options… and since those commands are not available unless you use a different library (which is ONLY meant for standalone ESP, running the Arduino-core)… and that is something you don’t/can’t switch too right now, then your choice is the choice of one.
So as best as I can repeatedly state… you must use Blynk.begin() and if disconnected you pretty much need to reset the Arduino in-order to start the whole connection process from the beginning, as I have already shown can be done in an earlier post.
This is really not a big issue when combined with Blynk.sync_() commands to restore last know settings with Blynk upon re-connection. I use it all the time in my main programming test bench setup (ESP-01 and Mega 2560).
You got point,i don’t remember also what was my orginal issue
It is big issue because arduino in my project is doing another tasks which are critical and can not be hard reset without proper shut down procedures because lots of electronic is attached on Arduino as main brain, and control through wi-fi is optional not main feature.
There is another way i am working on, and that is rewriting BlynkSimpleEsp8266.h
so that Blynk.begin does not freeze any more…
I don’t recall if I have tried part of this method yet… but I suppose that with a check loop that monitors Blynk connection, you could initiate a reboot of just the ESP by controlling the power of the ESP from a pin on the Arduino (via transistor, etc… not direct power feed) which will then try to reconnect to the last WiFi connection upon reboot, and then you could rerun the Blynk.begin() command… all in the check loop?
The other option, I’m using is to reset only the ESP board, and after a while (I’m using 3 secs) start the usual Blynk.begin()… And… yes… this method works (mostly), but I wouldn’t call it “optimal”. Overall the AT-way (ESP shield) is less than optimal. I’m using it, but it is a kind of “test bench”
P.S.: I tried to use Blynk.connect() and Blynk.disconect() but the latter just reboots my Atmega32. Can’t confirm it is Blynk’s or Arduino’s board definition fault made by Mighty’s, but anyway disconnect() DOES.NOT WORK for me…