ESP8266和Arduino接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP8266和Arduino接口相关的知识,希望对你有一定的参考价值。
我已将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
}
}
您的esp8266可能工作在56000或115200波特率而不是9600.这将导致垃圾被读取。
如果是115200,它将无法在带有softwareSerial的普通数字引脚上工作。
如果是较旧的主板,则可以尝试更改为56000: -
esp8266.begin(56000); // your esp's baud rate might be different
否则,您需要将esp8266挂钩到HardwareSerial端口
Serial.begin(115200);
代码似乎没问题,但你应该看看你的ESP8266波特率可能会有所不同。查看以下内容:
- 单独检查ESP8266波特率,一旦有了它,就可以在Arduino中声明相同的波特率。
- 检查你的Arduino模型,一些像nano一样的克隆驱动不同于原来的电压,
上传代码并检查串行监视器是否有特定波特率的响应,如果没有对特定波特率做出任何响应,则更改波特率直到得到响应。对于少数模块,默认波特率为57600.So根据它进行检查。
您可以使用上面给出的代码并更改esp8266.begin(56000);
的波特率,更改波特率,如9600,56000,112500等,并以9600波特率检查串行监视器。
Serial.begin(9600);
您将在显示器上获得响应,并尝试通过将3.3v连接到RST引脚1-2秒来重置wifi模块。希望能帮助到你。
作为其他答案的补充,尝试用逻辑电平转换器替换分压器,因为esp具有3.3v逻辑和arduino 5v逻辑。
检查逻辑值,因为esp8266适用于3.3v和串行端口波特率。在少数情况下,ESP8266可能存在内部故障并产生垃圾值。就ESP8266而言,结帐here它给了我很多帮助
以上是关于ESP8266和Arduino接口的主要内容,如果未能解决你的问题,请参考以下文章
Arduino ESP8266利用HTTPClient库 获取心知天气请求实例
基于Arduino和ESP8266的JSON数据获取与解压之和风天气