BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help with ESP-01s and Arduino Nano


#37

ok. i will try…

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…

some methods used there:

Blynk.connectWiFi(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect() ;
Blynk.connected()


#38

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… :slight_smile: … very tired :sleeping: )

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.


#39

common reconnection to wifi and blynk server with reseting arduino!??

Arduino is executing main program, its not sollution to reset whole program only to establish connection to Wifi and blynk…is there any better way!?


#40

What do you expect? This whole IoT thing is predated by the basic Arduino by a few years or so :wink:

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 :stuck_out_tongue_winking_eye: 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 :smiley: But I suspect you have only the two Blynk based choices as mentioned before…


Nappy time for me… :sleeping:


#42

sory but in copy paste indentitation is lost…btw how to copycodehere with indentation?


#43

Blynk - FTFC


#44
/* 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;}  
}

#45

Does it work??

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.


#46

There is at least one more way to reset connection with WIFI without resetting Atmega, which I’m using:

  • I’m checking periodically (in my case 5 sec) blynk.connected()
  • If result is false, then do a hardware reset (RESET pin) on ESP.
  • wait a while, and do an initialization by means of Blynk.begin as usual…

I tried with Blynk.disconnect() but all it did was to crash my Atmega… I see no issue when using another instance of Blynk.begin()


#47

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…


#48

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…

  1. 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])’

…bla…bla…bla…“candidate expects 4 arguments, 1 provided”

so i looked in BlynkSimpleEsp8266.h and found :

                const char* domain = BLYNK_DEFAULT_DOMAIN,
                uint16_t    port   = BLYNK_DEFAULT_PORT)
    {
        Base::begin(auth);
        this->conn.begin(domain, port);
    }

The right call of function would be :

Blynk.config(wifi,auth, BLYNK_DEFAULT_DOMAIN,BLYNK_DEFAULT_PORT);

So i will try and test it further…


#49

Commands like that require the specific Libraries like

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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.

http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/esp8266/install-esp8266-core-for-arduino-ide

http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/arduino/esp8266-standalone


#50

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…

Am I using Wrong libraries?


#51

I don’t even understand your original issue anymore :stuck_out_tongue:

If you need to use an Arduino/ESP combo, then these are two libraries to use… Exactly what each one does is something I haven’t looked into yet.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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).


#52

You got point,i don’t remember also what was my orginal issue :slight_smile: :stuck_out_tongue:

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…


#53

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?


#54

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”


#55

Yes… someone else does use that similar type of method I just posted… good to know :slight_smile:

Resetting instead of switching power… even better idea :+1: as that can be done directly from a digital pin


#56

Correct! :stuck_out_tongue:

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…


#57

I think those might be some of the “unavailable” commands when using ESP in AT mode.

But Blynk.connected() will work, thus can be used to trigger reset actions.