Arduino Uno - ESP 8266: sending AT commands via Code I bought a ESP 8266 &amp; I am able to send AT commands and receive response via serial monitor when I upload a empty sketch. I changed the baud rate from 115200 to 9600 by using AT+CIOBAUD=9600 and am able to run AT commands at 9600 baud rate.

Empty Sketch:

void setup() 
{

}
void loop() 
{

}


Here is how my circuit looks:


Problem

The problem is when I create a custom sketch to run AT commands it doesn't work. Here is how my sketch looks:

#include &lt;SoftwareSerial.h&gt;
SoftwareSerial esp8266(0,1); //RX,TX

void setup()
{

  Serial.begin(9600);
  esp8266.begin(9600); 

  Serial.println("Run AT Command");

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



}

void loop(){
   String response = "";
   while(esp8266.available())     {
        char c = esp8266.read(); 
        response+=c;
        Serial.print(c);
      }  

      if(response != ""){
        Serial.println(response);
      }

}


ESP8266 is receiving 3.3V but I have read TX and RX sends 5V, and they also need to be 3.3V. Don't know if thats what is causing it, but I am able to run AT commands on empty sketch, so this is a bit weird.


  UPDATE:


My sketch is:

#include &lt;SoftwareSerial.h&gt;
SoftwareSerial esp8266(2, 3); //RX,TX

void setup()
{

  Serial.begin(9600);
  esp8266.begin(9600);


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



}

void loop() {
  String response = "";
  while (esp8266.available())     {
    char c = esp8266.read();
    response += c;
  }

    if (response != "") {
      Serial.println("Response: " + response);
    }



}


Serial Monitor shows this:

Response: A
Response: T


OK
Response: 


OUTPUT when I run AT+CIFSR command:

Response: A
Response: T+CIFSR


Response: 
+CIFSR:APIP
Response: ,"192.168.4.1
Response: "
+CIFSR:APMA
Response: C,"5e:cf:7f:80
Response: :f7:74"

OK


Question now is: How do I store the string being returned from the esp8266. I thought my response string was suppose to store the "OK" but that did not happen.
Of course you cannot use the same pins for both hardware and software serial, but that part seems to have been solved in comments.


  Question now is: How do I store the string being returned from the esp8266. I thought my response string was suppose to store the "OK" but that did not happen.


 while (esp8266.available())     {
    char c = esp8266.read();
    response += c;
 }


This will only read as long as characters are available, but the ATmega is able to run this loop much more quickly than characters are received, so unless the entire message was previously transferred it will likely only get a character or two before the receive buffer is found (temporarily) empty and it terminates.

You should instead read until a terminating condition is found, such as a newline character.  For safety you might also put in a "give up and try again" timeout, but make it long.

Some would try to solve this problem by inserting short delays before trying to read, but that is unwise - depending on delays creates unreliability as the necessary delay can easily change due to conditions that may on the surface seem to have little relevance, for example the esp866 might respond more slowly if it is seeing a lot of broadcast traffic on a network, or if the firmware version changes.