Esp-01 setup error while compilation

Hello I’m using Arduino uno to test wifi with Blynk… All AT commands are working but when I try to upload the code. It’s throwing compilation error.
Below is the code

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "yourAUTH";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "yourSSID";
char pass[] = "yourpassword";
// 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 115200
ESP8266 wifi(&Serial);
void setup()
{
 // Debug console
 Serial.begin(9600);
 delay(10);
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
 Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
 Blynk.run();
} 

Please help me connect to Wifi. Thanks in advance.

Well, the code compiles fine for me, under the Atdiono IDE 1.8.3 for board type “Arduino Mega or Mega 2560”.
It seems you are trying to compile this code for an Uno…

image

However, you didn’t heed my previous advice (no surprise there then!)…

Once you un-comment the first of these lines then you’ll have an object called EspSerial which you need to use in place of Serial in the other two lines of code.

Pete.

Yes Pete, I have connected to 2 and 3 in uno just for testing purposes… I’ll do the rest and will let u know… Thank you.

Well, that won’t work unless you use the SoftwareSerial library, and if 115200 is the actual baud rate that your ESP is using then SoftwareSerial won’t work at that speed - so you’ll need to re-programme your ESP-01 via AT commands.

Pete.

I have manually changed the baud rate using AT commands… But the problem is the main code is getting a compilation error.

.

The code looks like this now.

#include <Arduino.h>
#include <BlynkSimpleStream.h>
#include <ESP8266_Lib.h>



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


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

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

// 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

int G=8;
int buttonstate1=0;

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  pinMode(G, OUTPUT);

  Serial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}

BLYNK_WRITE(V1) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {
  digitalWrite(G, HIGH);
 }
 else
 {
  digitalWrite(G, LOW);
 }



void loop()

{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

You’re using the wrong library.

You also have a missing closing curly bracket in your BLYNK_WRITE(V1)

You aren’t initialising your EspSerial port, but you’re initialising Serial twice.

Pete.

This still got some problems. :frowning:

#include <Arduino.h>

#include <BlynkSimpleStream.h>

#include <ESP8266_Lib.h>

#include <BlynkSimpleShieldEsp8266.h>

/* Comment this out to disable prints and save space */

#define BLYNK_PRINT Serial

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "bMLxyWgMZ9YS";

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "No Network";

char pass[] = "Nopassword";

// 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

int G=8;

int buttonstate1=0;

ESP8266 wifi(&EspSerial);

void setup()

{

  // Debug console

  Serial.begin(9600);

  // Set ESP8266 baud rate

  pinMode(G, OUTPUT);

  Blynk.begin(auth, wifi, ssid, pass);

  // You can also specify server:

  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);

  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

}

BLYNK_WRITE(V1) 

{

  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)

 {

  digitalWrite(G, HIGH);

 }

 else

 {

  digitalWrite(G, LOW);

 }

}

void loop()

{

  Blynk.run();

  // You can inject your own code or combine it with other sketches.

  // Check other examples on how to communicate with Blynk. Remember

  // to avoid delay() function!

}

Of course. Try re-reading what I wrote!

Pete.

I have changed it… But now I want you to see… It’s regarding Blynk libraries…

#include <Arduino.h>
#include <BlynkSimpleStream.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

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

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

// 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

int G=8;
int buttonstate1=0;

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(9600);
  EspSerial.begin(9600);
  // Set ESP8266 baud rate
  pinMode(G, OUTPUT);



  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}

BLYNK_WRITE(V1) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {
  digitalWrite(G, HIGH);
 }
 else
 {
  digitalWrite(G, LOW);
 }
}


void loop()

{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Pete.

What library should I use then?

BlynkSimpleStream is used for connecting via the USB script.

BlynkSimpleShieldEsp8266 is used when you use an ESP-01 as a WiFi shield.

Ask yourself which connection type you are currently using.

Then re-read what I wrote - the answer was there all along, if you bothered to look.

Pete.

Mark this thread as solved @PeteKnight :slight_smile:

Success sir This lil bit of explanation worked :slight_smile:

And was required because you don’t take heed of the advice and information you have already been given!

Pete.

Looks like it’s not finished yet… :frowning:
I’m running into trouble in mega. I have connected Rx and Tx to 18 & 19 to mega respectively. The code is getting compilation error it says Serial1 is not declared in the scope. Please help.

#include <Arduino.h>
// #include <BlynkSimpleStream.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

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

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

//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

int G=8;
int buttonstate1=0;

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  // Serial.begin(9600);
  EspSerial.begin(9600);
  Serial.begin(9600);
  // Set ESP8266 baud rate
  pinMode(G, OUTPUT);
  pinMode(13, OUTPUT);

  Serial.Println("This is working");  

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}

BLYNK_WRITE(V1) 
{
  buttonstate1 = param.asInt();

  if(buttonstate1 == 1)
 {
  digitalWrite(G, HIGH);

 }
 else
 {
  digitalWrite(G, LOW);
 }
}


void loop()

{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Uno doesn’t have a Serial1 port.

Pete.

This is not uno. This is now connected to mega as I mentioned above… It was working fine in uno using software serial.

I repeat…

Pete.

Sorry for that. I actually forgot to change the board. It’s working now but I’m unable to use Serial.println.

Have you ever heard the phrase “There are none so blind as those who will not see”?

Pete.