全部为AT + RST
首先返回ok
您的硬件连接是perfect.But这可能是由于一些常见的bugs.Have在许多前来为我工作其次,是否有必须重置esp8266的原因?
代码:
#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
}
}
这段代码的目标很简单:以接收来自Arduino的串行窗口命令,将它们发送到ESP8266,并打印ESP8266的响应命令或其他行动(如接收HTTP请求)。
如果上面的代码段失败(发生在一些情况下),而(serial.available())码块使用以下段替换:
while(Serial.available()) // read the command character by character
{
// read one character from serial terminal
char ch = Serial.read();
// send that one character to the esp8266
esp8266.print(ch); // send the read character to the esp8266
}
注我下降字符串数据类型。
如果您想直接从阿尔杜伊诺到ESP8266发送命令,那么你应该使用下面的代码段:
使用此代码连接到ESP8266:
#include <SoftwareSerial.h>
const byte rxPin = 2; // Wire this to Tx Pin of ESP8266
const byte txPin = 3; // Wire this to Rx Pin of ESP8266
// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);
void setup() {
Serial.begin(115200);
ESP8266.begin(115200); // Change this to the baudrate used by ESP8266
delay(1000); // Let the module self-initialize
}
void loop() {
Serial.println("Sending an AT command...");
ESP8266.println("AT");
delay(30);
while (ESP8266.available()){
String inData = ESP8266.readStringUntil('\n');
Serial.println("Got reponse from ESP8266: " + inData);
}
}
PS:不要忘了将串行结束的行设置为换行或回车。
在上面的代码段Iam只发送一个AT
命令(ESP8266.println("AT");
)进行演示。您可以用同样的方式发送任何命令。
你是什么意思不工作?它显然重置自己。 –
yes.gre_gor是对的。我认为Ans是因为他在最后得到'无效'而感到困惑 – Billa
是的我最后在无效时感到困惑。但现在其工作 –