1

I am communicating with ESP8266 using SoftwareSerial of Arduino UNO. I can successfully send AT commands and get response when communicating through the terminal. But if i programmatically send particular AT commands there is no response. For eg: I type AT in serial monitor I get OK But if i send the following through SoftwareSerial pins

mySerial.println("AT");

there is no response.

Can anyone tell what will be the solution as to how to send AT commands explicitly.

The sketch is

#include<SoftWareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial.println("AT");
  delay(100);
  while (mySerial.available()>0 ) {
    char ch=mySerial.read();
    data.concat(ch);
    if(ch=='\n')
      Serial.println(data);
    data="";
  }
}//setup

void loop()  {
  while (mySerial.available()>0 ) {
    char ch=mySerial.read();
    data.concat(ch);
    if(ch=='\n')
      Serial.println(data);
    data="";
  }
  while (Serial.available()>0 ) {
    mySerial.write(Serial.read());
    delay(100);
  }
}//loop

I am using Sparkfun Level Shifter to get 3.3V from 5V. The wiring is straightforward. ESP8266 Vcc and CH_PD to 3.3V; GND to GND; Tx to Arduino 2(Rx) directly; Rx to Sparkfun 3.3V RXO LV side and Arduino 3(Tx) to RXI HV side) so that when Arduino transmits it gets stepped down to 3.3V and ESP8266 receives it. The wiring should be correct as it is working perfectly when AT commands are sent manually.

I tried everything of sending mySerial.write("AT/r/n"), mySerial.print("AT") mySerial.print("\r\n"), etc but nothing seems to work.

Is it that there is some problem with SoftwareSerial. But then how is it working if i send AT commands manually through terminal ?

  • How is it wired? What pins are you using? Show your sketch. – Majenko Oct 24 '15 at 9:50
  • Can you confirm that you have it wired with the transmit pin on the Arduino going to the receive pin on the ESP8266 and vice versa? – dlu Dec 16 '15 at 3:06
3

I have been playing with an ESP8266 over the last couple of days with similar results. My conclusion was that it is very timing dependent.

If you build in (fairly large) delays into your code then you will find it works better. The delays are effectively what you are doing as you type in each new command.

I found, for example, that the AT+CWJAP (join access point) command takes quite a while, like 10 seconds or so.

Preferably don't use "actual" delays because they are blocking (unless you don't care about that). You could have a list of commands you want to send, and a system that pulls the next one out of the list when a certain time has elapsed.

If you use something like 5 to 10 second delays between commands you should get better results. At the very least, after sending one command, look for a confirmation string from the device (eg. OK, ERROR etc.).

  • Thank you for your insight. I have done that too. I have added a specific timeout for each command and checked if the software serial has any response after the timeout, but again there is nothing. Regarding checking for the response string I did that. For "AT" after a delay of 1sec, I checked if there was any "OK" but again the efforts were in vain. I tried changing the delay. Nothing happened. – Dave127 Oct 30 '15 at 15:03
  • 2
    Are you sure it is running at 9600? I found mine was configured to run at 115200 baud. I tried changing the delay. Nothing happened. - please amend your question to show the code which incorporates my suggestion. – Nick Gammon Oct 30 '15 at 19:37
2

Same problem here with this code.

Three step for me to make it work :

  • Use method write() and not println()
  • Send newline \n when talking to esp8266
  • don't wait newline to print the response

    esp8266Serial.write("AT\n");
    delay(100);
    while (esp8266Serial.available() > 0 ) {
      char ch = esp8266Serial.read();
      Serial.println(ch);
    }
    

I hope it helps

1

It appears to me that when the ESP8266 sends a few characters to the Arduino, the initial version of the code always erases those characters (data="";) without ever printing anything to your serial monitor. So there is no way to tell whether the ESP8266 is sending anything or not.

To make it easier to debug, try printing every character from the ESP8266 to the serial monitor, whether or not it makes a complete message.

What do you see on the serial monitor with the following code?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

String data = "";

void setup(){
// Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial.println("AT");
}//setup

void loop(){
  if( mySerial.available()>0 ){
    char ch=mySerial.read();
    data.concat(ch);
    Serial.print((int)ch, HEX); // debug: print *every* character, in hex.
    if( ch=='\n' ){
      Serial.println();
      // Use "F()" macro as recommended by
      // https://learn.adafruit.com/memories-of-an-arduino/optimizing-sram .
      Serial.print(F("complete message:"));
      Serial.println(data);
      data="";
      Serial.print(F("waiting for next message ..."));
      }
  }
  if( Serial.available()>0 ){
    mySerial.write(Serial.read());
  }
}//loop
0

Did you make sure that your lines end with \n\r?

Remember you need both newline characters to send over the line from the terminal.

  • Did you mean to type "\n\r?" Off hand I can't think of what '\l' would be, but it is late… That said, I would expect the newline ('\n') to be sufficient. – dlu Dec 16 '15 at 2:59
-1

The ESP8266 needs 115200 baud.

The example didn't run clean in my case until I increased the baud rate for the Arduino<->Computer (USB) communication to 115200 baud as well. With 115200 on mySerial and 9600 on USB, an AT request of mine did not return an OK in most cases.

  • in AT firmware you can set the baud rate. SoftwareSerial can't run at 115200 so must lower baud rate be set for AT firmware – Juraj Sep 30 '18 at 13:50

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.