【问题标题】:How to make AT commands work programatically in arduino for ESP8266 wifi module如何使 AT 命令在 arduino 中为 ESP8266 wifi 模块以编程方式工作
【发布时间】:2015-02-24 09:08:26
【问题描述】:

我正在使用 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 草图中以编程方式执行此操作。我在我的 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 

【问题讨论】:

    标签: tcp tcpclient arduino-uno esp8266 arduino-ide


    【解决方案1】:

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

    根据您的 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 回复不可靠 - 有时它们被读取为 OK,但有时它们是垃圾值。我怀疑是模块功率不足的问题。

    【讨论】:

    • 我遇到了同样的问题,有时它会返回垃圾值。我解决了它,增加了延迟句子的时间。对我来说,100 就可以了。所以,不要使用 'delay(10);'我写了 'delay(100)' 让它适用于所有没有垃圾的命令。
    【解决方案2】:

    我遇到了同样的问题,但还没有找到解决方案。 但是您的连接有点麻烦,您必须将 ESP8266 模块的 TX 引脚连接到 arduino 的 RX 引脚,将 E​​SP8266 模块的 RX 引脚连接到 TX 引脚。 希望这对您有所帮助

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多