|
| 1 | +/** |
| 2 | + StreamHTTPClient.ino |
| 3 | +
|
| 4 | + Created on: 24.05.2015 |
| 5 | +
|
| 6 | +*/ |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | + |
| 10 | +#include <ESP8266WiFi.h> |
| 11 | +#include <ESP8266WiFiMulti.h> |
| 12 | + |
| 13 | +#include <ESP8266HTTPClient.h> |
| 14 | + |
| 15 | +#define USE_SERIAL Serial |
| 16 | + |
| 17 | +ESP8266WiFiMulti WiFiMulti; |
| 18 | + |
| 19 | +void setup() { |
| 20 | + |
| 21 | + USE_SERIAL.begin(115200); |
| 22 | + // USE_SERIAL.setDebugOutput(true); |
| 23 | + |
| 24 | + USE_SERIAL.println(); |
| 25 | + USE_SERIAL.println(); |
| 26 | + USE_SERIAL.println(); |
| 27 | + |
| 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 | + } |
| 33 | + |
| 34 | + WiFi.mode(WIFI_STA); |
| 35 | + WiFiMulti.addAP("SSID", "PASSWORD"); |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +void loop() { |
| 40 | + // wait for WiFi connection |
| 41 | + if ((WiFiMulti.run() == WL_CONNECTED)) { |
| 42 | + |
| 43 | + HTTPClient http; |
| 44 | + |
| 45 | + BearSSL::WiFiClientSecure client; |
| 46 | + |
| 47 | + bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024); |
| 48 | + USE_SERIAL.printf("\nConnecting to https://tls.mbed.org\n"); |
| 49 | + USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); |
| 50 | + if (mfln) { |
| 51 | + client.setBufferSizes(1024, 1024); |
| 52 | + } |
| 53 | + |
| 54 | + USE_SERIAL.print("[HTTP] begin...\n"); |
| 55 | + |
| 56 | + // configure server and url |
| 57 | + const uint8_t fingerprint[20] = {0xEB, 0xD9, 0xDF, 0x37, 0xC2, 0xCC, 0x84, 0x89, 0x00,0xA0, 0x58, 0x52, 0x24, 0x04, 0xE4, 0x37, 0x3E, 0x2B, 0xF1, 0x41}; |
| 58 | + client.setFingerprint(fingerprint); |
| 59 | + |
| 60 | + if(http.begin((Client&) client, "https://tls.mbed.org/")) { |
| 61 | + //if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { |
| 62 | + |
| 63 | + USE_SERIAL.print("[HTTP] GET...\n"); |
| 64 | + // start connection and send HTTP header |
| 65 | + int httpCode = http.GET(); |
| 66 | + if (httpCode > 0) { |
| 67 | + // HTTP header has been send and Server response header has been handled |
| 68 | + USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); |
| 69 | + |
| 70 | + // file found at server |
| 71 | + if (httpCode == HTTP_CODE_OK) { |
| 72 | + |
| 73 | + // get lenght of document (is -1 when Server sends no Content-Length header) |
| 74 | + int len = http.getSize(); |
| 75 | + |
| 76 | + // create buffer for read |
| 77 | + uint8_t buff[128] = { 0 }; |
| 78 | + |
| 79 | + // get tcp stream |
| 80 | + WiFiClient * stream = &client; |
| 81 | + |
| 82 | + // read all data from server |
| 83 | + while (http.connected() && (len > 0 || len == -1)) { |
| 84 | + // get available data size |
| 85 | + size_t size = stream->available(); |
| 86 | + |
| 87 | + if (size) { |
| 88 | + // read up to 128 byte |
| 89 | + int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); |
| 90 | + |
| 91 | + // write it to Serial |
| 92 | + USE_SERIAL.write(buff, c); |
| 93 | + |
| 94 | + if (len > 0) { |
| 95 | + len -= c; |
| 96 | + } |
| 97 | + } |
| 98 | + delay(1); |
| 99 | + } |
| 100 | + |
| 101 | + USE_SERIAL.println(); |
| 102 | + USE_SERIAL.print("[HTTP] connection closed or file end.\n"); |
| 103 | + |
| 104 | + } |
| 105 | + } else { |
| 106 | + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); |
| 107 | + } |
| 108 | + |
| 109 | + http.end(); |
| 110 | + } else { |
| 111 | + USE_SERIAL.printf("Unable to connect\n"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + delay(10000); |
| 116 | +} |
0 commit comments