【问题标题】:ESP8266 randomly cutting responseESP8266 随机切割响应
【发布时间】:2021-01-08 21:57:08
【问题描述】:

我希望我的 NodeMCU V3 从 OpenWeatherMapApi 获取天气数据。我找到了一些示例代码如何发送 GET 请求,但响应被随机截断。读取响应后,我打印收到的 JSON 响应的长度。响应应该有大约 16k 个字符,但每个请求都是随机的。有时应该是 16k,有时是 11k,有时是 13k 等等。这是我的代码:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

WiFiClient client;

const char *url = "/data/2.5/onecall?lat=51.039117&lon=21.072800&appid=xxx&lang=pl&units=metric&exclude=minutely";
const char *host = "api.openweathermap.org";

int fetchWeatherJson(struct weatherDataPacketStruct *wdps) {
  if (!isConnected())
    return -1;
  Serial.println("Fetching weather");
  if (!client.connect(host, 80)) {
    Serial.println("Connection failed");
    return -1;
  }
  client.setTimeout(15000);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }

  Serial.println("reply was:");
  Serial.println("==========");
  String line;
  while (client.available()) {
    line = client.readStringUntil('\n');  //Read Line by Line
   // Serial.println(line); //Print response
  }
  Serial.println(line.length());
  parseWeatherJson(line.c_str(), wdps);

  return 1;
}

有什么问题吗?谢谢。

【问题讨论】:

  • client.available() 在字节之间的间隙处返回 false
  • 感谢您的回复。我试图删除 while 循环并在某些时候添加延迟,但问题仍然存在。我还注意到 JSON 不仅仅是随机剪切的,因为响应的最后一个字符始终是“}”。我认为这可能是 readStringUntil() 的问题,所以我用 readString() 替换了它,但响应仍然被削减。
  • 您可以尝试在while循环中添加一个小的延迟(1-10ms),以允许更多的数据进入。这感觉不是一个非常巧妙的解决方案,但它可以帮助您验证问题是什么。
  • 我尝试在 while 循环中添加各种值的延迟,但仍然没有

标签: c++ arduino esp8266 nodemcu


【解决方案1】:

我在这里找到了一些解决方案:ESP8266 - Response from server gets cut
使用 read() 而不是 readString() 并放置 delay(1) 似乎起初可以工作,但是当我将读取的字符保存到数组时打印它,响应再次被削减。但是,当我删除延迟(1)时,一切都开始工作了,不知道为什么。这是最终代码:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

WiFiClient client;

const char *url = "/data/2.5/onecall?lat=51.039117&lon=21.072800&appid=xxx&lang=pl&units=metric&exclude=minutely";
const char *host = "api.openweathermap.org";
char responseBuff[18000];

int fetchWeatherJson(struct weatherDataPacketStruct *wdps) {
  client.flush();
  if (!isConnected())
    return -1;

  Serial.println("FETCHING weather");
  if (!client.connect(weatherHost, 80)) {
    Serial.println("connection failed");

    return -1;
  }
  client.setTimeout(15000);
  client.print(String("GET ") + weatherUrl + " HTTP/1.1\r\n" +
               "Host: " + weatherHost + "\r\n" +
               "Connection: close\r\n\r\n");

  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  
  long int time = millis();
  long int wait = 1000 * 10;
  int counter = 0;
  while ((time + wait) > millis()) {
    while (client.available()) {
      char c = client.read();
      responseBuff[counter] = c;
      counter++;

      Serial.print(c);

      if (c == '\0')
        continue;
    }
  }
  Serial.println(counter);

  return parseWeatherJson(responseBuff, wdps);
}

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 2016-06-25
    • 1970-01-01
    • 2018-11-17
    • 2017-08-18
    • 2016-09-10
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多