How to send HTTP response with JSON data from Ardu

2019-06-03 16:12发布

问题:

In my research for a near future Arduino project, I am experimenting with sending JSON from an Arduino webserver.

I am trying to get my Arduino to send back some dummy JSON data when I go to the Arduino's IP address in my browser. The browser hangs while waiting for a response from the webserver. After 10-15 seconds this is the response header I get:

HTTP/1.1 504 Fiddler - Receive Failure
Date: Thu, 24 Sep 2015 20:52:36 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 22:52:36.877

[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.

Now in my code I am trying to send a response header with Content-Type: application/json; charset=utf-8 so I don't understand why the response I get in the browser is of Content-Type: text/html; charset=utf-8.

Following is the part of Arduino code where I am responding to a client request on the webserver:

void loop() {
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: application/json;charset=utf-8");
          client.println("Server: Arduino");
          client.println("Connection: close");
          client.println();
          client.println("[{\"tempIn\":23.2, \"tempOut\":16.8, \"unit\":\"Celcius\" }]");
          client.println();
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
    Serial.println("client disconnected");
  }
}

By the way I modified the example in this tutorial: http://www.raywenderlich.com/38841/arduino-tutorial-temperature-sensor

Well I can infer from the Fiddler message that my response from the Arduino is not complete. But I can't figure out what I am missing. I have been searching all over the internet for a clue but I can only find examples on how to send JSON TO the Arduino and not FROM it so I hope that some of you can help me find a solution. Or maybe point me in the right direction of other ways to send JSON data from the Arduino to a web browser.

EDIT: This is so strange. Today I tried to change the line "Content-Type: application/json" to "Content-Type: text/html". Now I received the JSON data as a text string. This was not very usefull though. Then I changed it back to "Content-Type: application/json" and now it works like a charm (!?!?!).. I have no idea why. This is the only thing I have changed in the code.

I concider this problem solved. :)

回答1:

I had the same problem. Then I looked at the packet I was sending to the arduino using wireshark. This handy app shows you the raw bytes in each packet. There was a double \r\n between the header and the body. So the sketch breaks out of the while loop before actually reading the body. This also explains why the standard Webserver example replies twice for every one request from the browser.

Its actually useful like this tho because you can use this to cut away the header, then read whatever is left into a String. This string will contain only your JSON text.