ESP8266

Hello, I have recently started working with ESP8266 and I am able to send commands to it via the serial monitor but when I write a sketch to do the same thing nothing happens.
This is the sketch that I am using.

#include <SoftwareSerial.h>

const byte rxPin = 0; // Wire this to Tx Pin of ESP8266
const byte txPin = 1; // Wire this to Rx Pin of ESP8266

// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);

void setup() {
  Serial.begin(9600);
  ESP8266.begin(9600); // Change this to the baudrate used by ESP8266
  delay(1000); // Let the module self-initialize
  Serial.println("Sending an AT command...");
  ESP8266.write("AT");
  delay(300);
  
}

void loop() {
  
  while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  }  
}

so I don’t understand what else should I try kindly help thanks.

You've already got hardware serial running on pins 0 and 1. Use different pins if you're going to use software serial to communicate with the ESP8266.

DrAzzy:
You've already got hardware serial running on pins 0 and 1. Use different pins if you're going to use software serial to communicate with the ESP8266.

Just tried that as well but with no luck

aniskazi:

ESP8266.write("AT");

Serial.write is used for sending binary data, you want to send text, so use

ESP8266.print("AT\n");

You need to send a line ending as well (\n)
If that doesn't work, try adding a carriage return as well (\r).

ESP8266.print("AT\r\n");

If that doesn’t work, try adding a carriage return as well (\r).

Or, send them in the proper order using println():

ESP8266.println("AT"):

PieterP:
Serial.write is used for sending binary data, you want to send text, so use

ESP8266.print("AT\n");

You need to send a line ending as well (\n)
If that doesn’t work, try adding a carriage return as well (\r).

ESP8266.print("AT\r\n");

PaulS:
Or, send them in the proper order using println():

ESP8266.println("AT"):

I tried doing that all

#include <SoftwareSerial.h>

const byte rxPin = 0; // Wire this to Tx Pin of ESP8266
const byte txPin = 1; // Wire this to Rx Pin of ESP8266

// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);

void setup() {
  Serial.begin(9600);
  ESP8266.begin(9600); // Change this to the baudrate used by ESP8266
  delay(1000); // Let the module self-initialize
  Serial.println("Sending an AT command...");
 ESP8266.print("AT\n");
  delay(300);
  
}

void loop() {
  
  while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  }  
}

and the only thing that I got on the serial monitor was:
Sending the AT command…
and than nothing.

DrAzzy:
You've already got hardware serial running on pins 0 and 1. Use different pins if you're going to use software serial to communicate with the ESP8266.

Now, you're just sending 'AT\r\n' to the computer.
Connect your ESP to different pins.
Pins 0 and 1 are the serial connection to the computer, and they are already in use.

PieterP:
Now, you’re just sending ‘AT\r\n’ to the computer.
Connect your ESP to different pins.
Pins 0 and 1 are the serial connection to the computer, and they are already in use.

#include <SoftwareSerial.h>

const byte rxPin = 2; // Wire this to Tx Pin of ESP8266
const byte txPin = 3; // Wire this to Rx Pin of ESP8266

// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);

void setup() {
  Serial.begin(9600);
  ESP8266.begin(9600); // Change this to the baudrate used by ESP8266
  delay(1000); // Let the module self-initialize
  Serial.println("Sending an AT command...");
 ESP8266.print("AT\r\n");
  delay(300);
  
}

void loop() {
  
  while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  }  
}

still nothing :confused:

What Arduino are you using?
Have you tried to use the ESP8266 directly with the computer? Are you sure it expects a baud of 9600? And does it respond to your AT messages?
If you are using a 5V Arduino, are you using a decent 3.3V power supply, with decent decoupling capacitors? And you are using a 5V to 3.3V level shifter on the Arduino's TX line, right? If not, you've probably fried your ESP8266 ...

still nothing

Did you change the pins that the ESPxxxx is actually connected to?

 while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  }
  1. Why is this a while statement?
  2. Why are you pissing away resources using the String class? Until you KNOW that you are getting good data from the ESPxxxx, why is there ANY assumption about the data (like that it actually contains a carriage return) and why is it necessary to store the data?

Are you CERTAIN that the ESPxxxx is loafing along at 9600 baud?

PieterP:
What Arduino are you using?
Have you tried to use the ESP8266 directly with the computer? Are you sure it expects a baud of 9600? And does it respond to your AT messages?
If you are using a 5V Arduino, are you using a decent 3.3V power supply, with decent decoupling capacitors? And you are using a 5V to 3.3V level shifter on the Arduino's TX line, right? If not, you've probably fried your ESP8266 ...

I am using the arduino mega2560 and I haven't directly used it with the computer and yes I actually changed the baud rate to 9600 at+ciobaud by using this command and yes when I manually enter the AT commands in the serial monitor they respond OK and everything works well.
Well actually I am just using the USB cable that is connected to my arduino and I am using the 3.3V pin of the arduino to connect the esp. And the esp is still working fine.

I am using the arduino mega2560

Then WHY in heavens name are you using SoftwareSerial? You have 4 hardware serial ports. Connect the ESPxxxx to one of them.

You DID read the page about which pins you could do SoftwareSerial on on the Mega, didn't you? (I didn't think so.)

aniskazi:
I am using the arduino mega2560

Then why not use a hardware serial port?

aniskazi:
I am using the 3.3V pin of the arduino to connect the esp.

Don't do that, the 3.3V pin of the Arduino can only supply 50mA, the ESP can draw up to 5 times as much.
Also, you need a bypassing capacitor: when transmitting WiFi, it draws a lot of current in very small peaks, without bypass caps, your voltage will drop low enough to make it unstable.

aniskazi:
And the esp is still working fine.

Not for long if you are connecting its 3.3V input pins to 5V outputs of the Mega. You will destroy the ESP.
Use a voltage divider or a level shifter.

First make sure your hardware is correct, once that is done, we can start to look at what's wrong with your code.

I explained how to wire it in this post: http://forum.arduino.cc/index.php?topic=421253.msg2901649. (forget about the LEDs and the program button, you don't need them.)

PieterP:
Then why not use a hardware serial port?
Don't do that, the 3.3V pin of the Arduino can only supply 50mA, the ESP can draw up to 5 times as much.
Also, you need a bypassing capacitor: when transmitting WiFi, it draws a lot of current in very small peaks, without bypass caps, your voltage will drop low enough to make it unstable.
Not for long if you are connecting its 3.3V input pins to 5V outputs of the Mega. You will destroy the ESP.
Use a voltage divider or a level shifter.

First make sure your hardware is correct, once that is done, we can start to look at what's wrong with your code.

I explained how to wire it in this post: http://forum.arduino.cc/index.php?topic=421253.msg2901649. (forget about the LEDs and the program button, you don't need them.)

what if I just used an external supply and use the voltage regulator to provide esp with sufficient power? would that be okay or will still have to use those bypass capacitors?

Pretty much every digital chip requires bypass capacitors to keep the power lines (and thus itself) stable. And pretty much every voltage regulator requires them to produce a stable voltage.
Why would you want to leave them out?

Did you read Coding Badly's link, or watched the video I suggested?