如何将带有 JSON 数据的 HTTP 响应从 Arduino 发送到网络浏览器? [英] How to send HTTP response with JSON data from Arduino to web browser?

查看:36
本文介绍了如何将带有 JSON 数据的 HTTP 响应从 Arduino 发送到网络浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我对近期 Arduino 项目的研究中,我正在尝试从 Arduino 网络服务器发送 JSON.

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

当我在浏览器中访问 Arduino 的 IP 地址时,我试图让我的 Arduino 发回一些虚拟的 JSON 数据.浏览器在等待来自网络服务器的响应时挂起.10-15 秒后,这是我得到的响应标头:

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.

现在在我的代码中,我试图发送一个带有 Content-Type: application/json; 的响应头;charset=utf-8 所以我不明白为什么我在浏览器中得到的响应是 Content-Type: text/html;charset=utf-8.

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.

以下是我在网络服务器上响应客户端请求的 Arduino 代码部分:

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

顺便说一下,我修改了本教程中的示例:http://www.raywenderlich.com/38841/arduino-tutorial-temperature-sensor

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

好吧,我可以从 Fiddler 消息中推断出我的 Arduino 响应不完整.但我无法弄清楚我错过了什么.我一直在互联网上寻找线索,但我只能找到关于如何将 JSON 发送到 Arduino 而不是从它发送的示例,所以我希望你们中的一些人可以帮助我找到解决方案.或者也许可以为我指出将 JSON 数据从 Arduino 发送到 Web 浏览器的其他方法的正确方向.

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.

这太奇怪了.今天我尝试将Content-Type: application/json"行更改为Content-Type: text/html".现在我以文本字符串的形式收到了 JSON 数据.虽然这不是很有用.然后我把它改回内容类​​型:应用程序/json",现在它就像一个魅力(!?!?!).我不知道为什么.这是我在代码中唯一改变的地方.

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. :)

推荐答案

我遇到了同样的问题.然后我查看了我使用 wireshark 发送到 arduino 的数据包.这个方便的应用程序向您显示每个数据包中的原始字节.标题和正文之间有一个双\r\n.因此,草图在实际读取正文之前脱离了 while 循环.这也解释了为什么标准的 Web 服务器示例对于来自浏览器的每个请求都会回复两次.

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.

它实际上很有用,因为您可以使用它来切掉标题,然后将剩下的任何内容读入字符串.此字符串将仅包含您的 JSON 文本.

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.

这篇关于如何将带有 JSON 数据的 HTTP 响应从 Arduino 发送到网络浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆