tcp - 如何使 AT 命令以编程方式在 arduino 中为 ESP8266 wifi 模块工作

标签 tcp tcpclient arduino-uno esp8266 arduino-ide

我正在使用 arduino uno 上的 ESP8266 wifi 模块从 arduino 到 raspberry-pi 进行简单的 tcp 无线通信。tcp 服务器正在 raspberry-pi 上运行。我能够使用以下 AT 命令进行 TCP 通信在 arduino 串行监视器中,波特率为 9600。

AT+CIPMUX=1
AT+CIPSTART=4,"TCP","192.168.43.150",7777
AT+CIPSEND=4,5
>hai

如何在 arduino sketch 中以编程方式执行此操作。我在我的 arduino uno 上使用了以下代码,但仍然没有成功。波特率仅为 9600,因为它直接在串行监视器中工作。

#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3);

void setup()
{
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
}

void loop()
{
 esp8266.println("AT");
 if(esp8266.available()) // check if the esp is sending a message 
 {
 while(esp8266.available())
  {
    // The esp has data so display its output to the serial window 
    char c = esp8266.read(); // read the next character.
    Serial.write(c);
  }  
 }
}

连接如下

  ESP8266  Arduino Uno

  Vcc       3.3V
  CH_PD     3.3V
  RX        RX(PIN 2) 
  TX        TX(PIN 3)
  GND       GND 

最佳答案

这可能有点晚了,但我最近遇到了类似的问题。如果已排序,请随意忽略它。

根据您的 ESP8266 模块的固件版本,9600 的波特率可能不起作用,试试 115200 - 它可能被证明更可靠?

我认为上面的代码不起作用的主要原因是 ESP 在 AT 命令末尾需要换行符和回车符。串行监视器为您添加这些。不要发送 AT,而是尝试发送 AT\r\n。这应该鼓励 ESP 回复 OK,或者如果回显打开 AT\r\nOK

Serial.available() 还检查​​接收缓冲区中是否有内容 - 不幸的是这需要时间,所以我不得不在其中放置一个 delay(10)让它在缓冲区中注册一个字符。

#include <SoftwareSerial.h>

//i find that putting them here makes it easier to 
//edit it when trying out new things

#define RX_PIN 2
#define TX_PIN 3
#define ESP_BRATE 115200

SoftwareSerial esp8266(RX_PIN, TX_PIN);

void setup()
{
  Serial.begin(9600);
  esp8266.begin(ESP_BRATE); // I changed this
}

void loop()
{
 esp8266.println("AT\r\n"); //the newline and CR added
 delay(10);  //arbitrary value

 if(esp8266.available()) // check if the esp is sending a message 
 {
 while(esp8266.available())
  {
    // The esp has data so display its output to the serial window 
    char c = esp8266.read(); // read the next character.
    Serial.write(c);
  }  
 }
}

我的下一个问题是我的 ESP 的 0 个回复是不可靠的 - 有时它们被读取为正常但有时它们是垃圾值。我怀疑这是模块功率不足的问题。

关于tcp - 如何使 AT 命令以编程方式在 arduino 中为 ESP8266 wifi 模块工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28691764/

相关文章:

networking - 数据链路层和传输层

html - 如何用C语言控制Web服务器和客户端之间的框架交换?

c# - C#同步TCP客户端套接字无法解释的丢失数据

c# - 同步多套接字客户端说明

c - Arduino闹钟没有rtc项目错误

c++ - TCP 服务器/客户端 : client recv() returns blank buffer

javascript - client.on 没有在单次连接上获取数据

c++ - cpp中的UDP套接字永远循环

c++ - 在 Arduino UNO 编程中,当您希望同时执行不同的功能时,使用什么代码/语句?

c - Arduino 问题 :"' i' 未在此范围内声明”