2

I bought a ESP 8266 & 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: Diagram

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 <SoftwareSerial.h>
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 <SoftwareSerial.h>
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.

  • Why are you using software serial? – PhillyNJ Jun 10 '16 at 19:16
  • Because most of the examples I saw used them – Java Gamer Jun 10 '16 at 20:46
  • Ok, makes sense. I usually use a Mega with Serial and Serial 2. According to your frizting diagram, it looks like you have RX and TX reversed. – PhillyNJ Jun 10 '16 at 20:53
  • I did try to change TX to RX and RX to TX, in arduino but when I do that, I dont get response from AT commands, on the serial monitor – Java Gamer Jun 10 '16 at 21:34
  • 1
    If you are using an Uno like in the image, use different pins for Software Serial like 2,3. Connect pin 2 to the ESP TX and pin 3 to the ESP RX. In your code, try SoftwareSerial(2,3); – TisteAndii Jun 12 '16 at 1:28
2

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.

2

There is another technique that you can use, instead of hanging around waiting for more characters from the ESP8266. Your code starts:

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

This has the effect of creating a new String every time loop() is entered, and you accumulate the data from the ESP8266 in that - but it's lost if you leave loop(). So it may be better to do it differently:

const char EOL = '\n'; // End of line
String response = ""; // Create String once, outside loop()

void loop() {
    char c = '\0'; // Initialise to NUL
    while (esp8266.available() && c!=EOL) { // Wait for End of Line
        c = esp8266.read();
        response += c;
    } // while
    if (c==EOL) {
        Serial.print("Response: "); // Don't use '+' if you can help it!!!
        Serial.println(response);
        // Do other processing
        response = ""; // Clear response for next string
    } // if

This has three effects:

  1. It keeps an incomplete response between calls to loop().
  2. It only processes response once the end-of-line is read.
  3. It keeps loop() running nicely (important on other Arduino), rather than hanging around
0

Just check if this answer helps. Here is the thing, For AT commands from serial monitor, RX or arduino should be connected to RX of ESP and TX of arduino to TX of ESP. That is the reason you are able to enter AT commands with your circuit when empty program is uploaded.

Now for arduino to communicate with ESP using program, you have to connect RX of arduino to TX of ESP and TX of Arduino to RX of ESP. In your case if using software serial 2 & 3, then pin2 to TX of Esp and pin3 to RX of ESP.

To read "OK" from ESP, try below code:

if(esp8266.find("OK"))

Also you may try this code to read:

esp8266.find("pin="); //this acts like a pointer. 
int value = esp8266.read(); //returns ASCII Code. this will read value which comes after pin=

Refer : https://www.arduino.cc/en/Serial/read

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.