|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov |
| 3 | + |
| 4 | +#include <Arduino.h> |
| 5 | +#include <AsyncTCP.h> |
| 6 | +#include <StreamString.h> |
| 7 | +#include <WiFi.h> |
| 8 | + |
| 9 | +#include <functional> |
| 10 | +#include <string> |
| 11 | + |
| 12 | +#define WIFI_SSID "IoT" |
| 13 | +#define WIFI_PASSWORD "" |
| 14 | + |
| 15 | +void fetchAsync(const char *host, std::function<void(const StreamString *)> onDone) { |
| 16 | + Serial.printf("[%s] Fetching: http://%s...\n", host, host); |
| 17 | + |
| 18 | + // buffer where we will accumulate the received data |
| 19 | + StreamString *content = new StreamString(); |
| 20 | + |
| 21 | + // reserve enough space to avoid reallocations |
| 22 | + content->reserve(32 * 1024); |
| 23 | + |
| 24 | + // create a new client |
| 25 | + AsyncClient *client = new AsyncClient(); |
| 26 | + |
| 27 | + // register a callback when the client disconnects |
| 28 | + client->onDisconnect([content, host, onDone](void *arg, AsyncClient *client) { |
| 29 | + Serial.printf("[%s] Disconnected.\n", host); |
| 30 | + onDone(content); |
| 31 | + delete client; |
| 32 | + delete content; |
| 33 | + }); |
| 34 | + |
| 35 | + // register a callback when an error occurs |
| 36 | + client->onError([host, onDone](void *arg, AsyncClient *client, int8_t error) { |
| 37 | + Serial.printf("[%s] Error: %s\n", host, client->errorToString(error)); |
| 38 | + }); |
| 39 | + |
| 40 | + // register a callback when data arrives, to accumulate it |
| 41 | + client->onData([host, content](void *arg, AsyncClient *client, void *data, size_t len) { |
| 42 | + Serial.printf("[%s] Received %u bytes...\n", host, len); |
| 43 | + content->write((const uint8_t *)data, len); |
| 44 | + }); |
| 45 | + |
| 46 | + // register a callback when we are connected |
| 47 | + client->onConnect([host](void *arg, AsyncClient *client) { |
| 48 | + Serial.printf("[%s] Connected!\n", host); |
| 49 | + |
| 50 | + // send request |
| 51 | + client->write("GET / HTTP/1.1\r\n"); |
| 52 | + client->write("Host: "); |
| 53 | + client->write(host); |
| 54 | + client->write("\r\n"); |
| 55 | + client->write("User-Agent: ESP32\r\n"); |
| 56 | + client->write("Connection: close\r\n"); |
| 57 | + client->write("\r\n"); |
| 58 | + }); |
| 59 | + |
| 60 | + Serial.printf("[%s] Connecting...\n", host); |
| 61 | + |
| 62 | + client->setRxTimeout(20000); |
| 63 | + // client->setAckTimeout(10000); |
| 64 | + client->setNoDelay(true); |
| 65 | + |
| 66 | + if (!client->connect(host, 80)) { |
| 67 | + Serial.printf("[%s] Failed to connect!\n", host); |
| 68 | + delete client; |
| 69 | + delete content; |
| 70 | + onDone(nullptr); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void setup() { |
| 75 | + Serial.begin(115200); |
| 76 | + while (!Serial) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // connect to WiFi |
| 81 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 82 | + while (WiFi.status() != WL_CONNECTED) { |
| 83 | + delay(500); |
| 84 | + } |
| 85 | + Serial.println("Connected to WiFi!"); |
| 86 | + Serial.println(WiFi.localIP()); |
| 87 | + |
| 88 | + // fetch asynchronously 2 websites: |
| 89 | + |
| 90 | + // equivalent to curl -v --raw http://www.google.com/ |
| 91 | + fetchAsync("www.google.com", [](const StreamString *content) { |
| 92 | + if (content) { |
| 93 | + Serial.printf("[www.google.com] Fetched website:\n%s\n", content->c_str()); |
| 94 | + } else { |
| 95 | + Serial.println("[www.google.com] Failed to fetch website!"); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + // equivalent to curl -v --raw http://www.time.org/ |
| 100 | + fetchAsync("www.time.org", [](const StreamString *content) { |
| 101 | + if (content) { |
| 102 | + Serial.printf("[www.time.org] Fetched website:\n%s\n", content->c_str()); |
| 103 | + } else { |
| 104 | + Serial.println("[www.time.org] Failed to fetch website!"); |
| 105 | + } |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +void loop() { |
| 110 | + delay(500); |
| 111 | +} |
0 commit comments