简体   繁体   English

带有标题问题的Arduino WiFi盾牌帖

[英]Arduino WiFi shield post with header problems

I'm trying to do a post from the arduino wifi shield to my java servlet. 我正在尝试从arduino wifi屏蔽到我的java servlet。 The servlet functions with url get, and jquery post, but I can't sort the headers out in my arduino code. servlet函数有url get和jquery post,但是我无法在我的arduino代码中对标题进行排序。 Any help will be greatly appreciated! 任何帮助将不胜感激!

The server returns 200, but I'm not getting the payload "content" as value. 服务器返回200,但我没有将有效负载“content”作为值。 I'm not exactly sure what I'm doing wrong but I'm pretty sure it's in how my headers are setup. 我不确定我做错了什么,但我很确定我的标题是如何设置的。 I've spent the last two days trying to get it. 我花了最后两天试图得到它。

#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");
}

Perhaps, some of your "Serial.println" and "client.println" commands should be "Serial.print" and "client.print" instead. 也许,你的一些“Serial.println”和“client.println”命令应该是“Serial.print”和“client.print”。 For example: 例如:

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

client.println(content.length()); client.println(content.length());

would avoid adding a line break between the text and the number. 会避免在文本和数字之间添加换行符。

This is maybe more advice on an approach than an answer. 这可能是对方法的建议而不是答案。

If I was doing something like this I would not start on the Arduino. 如果我正在做这样的事情,我就不会从Arduino开始。 The endless compile, download, run, look at print()'s would drive me crazy. 无休止的编译,下载,运行,看看print()会让我发疯。 I would fully prototype the client/server interaction in whatever you have at your fingertips, preferably something with a debugger. 无论你掌握什么,我都会完全建立客户端/服务器交互的原型,最好是带有调试器的东西。 (Java, Python, PHP, VB, whatever you know that you can slap together) (Java,Python,PHP,VB,无论你知道什么,你都可以一起拍)

Second, I would run Wireshark on the server so that I could see exactly what was being sent and responded. 其次,我会在服务器上运行Wireshark,这样我就可以确切地看到发送和响应的内容。

Then I would port the same interaction over to the Arduino. 然后我会将相同的交互移植到Arduino。 Again inspect with Wireshark to confirm you are getting what you expected. 再次与Wireshark一起检查以确认您得到了您的预期。 If you send the same bytes, you should get the same response. 如果发送相同的字节,则应该得到相同的响应。


Even if you choose to implement straight on Arduino, consider having Wireshark to capture the actual network traffic. 即使您选择直接在Arduino上实现,也可以考虑让Wireshark捕获实际的网络流量。

With Wireshark, you might see that the Arduino println() is not sending the correct line end for the server. 使用Wireshark,您可能会看到Arduino println()没有为服务器发送正确的行结束。

Also, there is no guarantee that last println() is actually sent. 此外,无法保证最后发送的println()。 The network stack implementation is free to buffer as it sees fit. 网络堆栈实现可以根据需要自由缓冲。 You might need a flush(). 您可能需要flush()。 A packet trace would show this. 数据包跟踪会显示这一点。

With a packet capture you might find that time matters. 通过数据包捕获,您可能会发现时间很重要。 In theory TCP is a stream and you should be able to send that POST data 1 character at a time in 1 packet and everything would work. 从理论上讲,TCP是一个流,您应该能够在1个数据包中一次发送该POST数据1个字符,一切都行。 But the Arduino might be so slow executing those println()'s by the server's standards that it times out. 但Arduino可能会因服务器的标准执行那些println()而执行速度太慢。 In such case you would see the server respond before the Arduino even finished sending. 在这种情况下,您会看到服务器在Arduino完成发送之前做出响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM