简体   繁体   English

ESP8266同时请求多个HTTP GET

[英]ESP8266 request multiple HTTP GET simultaneously

I started my first project with the ESP8266. 我从ESP8266开始了我的第一个项目。

It's a Temperature Monitor which shows the data on a webserver. 这是一个温度监视器,可在网络服务器上显示数据。

Since I don't want to refresh the page manually to get the new data, I'm using HTTP requests to display it. 由于我不想手动刷新页面以获取新数据,因此我使用HTTP请求来显示它。

I'm sending 3 different requests, one for the current temperature, one for the highest and one for the lowest. 我要发送3个不同的请求,一个请求当前温度,一个请求最高温度,另一个请求最低温度。

The problem i'm facing is, that the data won't refresh simultaneously, though I am sending all of those at the same time. 我面临的问题是,即使我同时发送所有这些数据,数据也不会同时刷新。

That's the code that's sending the requests: 那是发送请求的代码:

<script>
    function getCurrent() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("current").innerHTML =
        this.responseText;
      }
    };
    xhttp.open("GET", "readCurrent", true);
    xhttp.send();
    }

    function getHigh() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("high").innerHTML =
        this.responseText;
      }
    };
    xhttp.open("GET", "readHigh", true);
    xhttp.send();
    }

    function getLow() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("low").innerHTML =
        this.responseText;
      }
    };
    xhttp.open("GET", "readLow", true);
    xhttp.send();
    }

    setInterval(function() {
      getHigh();
      getLow();
      getCurrent();
    }, 2000);
</script>

And that's the code handling them: 这就是处理它们的代码:

float temp;
float hoechst;
float tiefst;

void handleRoot() {
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
}

void handleCurrent() {
  float t = temp;
  server.send(200, "text/plane", String(t));
}

void handleHigh() {
  float high = hoechst;
  server.send(200, "text/plane", String(high));
}

void handleLow() {
  float low = tiefst;
  server.send(200, "text/plane", String(low));
}

void setup() {
  [...]
  server.on("/", handleRoot);
  server.on("/readCurrent", handleCurrent);
  server.on("/readHigh", handleHigh);
  server.on("/readLow", handleLow);
  [...]
}

The Loop is just updating the Temperatures with this function: 循环仅使用此功能更新温度:

void updateTemperatures() {
  sensor.requestTemperatures();
  yield();
  float low = tiefst;
  float high = hoechst;
  float t = sensor.getTempCByIndex(0);
   if(t < low) {
    low = t;
    } else if(t > high) {
      high = t;
      }
   yield();
   temp = t;
   tiefst = low;
   hoechst = high;
  }

And handling the clients (server.handleClient()) 和处理客户端(server.handleClient())

So my Question: How can I update the data simultaneously, or is that even possible with the ESP8266? 所以我的问题是:如何同时更新数据,或者甚至可以用ESP8266更新数据?

You update the data simultaneously by returning all three values in one request. 通过在一个请求中返回所有三个值,可以同时更新数据。

That would be the way to do it with any web server, let alone one running on an extremely limited processor like the ESP8266. 那将是使用任何Web服务器的方法,更不用说运行在极其有限的处理器(例如ESP8266)上的Web服务器了。

You can return all three values at once with code that looks something like this: 您可以使用如下所示的代码一次返回所有三个值:

void handleAll() {
  String results_json = "{ \"temperature\": " + String(temp) + ",", +
                           "\"high\": " + String(hoechst) + "," +
                           "\"low\": " + String(tiefst) + " }";

  server.send(200, "application/json", results_json);
}

This composes a JSON object with all three values in it. 这将组成一个包含所有三个值的JSON对象。 JSON is "JavaScript Object Notation" and is very easy for Javascript to put together and take apart. JSON是“ JavaScript对象表示法”,很容易将Javascript组合在一起并分解。

You'd also need to update your ESP8266 web server code to add 您还需要更新ESP8266 Web服务器代码以添加

server.on("/readAll", handleAll);

With this change you can eliminate the other three /read handlers. 通过此更改,您可以消除其他三个/ read处理程序。

And you'd need to update your Javascript. 而且您需要更新Javascript。 You'd just need to do one call in Javascript, convert the returned text to a Javascript object and read each of the three values from it to set the elements in the DOM. 您只需要用Javascript进行一次调用,将返回的文本转换为Javascript对象,并从中读取三个值中的每一个即可设置DOM中的元素。 This is something jQuery can so trivially for you. jQuery可以为您带来如此微不足道的东西。

And, it's 'text/plain' , not 'text/plane' . 而且,它是'text/plain'而不是'text/plane'

You might also check out jQuery - it will greatly simplify your Javascript code. 您可能还会签出jQuery-这将大大简化您的Javascript代码。

Simply put: You can't update the data simultaneously because there's only one CPU core. 简而言之:您不能同时更新数据,因为只有一个CPU内核。 Also, you should design with economy in mind, you wanted to have three transactions to get a few numbers... One of the simplest forms of database is CSV, or "Comma-Separated Values"; 另外,您应该在设计时考虑到经济性,您希望进行三笔交易来获得一些数字。数据库的最简单形式之一是CSV,即“逗号分隔值”。 essentially: values separated by commas. 本质上:用逗号分隔的值。

Knowing the order your temperatures are going to be in the list (low, current, high), you can simply create a new variable, or concatenate your variables in the statement that outputs the data, that's low "," current "," high and that would return you something like -1.23455,23.53556,37.23432 that you can easily split into three by looking for the commas, and using string operators. 知道温度在列表中的顺序(低,当前,高温),您可以简单地创建一个新变量,或在输出数据的语句中连接变量,即“低”,“当前”,“高”然后返回-1.23455,23.53556,37.23432之类的内容,您可以通过查找逗号并使用字符串运算符轻松地将其分为三部分。

Now you can get your three values from a single transaction from the low-spec device! 现在,您可以通过低规格设备通过一次交易获得三个值!

Good luck! 祝好运! : ) :)

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

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