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ā€¦
@Gunner, 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?