15
15
#define WIFI_SSID " *********"
16
16
#define WIFI_PASS " *********"
17
17
18
- // 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP)
19
- #define MAX_CLIENTS CONFIG_LWIP_MAX_ACTIVE_TCP
20
- // #define MAX_CLIENTS 1
21
-
22
- size_t permits = MAX_CLIENTS;
18
+ bool client_running = false ;
23
19
24
20
void makeRequest () {
25
- if (!permits) {
21
+ client_running = true ;
22
+ AsyncClient *client = new AsyncClient;
23
+ if (client == nullptr ) {
24
+ Serial.println (" ** could not allocate client" );
25
+ client_running = false ;
26
26
return ;
27
27
}
28
28
29
- Serial.printf (" ** permits: %d\n " , permits);
30
-
31
- AsyncClient *client = new AsyncClient;
32
-
33
29
client->onError ([](void *arg, AsyncClient *client, int8_t error) {
34
30
Serial.printf (" ** error occurred %s \n " , client->errorToString (error));
35
31
client->close (true );
36
32
delete client;
33
+ client_running = false ;
37
34
});
38
35
39
36
client->onConnect ([](void *arg, AsyncClient *client) {
40
- permits--;
41
37
Serial.printf (" ** client has been connected: %" PRIu16 " \n " , client->localPort ());
42
38
43
39
client->onDisconnect ([](void *arg, AsyncClient *client) {
44
40
Serial.printf (" ** client has been disconnected: %" PRIu16 " \n " , client->localPort ());
45
41
client->close (true );
46
42
delete client;
47
-
48
- permits++;
49
- makeRequest ();
43
+ client_running = false ;
50
44
});
51
45
52
46
client->onData ([](void *arg, AsyncClient *client, void *data, size_t len) {
@@ -58,6 +52,7 @@ void makeRequest() {
58
52
59
53
if (!client->connect (HOST, PORT)) {
60
54
Serial.println (" ** connection failed" );
55
+ client_running = false ;
61
56
}
62
57
}
63
58
@@ -74,13 +69,12 @@ void setup() {
74
69
Serial.println ();
75
70
Serial.print (" Connected to WiFi. IP: " );
76
71
Serial.println (WiFi.localIP ());
77
-
78
- for (size_t i = 0 ; i < MAX_CLIENTS; i++) {
79
- makeRequest ();
80
- }
81
72
}
82
73
83
74
void loop () {
75
+ if (!client_running) {
76
+ makeRequest ();
77
+ }
84
78
delay (1000 );
85
79
Serial.printf (" ** free heap: %" PRIu32 " \n " , ESP.getFreeHeap ());
86
80
}
0 commit comments