Files

82 lines
2.1 KiB
Arduino
Raw Permalink Normal View History

2024-10-13 09:21:27 +02:00
#include <Arduino.h>
#include <AsyncTCP.h>
#include <WiFi.h>
2024-12-15 17:00:32 +01:00
// 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
2024-12-11 10:38:02 +01:00
#define HOST "192.168.125.122"
2024-12-15 17:00:32 +01:00
#define PORT 3333
// WiFi SSID to connect to
#define WIFI_SSID "IoT"
2024-10-13 09:21:27 +02:00
2024-10-13 10:21:00 +02:00
// 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP)
#define MAX_CLIENTS CONFIG_LWIP_MAX_ACTIVE_TCP
2024-10-13 11:28:15 +02:00
// #define MAX_CLIENTS 1
2024-10-13 10:21:00 +02:00
size_t permits = MAX_CLIENTS;
2024-10-13 09:21:27 +02:00
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) {
2024-12-15 17:07:41 +01:00
Serial.printf("** data received by client: %" PRIu16 ": len=%u\n", client->localPort(), len);
2024-10-13 09:21:27 +02:00
});
2024-12-15 17:00:32 +01:00
client->write("GET /README.md HTTP/1.1\r\nHost: " HOST "\r\nUser-Agent: ESP\r\nConnection: close\r\n\r\n");
2024-10-13 09:21:27 +02:00
});
if (client->connect(HOST, PORT)) {
} else {
Serial.println("** connection failed");
}
}
void setup() {
Serial.begin(115200);
while (!Serial)
continue;
WiFi.mode(WIFI_STA);
2024-12-15 17:00:32 +01:00
WiFi.begin(WIFI_SSID);
2024-10-13 09:21:27 +02:00
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("** connected to WiFi");
Serial.println(WiFi.localIP());
2024-10-13 10:21:00 +02:00
for (size_t i = 0; i < MAX_CLIENTS; i++)
2024-10-13 09:21:27 +02:00
makeRequest();
}
void loop() {
2024-10-13 11:28:15 +02:00
delay(1000);
Serial.printf("** free heap: %" PRIu32 "\n", ESP.getFreeHeap());
2024-10-13 09:21:27 +02:00
}