首页 文章

ESP8266和Arduino接口

提问于
浏览
1

我已将Arduino与ESP8266连接起来

Arduino引脚2连接到ESP的Tx Arduino引脚3连接到ESP的Rx通过分压器Arduino GND连接到ESP的GND Arduino 3v3连接到ESP的CH_PD

我使用1117电压调节器为ESP8266供电

当我最初购买ESp8266它工作但现在它显示了无穷无尽的垃圾值...

使用以下代码对arduino进行编程

#include <SoftwareSerial.h>

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
}

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



  if(Serial.available())
  {
    // the following delay is required because otherwise the arduino will read the first letter of the command but not the rest
    // In other words without the delay if you use AT+RST, for example, the Arduino will read the letter A send it, then read the rest and send it
    // but we want to send everything at the same time.
    delay(1000); 

    String command="";

    while(Serial.available()) // read the command character by character
    {
        // read one character
      command+=(char)Serial.read();
    }
    esp8266.println(command); // send the read character to the esp8266
  }
}

5 回答

  • 1

    您的esp8266可能工作在56000或115200波特率而不是9600.这将导致垃圾被读取 .

    如果是115200,它将无法在带有softwareSerial的普通数字引脚上工作 .

    如果是较旧的主板,则可以尝试更改为56000: -

    esp8266.begin(56000); // your esp's baud rate might be different
    

    否则,您需要将esp8266挂钩到HardwareSerial端口

    Serial.begin(115200);
    
  • 0

    代码似乎没问题,但你应该检查你的ESP8266波特率可能不同 . 查看以下内容:

    • 单独检查ESP8266波特率,一旦有了,就向Arduino声明相同的波特率 .

    • 检查你的Arduino模型,一些像nano一样的克隆驱动不同于原来的电压,

  • 1

    上传代码并检查串行监视器是否有特定波特率的响应,如果没有对特定波特率做出任何响应,则更改波特率直到得到响应 . 对于少数模块,默认波特率为57600.So根据它进行检查 .

    您可以使用上面给出的代码并更改_2911059的波特率_更改波特率,如9600,56000,112500等,并以9600波特率检查串行监视器 .

    Serial.begin(9600);
    

    您将在显示器上获得响应,并尝试通过将3.3v连接到RST引脚1-2秒来重置wifi模块 . 希望能帮助到你 .

  • 0

    作为其他答案的补充,尝试用逻辑电平转换器替换分压器,因为esp具有3.3v逻辑和arduino 5v逻辑 .

  • 0

    检查逻辑值,因为esp8266适用于3.3v和串行端口波特率 . 在少数情况下,ESP8266可能存在内部故障并产生垃圾值 . 就ESP8266而言,结帐here这对我帮助很大

相关问题