|
12 | 12 |
|
13 | 13 | #include <ESP8266httpClient.h>
|
14 | 14 |
|
| 15 | +#define USE_SERIAL Serial1 |
15 | 16 |
|
16 | 17 | ESP8266WiFiMulti WiFiMulti;
|
17 | 18 |
|
18 | 19 | void setup() {
|
19 |
| - Serial.begin(115200); |
20 | 20 |
|
| 21 | + USE_SERIAL.begin(115200); |
| 22 | + // USE_SERIAL.setDebugOutput(true); |
21 | 23 |
|
22 |
| - Serial.println(); |
23 |
| - Serial.println(); |
24 |
| - Serial.println(); |
| 24 | + USE_SERIAL.println(); |
| 25 | + USE_SERIAL.println(); |
| 26 | + USE_SERIAL.println(); |
25 | 27 |
|
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 | + } |
32 | 33 |
|
33 | 34 | WiFiMulti.addAP("SSID", "PASSWORD");
|
34 | 35 |
|
35 |
| - //WiFi.disconnect(); |
36 |
| - while(WiFiMulti.run() != WL_CONNECTED) { |
37 |
| - delay(100); |
38 |
| - } |
39 | 36 | }
|
40 | 37 |
|
41 | 38 | void loop() {
|
| 39 | + // wait for WiFi connection |
42 | 40 | if((WiFiMulti.run() == WL_CONNECTED)) {
|
43 |
| - httpClient http; |
44 | 41 |
|
45 |
| - Serial.print("[HTTP] begin...\n"); |
| 42 | + httpClient http; |
46 | 43 |
|
| 44 | + USE_SERIAL.print("[HTTP] begin...\n"); |
| 45 | + // configure traged server and url |
47 | 46 | http.begin("192.168.1.12", 80, "/test.html");
|
48 | 47 |
|
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 |
51 | 53 |
|
52 |
| - Serial.print("[HTTP] GET... ok.\n"); |
| 54 | + USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); |
53 | 55 |
|
54 |
| - size_t len = http.getSize(); |
| 56 | + // file found at server |
| 57 | + if(httpCode == 200) { |
55 | 58 |
|
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(); |
61 | 61 |
|
62 |
| - Serial.write(buff, c); |
63 |
| - len -= c; |
| 62 | + // create buffer for read |
| 63 | + uint8_t buff[128] = { 0 }; |
64 | 64 |
|
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(); |
69 | 67 |
|
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(); |
71 | 72 |
|
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)); |
74 | 76 |
|
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 | + } |
76 | 94 | }
|
| 95 | + |
| 96 | + delay(10000); |
77 | 97 | }
|
| 98 | + |
0 commit comments