Skip to content

Commit 0a8f5be

Browse files
committed
add more documentation and cleanup the example
1 parent 1e7b968 commit 0a8f5be

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,87 @@
1212

1313
#include <ESP8266httpClient.h>
1414

15+
#define USE_SERIAL Serial1
1516

1617
ESP8266WiFiMulti WiFiMulti;
1718

1819
void setup() {
19-
Serial.begin(115200);
2020

21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
2123

22-
Serial.println();
23-
Serial.println();
24-
Serial.println();
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
2527

26-
27-
for(uint8_t t = 4; t > 0; t--) {
28-
Serial.printf("[SETUP] WAIT %d...\n", t);
29-
Serial.flush();
30-
delay(1000);
31-
}
28+
for(uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
3233

3334
WiFiMulti.addAP("SSID", "PASSWORD");
3435

35-
//WiFi.disconnect();
36-
while(WiFiMulti.run() != WL_CONNECTED) {
37-
delay(100);
38-
}
3936
}
4037

4138
void loop() {
39+
// wait for WiFi connection
4240
if((WiFiMulti.run() == WL_CONNECTED)) {
43-
httpClient http;
4441

45-
Serial.print("[HTTP] begin...\n");
42+
httpClient http;
4643

44+
USE_SERIAL.print("[HTTP] begin...\n");
45+
// configure traged server and url
4746
http.begin("192.168.1.12", 80, "/test.html");
4847

49-
Serial.print("[HTTP] GET...\n");
50-
if(http.GET()) {
48+
USE_SERIAL.print("[HTTP] GET...\n");
49+
// start connection and send HTTP header
50+
int httpCode = http.GET();
51+
if(httpCode) {
52+
// HTTP header has been send and Server response header has been handled
5153

52-
Serial.print("[HTTP] GET... ok.\n");
54+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
5355

54-
size_t len = http.getSize();
56+
// file found at server
57+
if(httpCode == 200) {
5558

56-
uint8_t buff[128] = { 0 };
57-
WiFiClient stream = http.getStream();
58-
while(http.connected() && len > 0) {
59-
size_t size = stream.available();
60-
int c = stream.readBytes(buff, ((size > 128) ? 128 : size));
59+
// get lenght of document (is -1 when Server sends no Content-Length header)
60+
int len = http.getSize();
6161

62-
Serial.write(buff, c);
63-
len -= c;
62+
// create buffer for read
63+
uint8_t buff[128] = { 0 };
6464

65-
delay(0);
66-
}
67-
Serial.println();
68-
Serial.print("[HTTP] connection closed or file end.\n");
65+
// get tcp stream
66+
WiFiClient stream = http.getStream();
6967

70-
} else {
68+
// read all data from server
69+
while(http.connected() && (len > 0 || len == -1)) {
70+
// get available data size
71+
size_t size = stream.available();
7172

72-
Serial.print("[HTTP] GET... fail.\n");
73-
}
73+
if(size) {
74+
// read up to 128 byte
75+
int c = stream.readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
7476

75-
delay(10000);
77+
// write it to Serial
78+
USE_SERIAL.write(buff, c);
79+
80+
if(len > 0) {
81+
len -= c;
82+
}
83+
}
84+
delay(1);
85+
}
86+
87+
USE_SERIAL.println();
88+
USE_SERIAL.print("[HTTP] connection closed or file end.\n");
89+
90+
}
91+
} else {
92+
USE_SERIAL.print("[HTTP] GET... faild, no connection or no HTTP server\n");
93+
}
7694
}
95+
96+
delay(10000);
7797
}
98+

0 commit comments

Comments
 (0)