Http Arduino模块esp8266返回错误请求
我正在用arduino uno测试无线模块esp8266。 我通过直接连接RX/TX和软件串行使其工作 这是我的代码:Http Arduino模块esp8266返回错误请求,http,get,arduino,esp8266,Http,Get,Arduino,Esp8266,我正在用arduino uno测试无线模块esp8266。 我通过直接连接RX/TX和软件串行使其工作 这是我的代码: #include <SoftwareSerial.h> SoftwareSerial esp8266(3, 2); // RX | TX #define DEBUG true int ERROR_PIN = 7; int OK_PIN = 6; char serialbuffer[400];//serial buffer for request url con
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2); // RX | TX
#define DEBUG true
int ERROR_PIN = 7;
int OK_PIN = 6;
char serialbuffer[400];//serial buffer for request url
const String ssid = "XXX";
const String pw = "XXX";
int state = 0;
void setup()
{
delay(1000);
/**
// init leds
pinMode(ERROR_PIN, OUTPUT);
pinMode(OK_PIN, OUTPUT);
state = 0;
digitalWrite(ERROR_PIN, HIGH);
digitalWrite(OK_PIN, LOW);
/**/
// init ports
Serial.begin(19200);
Serial.println("initializing esp8266 port...");
esp8266.begin(19200);
delay(400);
// init WIFI
/**/
while(!esp8266.available())
{
Serial.print("...");
delay(300);
}
Serial.println();
Serial.println("FINISH esp8266 initializing!");
//
/**
digitalWrite(ERROR_PIN, LOW);
digitalWrite(OK_PIN, HIGH);
state = 1;
/**/
/**/
// Setup connection
sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE?\r\n",1000,DEBUG);
//sendData("AT+CWMODE=1\r\n",2000,DEBUG);
//sendData("AT+RST\r\n",3000,DEBUG);
//sendData("AT+CWLAP\r\n",6000,DEBUG);
sendData("AT+CWJAP=\"" + ssid + "\",\""+ pw +"\"\r\n",12000,DEBUG);
sendData("AT+CIFSR\r\n",8000,DEBUG);
sendData("AT+CIPMUX=1\r\n", 6000, DEBUG);
webRequest("");
/**/
/**/
}
void loop()
{
if (esp8266.available())
{
char c = esp8266.read() ;
Serial.print(c);
/**
if(state == 0)
{
state = 1;
digitalWrite(ERROR_PIN, LOW);
digitalWrite(OK_PIN, HIGH);
}
/**/
}
else
{
/**
if(state > 0)
{
state = 0;
digitalWrite(ERROR_PIN, HIGH);
digitalWrite(OK_PIN, LOW);
}
/**/
}
if (Serial.available())
{
char c = Serial.read();
esp8266.print(c);
}
}
//////////////////////////////////////////////////////////////////////////////
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
//////////////////////////////////////////////////////////////////////////////////
String webRequest(String url)
{
String response = "";
url = "www.google.es";
//String tmpCommand = "AT+CIPSTART=4," + "\"TCP\",\"" + url + "\",80";
String tmpSTARTCommmand = "AT+CIPSTART=0,\"TCP\",\"retro.hackaday.com\",80\r\n\r\n";
String tmpGETCommand = "GET / HTTP/1.1\r\nHost: ";
tmpGETCommand += "retro.hackaday.com";
tmpGETCommand += ":80\r\n\r\n";
String tmpSENDCommand = "AT+CIPSEND=0," + String(tmpGETCommand.length()) + "\r\n";
sendData(tmpSTARTCommmand, 8000, DEBUG);
sendData(tmpSENDCommand, 8000, DEBUG);
sendData(tmpGETCommand, 15000, DEBUG);
}
#包括
软件系列esp8266(3,2);//RX | TX
#定义调试为真
int ERROR_引脚=7;
int OK_引脚=6;
字符串行缓冲区[400]//请求url的串行缓冲区
常量字符串ssid=“XXX”;
常量字符串pw=“XXX”;
int state=0;
无效设置()
{
延迟(1000);
/**
//初始化发光二极管
引脚模式(错误引脚,输出);
引脚模式(OK_引脚,输出);
状态=0;
数字写入(错误_引脚,高);
数字写入(OK_引脚,低电平);
/**/
//初始化端口
连载《开始》(19200);
Serial.println(“初始化esp8266端口…”);
esp8266.贝京(19200年);
延迟(400);
//init WIFI
/**/
而(!esp8266.available())
{
连续打印(“…”);
延迟(300);
}
Serial.println();
Serial.println(“完成esp8266初始化!”);
//
/**
数字写入(错误_引脚,低);
数字写入(正常引脚,高电平);
状态=1;
/**/
/**/
//设置连接
sendData(“AT+RST\r\n”,2000年,调试);
sendData(“AT+CWMODE?\r\n”,1000,调试);
//sendData(“AT+CWMODE=1\r\n”,2000,调试);
//sendData(“AT+RST\r\n”,3000,调试);
//sendData(“AT+CWLAP\r\n”,6000,调试);
sendData(“AT+CWJAP=\”“+ssid+”\,\”“+pw+”\“\r\n”,12000,调试);
sendData(“AT+CIFSR\r\n”,8000,调试);
sendData(“AT+CIPMUX=1\r\n”,6000,调试);
webRequest(“”);
/**/
/**/
}
void循环()
{
如果(esp8266.available())
{
char c=esp8266.read();
连续打印(c);
/**
如果(状态==0)
{
状态=1;
数字写入(错误_引脚,低);
数字写入(正常引脚,高电平);
}
/**/
}
其他的
{
/**
如果(状态>0)
{
状态=0;
数字写入(错误_引脚,高);
数字写入(OK_引脚,低电平);
}
/**/
}
if(Serial.available())
{
char c=Serial.read();
esp8266.印刷品(c);
}
}
//////////////////////////////////////////////////////////////////////////////
字符串sendData(字符串命令、常量int超时、布尔调试)
{
字符串响应=”;
打印(命令);//将读取的字符发送到esp8266
长整数时间=毫秒();
而((时间+超时)>毫秒()
{
而(esp8266.available())
{
//esp有数据,因此将其输出显示到串行窗口
char c=esp8266.read();//读取下一个字符。
响应+=c;
}
}
如果(调试)
{
串行打印(响应);
}
返回响应;
}
//////////////////////////////////////////////////////////////////////////////////
字符串webRequest(字符串url)
{
字符串响应=”;
url=“www.google.es”;
//字符串tmpCommand=“AT+CIPSTART=4,”+“\”TCP\”,\“+url+”,80”;
字符串tmpstartcommand=“AT+CIPSTART=0,\“TCP\”,\“retro.hackday.com\”,80\r\n\r\n;
字符串tmpGETCommand=“GET/HTTP/1.1\r\nHost:”;
tmpGETCommand+=“retro.hackday.com”;
tmpGETCommand+=“:80\r\n\r\n”;
字符串tmpSENDCommand=“AT+CIPSEND=0,”+String(tmpGETCommand.length())+“\r\n”;
sendData(tmpstartcommand,8000,调试);
sendData(tmpSENDCommand,8000,调试);
sendData(tmpGETCommand,15000,调试);
}
在我执行webrequest之前,这一直有效。我收到一个错误的请求响应
initializing esp8266 port...
.........
FINISH esp8266 initializing!
BâÂúØÐPÊþ^X8Â�Ä^Âú[8ÐûÈâ·CâËØè[8Ð{Èâ·GâÃØRÈ蚉5˜‰0
bÕ
ready
AT+CWMODE?
+CWMODE:3
OK
AV®)AB•«Ë—mX·et","XXX"
OK
AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:9b:c3:83"
+CIFSR:STAIP,"192.168.1.89"
+CIFSR:STAMAC,"18:fe:34:9b:c3:83"
OK
AT+CIPMUX=1
OK
AV%AMEÕÕ*$‘²troÐ…�‘½µ‰,80
0,CONNECT
OK
AVCIPSEND=0,47
> GE@/!QQAŠrŠ%åõÑ: ÊÑɽB�……¹½µÂ‚%\n\r\nbusy s...
SEND OK
+IPD,0,323:HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Apr 2015 16:17:29 GMT
Content-Type: text/html
Content-Length: 172
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
OK
"qXÑzÂC!É1âø‚h[•�™cü ÐQ!}Ñfócú I]Ø÷ÃBj 1¤(ÑÃÖa”K!~CóbÕ
ready
正在初始化esp8266端口。。。
.........
完成esp8266初始化!
BúØÐPÊþþX8�5‰0
bÕ
准备好的
在+CW模式下?
+工作模式:3
好啊
AV®)AB•«Ë-mX·et,“XXX”
好啊
AT+CIFSR
+CIFSR:APIP,“192.168.4.1”
+CIFSR:APMAC,“1a:fe:34:9b:c3:83”
+CIFSR:STAIP,“192.168.1.89”
+CIFSR:STAMAC,“18:fe:34:9b:c3:83”
好啊
AT+CIPMUX=1
好啊
AV%AMEÕÕ*$troÐ…�‘½µ‰,80
0,连接
好啊
AVCIPSEND=0,47
>通用电气/!QQAŠrŠ%�……CharStyle½µCharStyle%\n\r\n使用s。。。
发送OK
+IPD,0323:HTTP/1.1400错误请求
服务器:nginx/1.6.2
日期:2015年4月4日星期六格林尼治标准时间16:17:29
内容类型:text/html
内容长度:172
连接:关闭
400错误请求
400错误请求
nginx/1.6.2
好啊
“qXñzÉ1øh[•”�™ÐQ!}fócúI]Ø÷Bj 1(Ða)K!~cóbÕ
准备好的
有什么想法吗?您不应该在get命令中包含端口号。端口已在cipstart中指定。http请求文本中出现错误。其他一切(发送请求和获取响应)都正常。
如果您仍然收到垃圾响应,则很可能是模块损坏。我使用以下代码退出: 但是我有mega2560。。 硬件连接:
检查您的电源。当我简单地将模块直接连接到Arduino电源引脚时,这些随机字符每隔一段时间就会出现一次。这些字符似乎是模块不断地自我重置,并吐出模块信息
我的解决方案是将另一个3.3v调节器与Arduino并联。将接地连接在一起,并从3.3v电源向ESP8266供电。问题已解决。我也尝试了没有端口号的情况,但我遇到了同样的情况,请求不正确。我也尝试了通过硬件串行,它的响应是相同的…忙s我正在等待一个不同的模块,我将尝试使用专用电源进行检查。我了解到这可能是与其相关的问题。新模块是否解决了您的问题?如果是…您是否看到框架版本之间有任何差异?尝试使用工作模块的版本烧录您的不工作模块?我有相同的问题,但与mega256有关你呢