chore: import local project into Gitea
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
|
||||
|
||||
/*
|
||||
This example demonstrates how to send data to a remote server asynchronously.
|
||||
Run on the remote computer: nc -l -p 1234
|
||||
|
||||
You should see in the logs:
|
||||
|
||||
Connected!
|
||||
Will send 5760 bytes...
|
||||
Acked 1436 bytes in 19 ms
|
||||
Will send 1436 bytes...
|
||||
Acked 1436 bytes in 2 ms
|
||||
Will send 996 bytes...
|
||||
Waiting for acks...
|
||||
Acked 1436 bytes in 1 ms
|
||||
Acked 1436 bytes in 5 ms
|
||||
Acked 1452 bytes in 17 ms
|
||||
Acked 996 bytes in 28 ms
|
||||
Buffer received - next send in 2 sec
|
||||
Will send 5760 bytes...
|
||||
Acked 1436 bytes in 14 ms
|
||||
Will send 1436 bytes...
|
||||
Acked 1436 bytes in 2 ms
|
||||
Acked 1436 bytes in 0 ms
|
||||
Acked 1452 bytes in 1 ms
|
||||
Will send 996 bytes...
|
||||
Waiting for acks...
|
||||
Acked 1436 bytes in 3 ms
|
||||
Acked 996 bytes in 18 ms
|
||||
Buffer received - next send in 2 sec
|
||||
|
||||
And in the remote terminal 3072 characters sent [......... ...........] and so on.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <StreamString.h>
|
||||
#include <WiFi.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define WIFI_SSID "IoT"
|
||||
#define WIFI_PASSWORD ""
|
||||
|
||||
#define REMOTE_IP "192.168.125.116"
|
||||
#define REMOTE_PORT 1234
|
||||
|
||||
#define BUFFER_SIZE 8 * 1024
|
||||
|
||||
static char buffer[BUFFER_SIZE] = {0};
|
||||
static size_t bufferPos = 0;
|
||||
|
||||
// 0 == disconnected
|
||||
// 1 == connecting
|
||||
// 2 == connected
|
||||
static uint8_t state = 0;
|
||||
|
||||
// number of bytes waiting for a ack
|
||||
static size_t waitingAck = 0;
|
||||
|
||||
static AsyncClient client;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// connect to WiFi
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("Connected to WiFi!");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// fill buffer
|
||||
buffer[0] = '[';
|
||||
for (size_t i = 1; i < BUFFER_SIZE - 1; i++) {
|
||||
buffer[i] = '.';
|
||||
}
|
||||
buffer[BUFFER_SIZE - 1] = ']';
|
||||
|
||||
// register a callback when the client disconnects
|
||||
client.onDisconnect([](void *arg, AsyncClient *client) {
|
||||
Serial.printf("Disconnected.\n");
|
||||
state = 0;
|
||||
});
|
||||
|
||||
// register a callback when an error occurs
|
||||
client.onError([](void *arg, AsyncClient *client, int8_t error) {
|
||||
Serial.printf("Error: %s\n", client->errorToString(error));
|
||||
});
|
||||
|
||||
// register a callback when data arrives, to accumulate it
|
||||
client.onData([](void *arg, AsyncClient *client, void *data, size_t len) {
|
||||
Serial.printf("Received %u bytes...\n", len);
|
||||
Serial.write((uint8_t *)data, len);
|
||||
});
|
||||
|
||||
// register a callback when we are connected
|
||||
client.onConnect([](void *arg, AsyncClient *client) {
|
||||
Serial.printf("Connected!\n");
|
||||
state = 2;
|
||||
});
|
||||
|
||||
client.onAck([](void *arg, AsyncClient *client, size_t len, uint32_t time) {
|
||||
Serial.printf("Acked %u bytes in %" PRIu32 " ms\n", len, time);
|
||||
assert(waitingAck >= len);
|
||||
waitingAck -= len;
|
||||
});
|
||||
|
||||
client.setRxTimeout(20000);
|
||||
client.setNoDelay(true);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch (state) {
|
||||
case 0:
|
||||
{
|
||||
Serial.printf("Connecting...\n");
|
||||
if (!client.connect(REMOTE_IP, REMOTE_PORT)) {
|
||||
Serial.printf("Failed to connect!\n");
|
||||
delay(1000); // to not flood logs
|
||||
} else {
|
||||
state = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 1:
|
||||
{
|
||||
Serial.printf("Still connecting...\n");
|
||||
delay(500); // to not flood logs
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
// fill PCB space until we can
|
||||
size_t willSend;
|
||||
while (bufferPos < BUFFER_SIZE && (willSend = client.write(buffer + bufferPos, BUFFER_SIZE - bufferPos))) {
|
||||
Serial.printf("Will send %u bytes...\n", willSend);
|
||||
bufferPos += willSend;
|
||||
waitingAck += willSend;
|
||||
}
|
||||
|
||||
// we have sent the whole buffer ?
|
||||
if (bufferPos >= BUFFER_SIZE) {
|
||||
// wait for acks, or send again after 2 sec
|
||||
if (waitingAck) {
|
||||
Serial.printf("Waiting for acks...\n");
|
||||
delay(100);
|
||||
} else {
|
||||
Serial.printf("Buffer received - next send in 2 sec\n");
|
||||
delay(2000);
|
||||
bufferPos = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
// Run a server at the root of the project with:
|
||||
// > python3 -m http.server 3333
|
||||
// Now you can open a browser and test it works by visiting http://192.168.125.122:3333/ or http://192.168.125.122:3333/README.md
|
||||
#define HOST "192.168.125.122"
|
||||
#define PORT 3333
|
||||
|
||||
// WiFi SSID to connect to
|
||||
#define WIFI_SSID "IoT"
|
||||
|
||||
// 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP)
|
||||
#define MAX_CLIENTS CONFIG_LWIP_MAX_ACTIVE_TCP
|
||||
// #define MAX_CLIENTS 1
|
||||
|
||||
size_t permits = MAX_CLIENTS;
|
||||
|
||||
void makeRequest() {
|
||||
if (!permits) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("** permits: %d\n", permits);
|
||||
|
||||
AsyncClient *client = new AsyncClient;
|
||||
|
||||
client->onError([](void *arg, AsyncClient *client, int8_t error) {
|
||||
Serial.printf("** error occurred %s \n", client->errorToString(error));
|
||||
client->close(true);
|
||||
delete client;
|
||||
});
|
||||
|
||||
client->onConnect([](void *arg, AsyncClient *client) {
|
||||
permits--;
|
||||
Serial.printf("** client has been connected: %" PRIu16 "\n", client->localPort());
|
||||
|
||||
client->onDisconnect([](void *arg, AsyncClient *client) {
|
||||
Serial.printf("** client has been disconnected: %" PRIu16 "\n", client->localPort());
|
||||
client->close(true);
|
||||
delete client;
|
||||
|
||||
permits++;
|
||||
makeRequest();
|
||||
});
|
||||
|
||||
client->onData([](void *arg, AsyncClient *client, void *data, size_t len) {
|
||||
Serial.printf("** data received by client: %" PRIu16 ": len=%u\n", client->localPort(), len);
|
||||
});
|
||||
|
||||
client->write("GET /README.md HTTP/1.1\r\nHost: " HOST "\r\nUser-Agent: ESP\r\nConnection: close\r\n\r\n");
|
||||
});
|
||||
|
||||
if (client->connect(HOST, PORT)) {
|
||||
} else {
|
||||
Serial.println("** connection failed");
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
continue;
|
||||
}
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(WIFI_SSID);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("** connected to WiFi");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
||||
makeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(1000);
|
||||
Serial.printf("** free heap: %" PRIu32 "\n", ESP.getFreeHeap());
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <StreamString.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define WIFI_SSID "IoT"
|
||||
#define WIFI_PASSWORD ""
|
||||
|
||||
void fetchAsync(const char *host, std::function<void(const StreamString *)> onDone) {
|
||||
Serial.printf("[%s] Fetching: http://%s...\n", host, host);
|
||||
|
||||
// buffer where we will accumulate the received data
|
||||
StreamString *content = new StreamString();
|
||||
|
||||
// reserve enough space to avoid reallocations
|
||||
content->reserve(32 * 1024);
|
||||
|
||||
// create a new client
|
||||
AsyncClient *client = new AsyncClient();
|
||||
|
||||
// register a callback when the client disconnects
|
||||
client->onDisconnect([content, host, onDone](void *arg, AsyncClient *client) {
|
||||
Serial.printf("[%s] Disconnected.\n", host);
|
||||
onDone(content);
|
||||
delete client;
|
||||
delete content;
|
||||
});
|
||||
|
||||
// register a callback when an error occurs
|
||||
client->onError([host, onDone](void *arg, AsyncClient *client, int8_t error) {
|
||||
Serial.printf("[%s] Error: %s\n", host, client->errorToString(error));
|
||||
});
|
||||
|
||||
// register a callback when data arrives, to accumulate it
|
||||
client->onData([host, content](void *arg, AsyncClient *client, void *data, size_t len) {
|
||||
Serial.printf("[%s] Received %u bytes...\n", host, len);
|
||||
content->write((const uint8_t *)data, len);
|
||||
});
|
||||
|
||||
// register a callback when we are connected
|
||||
client->onConnect([host](void *arg, AsyncClient *client) {
|
||||
Serial.printf("[%s] Connected!\n", host);
|
||||
|
||||
// send request
|
||||
client->write("GET / HTTP/1.1\r\n");
|
||||
client->write("Host: ");
|
||||
client->write(host);
|
||||
client->write("\r\n");
|
||||
client->write("User-Agent: ESP32\r\n");
|
||||
client->write("Connection: close\r\n");
|
||||
client->write("\r\n");
|
||||
});
|
||||
|
||||
Serial.printf("[%s] Connecting...\n", host);
|
||||
|
||||
if (!client->connect(host, 80)) {
|
||||
Serial.printf("[%s] Failed to connect!\n", host);
|
||||
delete client;
|
||||
delete content;
|
||||
onDone(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// connect to WiFi
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("Connected to WiFi!");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// fetch asynchronously 2 websites:
|
||||
|
||||
// equivalent to curl -v --raw http://www.time.org/
|
||||
fetchAsync("www.time.org", [](const StreamString *content) {
|
||||
if (content) {
|
||||
Serial.printf("[www.time.org] Fetched website:\n%s\n", content->c_str());
|
||||
} else {
|
||||
Serial.println("[www.time.org] Failed to fetch website!");
|
||||
}
|
||||
});
|
||||
|
||||
// equivalent to curl -v --raw http://www.google.com/
|
||||
fetchAsync("www.google.com", [](const StreamString *content) {
|
||||
if (content) {
|
||||
Serial.printf("[www.google.com] Fetched website:\n%s\n", content->c_str());
|
||||
} else {
|
||||
Serial.println("[www.google.com] Failed to fetch website!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(100);
|
||||
}
|
||||
Reference in New Issue
Block a user