2013-01-05 27 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());

将避免在文本和数字之间添加换行符。