Arduino模块esp8266返回错误请求

时间:2015-04-04 16:20:25

标签: http get arduino esp8266

我正在用我的arduino uno测试wifi模块esp8266。 我使用直接连接RX / TX和通过softwareserial工作。

这是我的代码:

#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);
}

这一直有效,直到我进行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

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

  1. 检查它是否完美,如果使用硬件串行Rx(引脚0),Tx(引脚1)和串行监视器,ESP8266会正确响应。
  2. 尝试降低波特率。大部分时间是SoftwareSerial和 Arduino Uno无法处理快速响应和松散的响应 信息。
  3. 如果您仍然收到垃圾回复,则很可能是您的模块已损坏。

答案 1 :(得分:1)

答案 2 :(得分:0)

你不应该在get命令中包含端口号。已在cipstart指定的端口。您的http请求文本中有错误。其他一切(发送请求和获得响应)都可以。 http://en.m.wikipedia.org/wiki/Hypertext_Transfer_Protocol

答案 3 :(得分:0)

检查电源。当我简单地将模块直接连接到Arduino电源引脚时,我偶尔会出现这些随机字符。这些似乎模块正在不断重置并吐出模块信息。

我的解决方案是将另一个3.3v稳压器与Arduino并联。将接地连接在一起,并从3.3v电源为ESP8266供电。问题解决了。