Skip to content

Commit 4565bc8

Browse files
committed
Added fetch website example
1 parent 93c6ccd commit 4565bc8

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ jobs:
2828
arduino-cli --config-file ${{ matrix.config }} core update-index
2929
arduino-cli --config-file ${{ matrix.config }} board listall
3030
arduino-cli --config-file ${{ matrix.config }} core install esp32:esp32
31-
- name: Compile Sketch
31+
32+
- name: Client
3233
run: arduino-cli --config-file ${{ matrix.config }} --library ./src/ compile --fqbn esp32:esp32:esp32 ./examples/Client/Client.ino
33-
- name: Compile Sketch with IPv6
34+
35+
- name: Client IPv6
3436
env:
3537
LWIP_IPV6: true
3638
run: arduino-cli --config-file ${{ matrix.config }} --library ./src/ compile --fqbn esp32:esp32:esp32 ./examples/Client/Client.ino
3739

40+
- name: FetchWebsite
41+
run: arduino-cli --config-file ${{ matrix.config }} --library ./src/ compile --fqbn esp32:esp32:esp32 ./examples/FetchWebsite/FetchWebsite.ino
42+
3843
platformio:
3944
name: "pio:${{ matrix.env }}:${{ matrix.board }}"
4045
runs-on: ubuntu-latest
@@ -85,3 +90,4 @@ jobs:
8590
pip install --upgrade platformio
8691
8792
- run: PLATFORMIO_SRC_DIR=examples/Client PIO_BOARD=${{ matrix.board }} pio run -e ${{ matrix.env }}
93+
- run: PLATFORMIO_SRC_DIR=examples/FetchWebsite PIO_BOARD=${{ matrix.board }} pio run -e ${{ matrix.env }}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[platformio]
22
default_envs = arduino-2, arduino-3
33
lib_dir = .
4-
src_dir = examples/Client
4+
; src_dir = examples/Client
5+
src_dir = examples/FetchWebsite
56

67
[env]
78
framework = arduino

0 commit comments

Comments
 (0)