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

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

问题描述

在我的研究工作不久的将来Arduino的项目,我尝试从一个Arduino的网络服务器发送JSON。

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

我试图让我的Arduino当我去到Arduino的IP地址在我的浏览器发回一些虚拟JSON数据。在等待Web服务器的响应浏览器挂起。 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.

现在在我的code我试图发送一个响应头与内容类型:应用程序/ JSON的;字符集= UTF-8 ,所以我不明白为什么我在浏览器中得到的响应是中的Content-Type:text / html的;字符集= 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的code在哪里,我回答web服务器上的客户端请求的一部分:

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

好吧,我可以从提琴手消息从Arduino的我的反应是不完整的推断。但我想不通,我错过了什么。我一直在寻找所有在互联网上的线索,但我只能找到关于如何发送JSON到Arduino的例子,而不是从它,所以我希望你们中的一些可以帮助我找到一个解决方案。或者,也许指向我在其他的方法来从阿尔杜伊诺发送JSON数据到一个网页浏览器的正确方向。

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.

编辑:
这太奇怪了。今天,我试图改变行内容类型:应用程序/ JSON到内容类型:text / html的。现在我收到的JSON数据作为文本字符串。这不是非常有用的,但。然后,我把它改回内容类​​型:应用程序/ JSON(!?!?!),现在,它就像一个魅力。我不知道为什么。这是我在code更改的唯一的事情。

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.

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

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