-->
Page 1 of 1

GitHub StreamHttpClient example explained

PostPosted: Sun May 22, 2016 2:21 pm
by Snoozerman
Hello Community!
This is my first post to the forum. I am trying to understand the concepts of ESP8266 libraries and classes and how they are meant to be used. The documentation is pretty spares in some areas so I have tried to also look at the source code which have some good examples.
Still some things are not totally clear to me.

Take this example;
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

The example includes 3 libraries
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

First, there seems to be several ways to connect to an access point.
Code: Select allESP8266WiFiMulti.addAP("SSID", "PASSWORD");
ESP8266WiFiMulti.run()

...or with WiFi (as defined in ESP8266WiFi.h)
Code: Select allWiFi.begin(ssid, password);


The former gives you ability to specify more than one access point to connect to, correct? Is that the basically the only (and good) motivation for using ESP8266WiFiMulti instead of WiFi.begin()? (If so, makes me wonder why bother with WiFi.begin() at all).
B.t.w. at a first glance in header ESP8266WiFiMulti.h the class seems to have very few methods. But then I found out that several are methods defined only in the .cpp file, for instance ESP8266WiFiMulti.run() . Any particular reason why the run()-method isn't in the header file or somebody just forgot to add it? (I'm a C++ newbe too so maybe there is nothing strange to this other than my limited c++ knowledge).

Then on line 60-72
Code: Select all// get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize();

// create buffer for read
uint8_t buff[128] = { 0 };

// get tcp stream
WiFiClient * stream = http.getStreamPtr();

// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
   // get available data size
   size_t size = stream->available();
   


In the while loop, do we really need to care about the content-length as returned by
Code: Select allhttp.getSize();
Wouldn't it be enough just to do
Code: Select allwhile (stream->available()){


And lastly; Can you read from stream although http.connected() returns false? I.e. can stream->available() return > 0 when http.connected() returns false.

Many questions from a newbe here :roll:
Maybe you can point me in the right direction. Any help would be very appreciated!