ESP8266 with Arduino Leonardo

Hello all,

I just recently bought a ESP8266 wifi shield that I intend to use with my Leonardo. I have some experience with arduino but I am running into some issues with my program. I ran the ESP8266 shield demo and connected to my wireless network, then I began integrating a simple RGB LED control to start with a small project. I found some code from an example and added some modifications. I placed three buttons in my Blynk app to control the RGB LED.

As I mentioned earlier my shield is successfully connected to my wifi and stacked on my leonardo, because the blue LED is solid. But the blynk app says the arduino Leonardo is not in the network. So I know something is missing in my code but I am not sure. Like I said earlier, I am just using three buttons on the blynk app to control and RGB LED from my Leonardo with and ESP8266 shield. I know my loop code needs to read the virtual switches but I am also unsure on how to do that.

Any help/guidance would be greatly appreciated. Thank you all in advance!

I blanked out my wifi information, but here is my code:

//#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

/*Defines*/
//IO PINS
#define RED_LED_PIN         2
#define GREEN_LED_PIN       3
#define BLUE_LED_PIN        4
 
//RED LED
#define RED_LED_ON          digitalWrite(RED_LED_PIN,HIGH)
#define RED_LED_OFF         digitalWrite(RED_LED_PIN,LOW)

//GREEN LED
#define GREEN_LED_ON        digitalWrite(GREEN_LED_PIN,HIGH)
#define GREEN_LED_OFF       digitalWrite(GREEN_LED_PIN,LOW)

//BLUE LED
#define BLUE_LED_ON         digitalWrite(BLUE_LED_PIN,HIGH)
#define BLUE_LED_OFF        digitalWrite(BLUE_LED_PIN,LOW)

//YELLOW LED
#define YELLOW_LED_ON       RED_LED_ON;GREEN_LED_ON
#define YELLOW_LED_OFF      RED_LED_OFF;GREEN_LED_OFF

//MAGENTA LED
#define MAGENTA_LED_ON      RED_LED_ON;BLUE_LED_ON
#define MAGENTA_LED_OFF     RED_LED_OFF;BLUE_LED_OFF

//CYAN LED
#define CYAN_LED_ON         BLUE_LED_ON;GREEN_LED_ON
#define CYAN_LED_OFF        BLUE_LED_OFF;GREEN_LED_OFF

//WHITE LED
#define WHITE_LED_ON        RED_LED_ON;GREEN_LED_ON;BLUE_LED_ON
#define WHITE_LED_OFF       RED_LED_OFF;GREEN_LED_OFF;BLUE_LED_OFF

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*********k";
char pass[] = "*******";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{ 
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  //Set LED output pins
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BLUE_LED_PIN, OUTPUT);
  
  Blynk.begin(auth, wifi, ssid, pass);
}


void loop()
{
  Blynk.run();
  if(RED_LED_PIN==1)
  {
    RED_LED_ON;
  }
  else if(GREEN_LED_PIN==1)
  {
    GREEN_LED_ON;
  }
  else if(BLUE_LED_PIN==1)
  {
    BLUE_LED_ON;
  }
  else
  {
    WHITE_LED_OFF;
  }
  
}

Glad you worked out how to format the sketch.

What does Serial Monitor show when you remove the first 2 characters of your sketch?

Just 2 rows in a typical Blynk loop().

Look up SimpleTimer and BLYNK_WRITE() in the docs.
BLYNK_WRITE(Vx) Writes a 1 to Virtual pin x if you assign a Button to Vx and press the button for ON (0 for OFF). Probably best to start with your buttons in Switch mode and then work up to Push mode.

I removed the comment on the first line and ran my program and saw nothing on the serial monitor.

Those docs should help greatly! I did put my buttons to switch mode in my app. I appreciate your response!

That can’t be right.
Your first line now looks like this, right?

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

Do you have Serial Monitor set to 9600 baud rate to match that of your sketch?

Without a working Serial Monitor it is very difficult to debug any sketch.

Yes, that is my first line exactly!

I think I opened my serial monitor too late, because now I am receiving data from the serial monitor. It shows: [1531] Failed to disable Echo

You should open SM before you upload your sketch.
Nightmare scenario. If you search the forum for “echo” you will see loads of Blynkers with similar problems.
Until you fix it there is no way forward with the Leonardo.

Is the esp8266 able to stack directly onto the Leonardo? Because I don’t know how the shield could stack on top and still cross the TX and RX pins. I currently have the pins 8 and 9 of the leonardo level shifted to 3.3V and connected to the SW_TX and SW_RX of the shield.

As for the echo problem, you’re saying I should update the firmware?

I find the sparkfun documentation to be uncharacteristically poor for this product.

It was also my understanding that this shield runs on 3.3V and requires a lot of current. So I also tried powering the shield externally on a breadboard instead of stacking it on my leonardo. I put 3.3V from a voltage regulator to VIN and it did not power up. The only way I could get it to power up was from a 5V regulator to the 5V pin. I tied the board gnd to the power supply -.

Could you provide me with any clarification on this subject as well?

Not specifically there are a bunch of reasons for the “echo nightmare”.

Give us a link to the product so we can check it out as there are lots of “ESP shields”.

https://www.sparkfun.com/products/13287

This is the link to the shield.

Mixed reviews of the product.

The hook up guide looks quite detailed.

From your OP I believe you tested the shield demo.

It might be that the AT firmware is not suitable for Blynk but Blynk and Sparkfun are partners (not for this shield though) so you would hope it ships with suitable AT firmware.

If you are using pins 8 and 9 I think you need to change your sketch to Software serial from hardware serial and change (2, 3) in the sketch to (8, 9) (could be 9, 8).

I wouldn’t change the AT firmware at this stage and see how you go with the correct sketch.

The hookup guide is quite lengthy, but I have had little success.

I am using pins 8 and 9 because they line up with the SW_RX and SW_TX pins of the shield.

Yes, I have tested the shield demo.
The serial monitor shows the following when I run the demo and press a key:

ESP8266 Shield Present
Mode set to station
Connecting to **********
Connected to: **********
My IP: ************

Press any key to connect client.

Move back to the Blynk sketch now with the required mods.

That is my confusion! I run the demo as is and enter my wifi information and it connects perfectly.

If I go to my Blynk app it says your Arduino Leonardo is not in the network.

That is why my code from the original post is a combination of the Sparkfun ESP8266 Shield Demo and the Blynk Wifi ESP8266 Shield examples.

You need to be using softserial and it is (8, 9) not (9, 8).

For test purposes rip all your stuff out of the code, get connected to Blynk and then add back the code a bit at a time.

Paste the correct sketch here if you want me to look through it but it is basically this https://github.com/blynkkk/blynk-library/blob/master/examples/Boards_WiFi/ESP8266_Shield/ESP8266_Shield.ino with a minor tweak.

This is my code with all excess removed, serial monitor still shows failed to disable echo [1531]:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h> 

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "***";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{ 
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);
}


void loop() {
  
  Blynk.run();
  
}

Presumably that fails, the mod is:

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8, 9); // RX, TX

Wow, stupid mistake on my part. Sorry about that!

I just received this on the serial monitor:
[1538] Failed to disable Echo
[10469] Ready (ping: 22ms).

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8, 9); // RX, TX

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{ 
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);
}


void loop() {
  
  Blynk.run();
  
}