2015-04-04 111 views
3

我正在用我的arduino uno測試wifi模塊esp8266。 我通過直接連接RX/TX和軟件串行工作。Arduino模塊esp8266返回錯誤的請求

這是我的代碼:

#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 
> [email protected]/!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 

任何想法?

+0

我有同樣的問題,但與mega2560。你有解決方案嗎? – Atheel 2015-08-23 08:46:23

+0

@Atheel還沒有。我只是覺得這是這個模塊系列的問題。我發現了另一個:https://www.olimex.com/Products/IoT/MOD-WIFI-ESP8266-DEV/open-source-hardware,它沒有問題。 – blfuentes 2015-08-23 09:22:49

+1

謝謝。我會訂購這一個:( – Atheel 2015-08-23 09:27:42

回答

0

您不應該在get命令中包含端口號。端口已在cipstart中指定。您的http請求文本中有任何錯誤。其他一切(發送請求並獲得響應)都可以。 http://en.m.wikipedia.org/wiki/Hypertext_Transfer_Protocol

+0

我也嘗試過沒有端口號,但我面臨着一個不好的請求相同的場景 – blfuentes 2015-04-05 13:15:08

1
  1. 檢查,如果它完美的作品,如果你使用的硬件串列Rx(pin0),TX(PIN1)和串行顯示器,ESP8266正確響應。
  2. 嘗試降低波特率。大多數時候SoftwareSerial和Arduino Uno都無法處理快速響應和鬆散的信息位。

如果您仍然收到垃圾回覆最有可能您的模塊已損壞。

+0

我也試過硬件串行和它響應相同...「忙碌...」我正在等待一個不同的模塊,我會嘗試用專用電源來檢查,我讀它可能是與它有關的問題 – blfuentes 2015-04-22 06:31:18

+0

是否新模塊解決了你的問題?如果是的話......你看到框架版本有什麼不同嗎?試過你的工作模塊不能用來自工作模塊的版本進行刻錄? – 2016-01-03 15:38:35

1
+0

你可以發佈架構和你正在使用的代碼嗎? – blfuentes 2015-08-23 12:53:46

+0

它的與他的例子相同的代碼: https://github.com/itead/ITEADLIB_Arduino_WeeESP8266/blob/master/examples/HTTPGET/HTTPGET.ino 我的概率是與他的網址...它似乎我必須tipe http:// befor他的網址。 – Atheel 2015-08-24 06:01:17

0

檢查電源。當我直接將模塊直接連接到Arduino電源引腳時,偶爾會出現這些隨機字符。這些似乎是模塊不斷重置自己並吐出模塊信息。

我的解決方案是與Arduino並聯另一個3.3V穩壓器。將地線連接在一起,並從3.3v電源提供ESP8266。問題解決了。