Closed jonmacd closed 5 years ago
Hi,
Thank you very much for this clear and complete report.
This issue was the perfect opportunity to try my new Arduino library: StreamUtils.
I used the ReadLoggingStream
to see what came back from nhl.com.
Here is what I saw:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
Cache-Control: max-age=20, public
Date: Sat, 20 Apr 2019 10:00:34 GMT
Server: Apache-Coyote/1.1
Vary: Accept-Encoding
Age: 3
X-Cache: Hit from cloudfront
Via: 1.1 ac987789ab8e4a7dbf75086d523e8589.cloudfront.net (CloudFront)
X-Amz-Cf-Id: -pXqZSRaoQGFBr51YrlerJx_j-Zr7NcK7LliVtk6CQnIuoIVpZuMQg==
6b9
That's a very common problem: the response uses chunked transfer encoding and 6b9
is the size of the chunk.
We don't see the rest of the response because ArduinoJson stopped reading.
So: good news and bad news.
Good news for you: you can fix your program by replacing just one character in the source code.
Simply change HTTP/1.1
to HTTP/1.0
in the request.
Bad news for me: ArduinoJson should return an InvalidInput
instead of Ok
when it reads 6b9
.
I'll fix that ASAP.
Regards, Benoit
After patching the code, I received the error IncompleteInput
, I believe this is a race condition in WifiClient
, because if I keep ReadLoggingStream
in the way (ie if we read slowly), everything works fine. We saw something similar in #951. BTW, I'm using the latest core for ESP8266: 2.5.0.
Hopefully, you'll not have the same problem. If you do, maybe I can create a new class in StreamUtils to slow down the read part.
Hi Benoit,
Thanks for looking into this. I'm getting deserializeJson() failed: IncompleteInput
now.
How did you use your StreamUtils library to slow down the reading exactly?
Hu @jonmacd,
I spent a lot of time trying to find a reliable solution and my conclusion is: there is definitely something wrong with WiFiClient
.
What I tried:
yield()
between each readStream
keepAlive()
I performed all my tests with ESP8266 core for Arduino 2.5.0.
It's not related to ArduinoJson; simply try to read the whole content with Stream::read()
and you'll see that the stream is truncated.
Here are my recommendations: try another version of the core or change the microcontroller as Bill did. Anyways, please keep me informed with your results.
Regards, Benoit
I do own an ESP32, however I currently have it in use somewhere else. There are quite a few shortcomings of the ESP8266 that I'm not a fan of (lack of WPA-Enterprise for example). I might pickup some more ESP32s and switch over to using them for all my WiFi based projects. The ESP32 seems to be much better supported these days.
Thanks again for your help Benoit!
I'm trying to parse some JSON data. I've been successful using other sets of data. But something seems to be going wrong with this particular one. It appears to work intermittently. From time to time it does successfully print the correct piece of data. None of the GET requests themselves are failing. They're all passing with code 200.
I'm using a NodeMCU (ESP8266) with the latest version of PlatformIO and the ArduinoJson library. I used arduinojson.org/v6/assistant to calculate the dynamic document capacity.
Code:
And here's a serial monitor output example of the code above to show you what I mean by inconsistant. Sometimes it will correctly grab the copyright item and sometimes it's empty. If it is successful it will always work the second time around as well.
Serial Monitor Example: