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

Need help with ESP-01s and Arduino Nano

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

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.

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

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…

  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…

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

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?

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

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…

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”

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

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…

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.

Yes, it is working. I’m using it to periodically check the status.

I just checked my code… Blynk.connect(60); will also work on the Arduino and act as a way of reconnecting (in my case with 60 second timeout) after my routine to do so gets processed.

I’m sorry @Nixon for cluttering your topic, but…
@GunnerTechTools, as you are at testing stage… Can You test the disconnect() for me?? I’m curios where is the fault…

For me reboot follows the disconnect() directly…

Most everything I do, and have done over the last year, is at a testing stage :stuck_out_tongue:

But I do seem to recall that Blynk.disconnect() was troublesome with the Arduino… if I get the clarity to test further I will… I just don’t want to mess up my running code right now.

1 Like

I do not know if this is right topic any more but i detect from where my problems come from…
I am using this library for standalone Arduino:

and when I only include IRremote.h Blynk.begin,Blynk.connectWiFi and Blynk.connect do not work…
but when i comment IRremote.h Everything works like charm.

IRremote.h Uses timer1 or timer2 selectable, in any case selected same thing blink connection functions do not work.
I tried changing pins for SoftwareSerial same thing…

I suspect that IRremote.h is conflicting with something…(related to blynk or software serial…)
Did anyone experienced such problems or have any idea how to work arround?

I solved previous issues by disabling blynk.timer, i manualy check for connection…

But now I am back at the beggining.
Let me summarize:

I’am using Esp-01 to connect arduino nano to wifi.
i am using this libraries:
ESP8266_Lib.h
BlynkSimpleShieldEsp8266.h

I reset ESP-01 to factory settings with wifi.restore() - successfully
Set baudrate of ESP-01 to 9600 into its flash wifi.setUart(9600,3) in ESP flash - sucessfully

I try to set DHCP on with wifi.setDHCP(1,1,1) and try to set DHCP off with wifi.setDHCP(1,0,1) in ESP flash
I try to set fixed IP with wifi.setStationIp(“192.168.1.100”,“192.168.1.254”,“255.255.255.0”,1) in ESP flash

In every case i connected to WiFi but not on Blynk Server.

this is how i connect:

   Blynk.config(wifi,auth);
   Blynk.connectWiFi(ssid, pass);
   Blynk.connect();

I can confirm that Blynk.connectWiFi(ssid, pass) successfully connects.

Blynk.connect();
First goes in BlynkProtocol.h and executes connect function and returns FALSE
Then goes in BlynkSimpleShieldEsp8266.h and executes connect and return FALSE

So i can not connect to Blynk server… What could be the problem?