How to print response to AT commands in Arduino serial port?

心已入冬 提交于 2019-12-11 12:38:29

问题


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() {}

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/36475331/how-to-print-response-to-at-commands-in-arduino-serial-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!