如何在Arduino串口中打印对AT命令的响应? [英] How to print response to AT commands in Arduino serial port?

查看:40
本文介绍了如何在Arduino串口中打印对AT命令的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印对 AT 命令的响应.我正在发送 AT 命令,但在 Arduino 串口中没有收到任何响应.它给出 -1 而不是 OK.

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

推荐答案

正如您被告知的,用回车符 \r 终止您的 AT 命令.此外,您当前的代码将仅读取响应的一个字节,如果响应甚至已经到达,因为您根本没有包含任何延迟.要与串行监视器交互地与 ESP 通信,我建议使用:

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

这基本上使您的 Arduino 成为 PC 和 ESP 之间通信的管道.您可以使用串行监视器向 ESP 发送命令并立即获得结果.它非常适合测试命令.请记住将串行监视器设置为 BOTH NL &CR;这将很好地为您提供命令以及您发送的任何 HTTP 请求,因为它将 \r\n 附加到您发送的所有内容.

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.

如果您确实想编写一个与 ESP 对话的草图,则必须在发送命令后提供一些延迟,以等待模块处理命令并做出响应.延迟取决于命令,至少 500ms.通常的程序是为每个命令定义一个超时期限,这取决于它预计需要多长时间,之后如果没有响应,你就放弃".GitHub 上有很多库涉及使用 AT 命令与某些设备通信;研究他们以学习他们的技术.

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.

这篇关于如何在Arduino串口中打印对AT命令的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆