2013-01-05 65 views
0

我正在嘗試從arduino wifi盾做一個帖子到我的java servlet。 servlet的功能是url get和jquery post,但是我不能在我的arduino代碼中排列標題。任何幫助將不勝感激!Arduino的WiFi盾頭帖頭問題

服務器返回200,但我沒有得到有效載荷「內容」的價值。我不完全確定我在做什麼錯,但我很確定這是在我的標題如何設置。我花了兩天的時間試圖獲得它。

#include <SPI.h> 
#include <WiFi.h> 

char ssid[] = "jesussavesforjust19.95"; // your network SSID (name) 
char pass[] = "********"; // your network password (use for WPA, or use as key for WEP) 
int keyIndex = 0;   // your network key Index number (needed only for WEP) 

int status = WL_IDLE_STATUS; 

IPAddress server(192,168,10,149); // numeric IP for Google (no DNS) 
WiFiClient client; 

void setup() { 
    Serial.begin(9600); 

    // attempt to connect to Wifi network: 
    while (status != WL_CONNECTED) { 
    Serial.println("Attempting to connect to SSID: "); 
    Serial.println(ssid); 
    status = WiFi.begin(ssid, pass); 
    // wait 10 seconds for connection: 
    delay(10000); 
    } 
    Serial.println("Connected to wifi"); 
    printWifiStatus(); 
    sendData("0600890876"); 
} 

void loop() { 

    // if there's incoming data from the net connection. 
    // send it out the serial port. This is for debugging 
    // purposes only: 
    if (client.available()) { 
    char c = client.read(); 
    Serial.println(c); 
    } 
    //String dataString = "060088765"; 
    // if you're not connected, and ten seconds have passed since 
    // your last connection, then connect again and send data: 
    if(!client.connected()) 
    { 
    Serial.println(); 
    Serial.println("disconnecting."); 
    client.stop(); 
    //sendData(dataString); 
    for(;;) 
     ; 
    } 
} 

// this method makes a HTTP connection to the server: 
void sendData(String thisData) { 
    // if there's a successful connection: 
    Serial.println("send data"); 
    if (client.connect(server, 8080)) { 
    String content = "value=0600887654"; 
    Serial.println(content); 
    Serial.println("connected"); 

    client.println("POST /hos HTTP/1.1"); 
    client.println("Host:localhost"); 
    client.println("Connection:Keep-Alive"); 
    client.println("Cache-Control:max-age=0"); 
    client.println("Content-Type: application/x-www-form-urlencoded\n"); 
    client.println("Content-Length: "); 
    client.println(content.length()); 
    client.println("\n\n"); 
    client.println(content); 
    } 
    else { 
    // if you couldn't make a connection: 
    Serial.println("form connection failed"); 
    Serial.println(); 
    Serial.println("disconnecting."); 
    client.stop(); 
    } 
} 


void printWifiStatus() { 
    // print the SSID of the network you're attached to: 
    Serial.println("SSID: "); 
    Serial.println(WiFi.SSID()); 

    // print your WiFi shield's IP address: 
    IPAddress ip = WiFi.localIP(); 
    Serial.println("IP Address: "); 
    Serial.println(ip); 

    // print the received signal strength: 
    long rssi = WiFi.RSSI(); 
    Serial.println("signal strength (RSSI):"); 
    Serial.println(rssi); 
    Serial.println(" dBm"); 
} 
+0

感謝沒有評論downvoting。 – Fred

回答

0

這可能比答案更多的建議。

如果我做這樣的事情,我不會在Arduino的開始。無休止的編譯,下載,運行,查看print()會讓我瘋狂。我會完全建立客戶端/服務器交互的原型,無論你在什麼位置觸手可及,最好是使用調試器。 (使用Java,Python,PHP,VB,不管你知道,你可以一起拍)

其次,我會在服務器上運行Wireshark的,這樣我可以看到什麼是被髮送和迴應。

然後我會將相同的交互移交給Arduino。再次檢查Wireshark以確認您獲得了您的預期。如果你發送相同的字節,你應該得到相同的響應。


即使您選擇直接在Arduino上實現,也應考慮讓Wireshark捕獲實際的網絡流量。

使用Wireshark,你可能會看到的Arduino的println()不發送服務器的正確行結束。

此外,也不能保證最後的println()實際發送。網絡堆棧實現可以自由緩衝,因爲它看起來合適。你可能需要一個flush()。數據包跟蹤會顯示這一點。

對於數據包捕獲,您可能會發現時間很重要。理論上TCP是一個流,你應該能夠在1個數據包中一次發送POST數據1個字符,並且一切都可以工作。但是,由於服務器的標準超時,Arduino可能會非常緩慢地執行這些println()。在這種情況下,您會在Arduino發送完之前看到服務器響應。

+0

我提到了一些問題,因爲我採取了所有這些步驟。 – Fred

2

也許,你的一些「Serial.println」和「client.println」命令應該是「Serial.print」和「client.print」代替。例如:

client.print(「Content-Length:」);

client.println(content.length());

將避免在文本和數字之間添加換行符。