0

arduinoのwifiシールドからJavaサーブレットに投稿しようとしています。サーブレットはurlgetとjquerypostで機能しますが、arduinoコードでヘッダーを分類できません。どんな助けでも大歓迎です!

サーバーは200を返しますが、ペイロード「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");
}
4

2 に答える 2

2

おそらく、「Serial.println」および「client.println」コマンドの一部は、代わりに「Serial.print」および「client.print」である必要があります。例えば:

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

client.println(content.length());

テキストと数字の間に改行を追加することを避けます。

于 2013-01-06T14:47:05.083 に答える
0

これは、答えよりもアプローチに関するアドバイスの方が多いかもしれません。

このようなことをしているとしたら、Arduinoから始めることはありません。無限のコンパイル、ダウンロード、実行、print()を見ると、私は夢中になります。私はあなたがあなたの指先で持っているもの、できればデバッガーを使ったもので、クライアント/サーバーの相互作用を完全にプロトタイプ化します。(Java、Python、PHP、VB、一緒に叩くことができるとわかっているものは何でも)

次に、サーバー上でWiresharkを実行して、送信および応答された内容を正確に確認できるようにします。

次に、同じインタラクションをArduinoに移植します。再度Wiresharkで検査して、期待どおりの結果が得られていることを確認します。同じバイトを送信すると、同じ応答が返されるはずです。


Arduinoに直接実装することを選択した場合でも、実際のネットワークトラフィックをキャプチャするためにWiresharkを使用することを検討してください。

Wiresharkを使用すると、Arduino println()がサーバーの正しい行末を送信していないことがわかります。

また、最後のprintln()が実際に送信されるという保証はありません。ネットワークスタックの実装は、適切と思われる場合は自由にバッファリングできます。あなたはflush()を必要とするかもしれません。パケットトレースはこれを示します。

パケットキャプチャを使用すると、時間が重要になる場合があります。理論的にはTCPはストリームであり、そのPOSTデータを一度に1文字ずつ1パケットで送信でき、すべてが機能するはずです。しかし、Arduinoは、サーバーの標準によるこれらのprintln()の実行が非常に遅いため、タイムアウトになる可能性があります。このような場合、Arduinoが送信を完了する前にサーバーが応答するのがわかります。

于 2013-01-05T08:48:18.303 に答える