2013-01-05 3 views
0

arduino wifi 방패에서 java 서블릿으로 게시물을 보내려고합니다. 서블릿은 url get 및 jquery post와 함께 작동하지만 arduino 코드에서 헤더를 정렬 할 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다!헤더 문제가있는 Arduino WiFi 방패 게시

서버가 200을 반환하지만 payload "content"가 값으로 표시되지 않습니다. 나는 내가 뭘 잘못하고 있는지 정확히 알지 못하지만, 헤더가 어떻게 설정되어 있는지 확신 할 수 있습니다. 나는 그것을 얻으려고 지난 2 일을 보내었다.

#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

의견없이 다운 그레이드 해 주셔서 감사합니다. – Fred

답변

0

이것은 답변이 아닌 접근 방법에 대한 조언입니다.

내가 이런 식으로 일을한다면 나는 Arduino에서 시작하지 않을 것입니다. 끝없는 컴파일, 다운로드, 실행, print()의 모습은 나를 미치게 만들 것입니다. 필자는 무엇이든간에 디버거를 사용하여 클라이언트/서버 상호 작용을 완전히 프로토 타입했습니다. (Java, Python, PHP, VB, 당신이 함께 할 수있는 것은 무엇이든간에)

둘째로, 서버에서 Wireshark를 실행하여 정확히 무엇이 보내지고 응답되었는지를 볼 수있었습니다.

그런 다음 Arduino와 동일한 상호 작용을 포트합니다. 다시 Wireshark를 검사하여 기대 한 바를 얻고 있는지 확인하십시오. 같은 바이트를 보내면 동일한 응답을 받아야합니다.


Arduino를 직선으로 구현하는 경우에도 실제 네트워크 트래픽을 캡처하려면 Wireshark를 사용하는 것이 좋습니다.

Wireshark를 사용하면 Arduino println()이 서버의 올바른 행 끝을 보내지 않고 있음을 알 수 있습니다.

또한 마지막 println()이 실제로 전송된다는 보장이 없습니다. 네트워크 스택 구현은 적합하다고 생각되는대로 버퍼링 할 수 있습니다. flush()가 필요할 수 있습니다. 패킷 추적이이를 나타냅니다.

패킷 캡처를 사용하면 시간이 중요 할 수 있습니다. 이론적으로 TCP는 스트림이며 한 패킷에 한 번에 한 문자 씩 POST 데이터를 보낼 수 있어야하며 모든 것이 작동합니다. 하지만 Arduino는 서버의 표준에 따라 println()을 실행하는 것이 너무 느려서 시간이 초과 될 수 있습니다. 이 경우 Arduino가 전송을 완료하기 전에 서버가 응답하는 것을 볼 수 있습니다.

+0

나는이 모든 단계를 밟았 기 때문에 그 중 일부를 언급했다. – Fred

2

아마도 "Serial.println"및 "client.println"명령 중 일부는 "Serial.print"및 "client.print"이어야합니다. 예 :

client.print ("Content-Length :");

client.println (content.length());

은 텍스트와 숫자 사이에 줄 바꿈을 추가하지 않습니다.

관련 문제