1

I want to print the response to AT command. I'm sending AT command but I'm not getting any response in the Arduino serial port. It's giving -1 instead of OK.

#include "SoftwareSerial.h"
String ssid = "connectify-krish";
String password = "12345678";

String myword= "";
SoftwareSerial esp(10, 11);
void setup() {
  Serial.begin(9600);
  esp.begin(9600);
  esp.write("AT");
  Serial.println(esp.read());
}

void loop() {}
dda
  • 6,030
  • 2
  • 25
  • 34
STACK2
  • 165
  • 1
  • 5
  • 16
  • 2
    Shouldn't you send a newline ("AT\n") in order to get a response? – Ctx Apr 07 '16 at 11:52
  • i used esp.write("AT\n");, but got same result "-1" – STACK2 Apr 07 '16 at 12:04
  • 1
    Depending on your device, maybe "AT\r\n" is needed? – Ctx Apr 07 '16 at 12:08
  • yes, it seems to be AT\r\n. See https://cdn.sparkfun.com/assets/learn_tutorials/4/0/3/4A-ESP8266__AT_Instruction_Set__EN_v0.30.pdf , page 7, note #5. – Alphonsos_Pangas Apr 07 '16 at 14:48
  • 1
    No, no, no! The **ONLY** correct way to terminate an AT command line is with `\r` only (unless you have messed with the S3 register, which you should not do). The [V.250 specification](http://www.itu.int/rec/T-REC-V.250-200307-I/en) is crystal clear on this, see chapter "5.2 DTE commands lines" – hlovdal Apr 07 '16 at 20:56
  • Instead of esp.write("AT") use esp.println("AT"). This will add \r\n at the end of the string. After sending the AT, write a while loop using millis() that waits for response from esp for a second. I use it with my esp its works pretty well. Also check if the module is working when directly connect to PC. Confim the baudrate is 9600, most esp modules have baudrate of 115200 – Hemal Chevli Apr 08 '16 at 04:25

2 Answers2

1

As already pointed out in the comments you are not terminating the AT command line, so the modem will never respond to this.

Make sure you read V.250 and learn the difference between an AT command and an AT command line. ATI is an AT command, and "ATI I I\r" is a command line invoking this command three times in a row. Notice by the way in this example that you will get just one single Final result code for all three of them, i.e. the Final result code is a response to a complete command line and not to individual command invocations.

Then after fixing the sending of the command you must implement proper handling of the response. This includes reading and parsing what the modem sends back as a response to the sent command line. See this answer for the code structure and implementation hints.

Community
  • 1
  • 1
hlovdal
  • 26,565
  • 10
  • 94
  • 165
1

As you've been told, terminate your AT commands with a carriage return character \r. Also you current code will read only a byte of the response, and thats if the response has even arrived since you included no delay at all. To communicate with the ESP interactively with the Serial monitor, I'd recommend using this:

#include <SoftwareSerial.h>

SoftwareSerial esp(10, 11);
void setup(){
  Serial.begin(9600);
  esp.begin(9600);
}

void loop()
{
  while (Serial.available())  // forward data from monitor to esp
    esp.write(Serial.read());
  while (esp.available())  // forward data from esp to monitor
    Serial.write(esp.read());
}

This basically makes your Arduino a conduit for communication between your PC and the ESP. You can send commands to the ESP with the Serial monitor and get their results immediately. Its great for testing commands. Just remember to set the serial monitor to BOTH NL & CR; this will serve you well for commands as well as any HTTP requests you send, as it appends \r\n to everything you send.

If you do want to write a sketch to talk to the ESP, you must provide some delay after sending a command to wait for the module to process the command and respond. The delay varies depending on the command, at least 500ms. The usual procedure is to define a timeout period for each command, depending on how long its expected to take, after which you 'give up' if there's no response yet. There are lots of libraries on GitHub that involve talking to some device using AT commands; study them to learn their techniques.

SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16