| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
This example demonstrates how to send data to a remote server asynchronously.
|
|
|
6 |
Run on the remote computer: nc -l -p 1234
|
|
|
7 |
|
|
|
8 |
You should see in the logs:
|
|
|
9 |
|
|
|
10 |
Connected!
|
|
|
11 |
Will send 5760 bytes...
|
|
|
12 |
Acked 1436 bytes in 19 ms
|
|
|
13 |
Will send 1436 bytes...
|
|
|
14 |
Acked 1436 bytes in 2 ms
|
|
|
15 |
Will send 996 bytes...
|
|
|
16 |
Waiting for acks...
|
|
|
17 |
Acked 1436 bytes in 1 ms
|
|
|
18 |
Acked 1436 bytes in 5 ms
|
|
|
19 |
Acked 1452 bytes in 17 ms
|
|
|
20 |
Acked 996 bytes in 28 ms
|
|
|
21 |
Buffer received - next send in 2 sec
|
|
|
22 |
Will send 5760 bytes...
|
|
|
23 |
Acked 1436 bytes in 14 ms
|
|
|
24 |
Will send 1436 bytes...
|
|
|
25 |
Acked 1436 bytes in 2 ms
|
|
|
26 |
Acked 1436 bytes in 0 ms
|
|
|
27 |
Acked 1452 bytes in 1 ms
|
|
|
28 |
Will send 996 bytes...
|
|
|
29 |
Waiting for acks...
|
|
|
30 |
Acked 1436 bytes in 3 ms
|
|
|
31 |
Acked 996 bytes in 18 ms
|
|
|
32 |
Buffer received - next send in 2 sec
|
|
|
33 |
|
|
|
34 |
And in the remote terminal 3072 characters sent [......... ...........] and so on.
|
|
|
35 |
*/
|
|
|
36 |
|
|
|
37 |
#include <Arduino.h>
|
|
|
38 |
#include <AsyncTCP.h>
|
|
|
39 |
#include <StreamString.h>
|
|
|
40 |
#include <WiFi.h>
|
|
|
41 |
#include <assert.h>
|
|
|
42 |
|
|
|
43 |
#include <functional>
|
|
|
44 |
#include <string>
|
|
|
45 |
|
|
|
46 |
#define WIFI_SSID "IoT"
|
|
|
47 |
#define WIFI_PASSWORD ""
|
|
|
48 |
|
|
|
49 |
#define REMOTE_IP "192.168.125.116"
|
|
|
50 |
#define REMOTE_PORT 1234
|
|
|
51 |
|
|
|
52 |
#define BUFFER_SIZE 8 * 1024
|
|
|
53 |
|
|
|
54 |
static char buffer[BUFFER_SIZE] = {0};
|
|
|
55 |
static size_t bufferPos = 0;
|
|
|
56 |
|
|
|
57 |
// 0 == disconnected
|
|
|
58 |
// 1 == connecting
|
|
|
59 |
// 2 == connected
|
|
|
60 |
static uint8_t state = 0;
|
|
|
61 |
|
|
|
62 |
// number of bytes waiting for a ack
|
|
|
63 |
static size_t waitingAck = 0;
|
|
|
64 |
|
|
|
65 |
static AsyncClient client;
|
|
|
66 |
|
|
|
67 |
void setup() {
|
|
|
68 |
Serial.begin(115200);
|
|
|
69 |
while (!Serial) {
|
|
|
70 |
continue;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// connect to WiFi
|
|
|
74 |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
|
75 |
while (WiFi.status() != WL_CONNECTED) {
|
|
|
76 |
delay(500);
|
|
|
77 |
}
|
|
|
78 |
Serial.println("Connected to WiFi!");
|
|
|
79 |
Serial.println(WiFi.localIP());
|
|
|
80 |
|
|
|
81 |
// fill buffer
|
|
|
82 |
buffer[0] = '[';
|
|
|
83 |
for (size_t i = 1; i < BUFFER_SIZE - 1; i++) {
|
|
|
84 |
buffer[i] = '.';
|
|
|
85 |
}
|
|
|
86 |
buffer[BUFFER_SIZE - 1] = ']';
|
|
|
87 |
|
|
|
88 |
// register a callback when the client disconnects
|
|
|
89 |
client.onDisconnect([](void *arg, AsyncClient *client) {
|
|
|
90 |
Serial.printf("Disconnected.\n");
|
|
|
91 |
state = 0;
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
// register a callback when an error occurs
|
|
|
95 |
client.onError([](void *arg, AsyncClient *client, int8_t error) {
|
|
|
96 |
Serial.printf("Error: %s\n", client->errorToString(error));
|
|
|
97 |
});
|
|
|
98 |
|
|
|
99 |
// register a callback when data arrives, to accumulate it
|
|
|
100 |
client.onData([](void *arg, AsyncClient *client, void *data, size_t len) {
|
|
|
101 |
Serial.printf("Received %u bytes...\n", len);
|
|
|
102 |
Serial.write((uint8_t *)data, len);
|
|
|
103 |
});
|
|
|
104 |
|
|
|
105 |
// register a callback when we are connected
|
|
|
106 |
client.onConnect([](void *arg, AsyncClient *client) {
|
|
|
107 |
Serial.printf("Connected!\n");
|
|
|
108 |
state = 2;
|
|
|
109 |
});
|
|
|
110 |
|
|
|
111 |
client.onAck([](void *arg, AsyncClient *client, size_t len, uint32_t time) {
|
|
|
112 |
Serial.printf("Acked %u bytes in %" PRIu32 " ms\n", len, time);
|
|
|
113 |
assert(waitingAck >= len);
|
|
|
114 |
waitingAck -= len;
|
|
|
115 |
});
|
|
|
116 |
|
|
|
117 |
client.setRxTimeout(20000);
|
|
|
118 |
client.setNoDelay(true);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
void loop() {
|
|
|
122 |
switch (state) {
|
|
|
123 |
case 0:
|
|
|
124 |
{
|
|
|
125 |
Serial.printf("Connecting...\n");
|
|
|
126 |
if (!client.connect(REMOTE_IP, REMOTE_PORT)) {
|
|
|
127 |
Serial.printf("Failed to connect!\n");
|
|
|
128 |
delay(1000); // to not flood logs
|
|
|
129 |
} else {
|
|
|
130 |
state = 1;
|
|
|
131 |
}
|
|
|
132 |
break;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
case 1:
|
|
|
136 |
{
|
|
|
137 |
Serial.printf("Still connecting...\n");
|
|
|
138 |
delay(500); // to not flood logs
|
|
|
139 |
break;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
case 2:
|
|
|
143 |
{
|
|
|
144 |
// fill PCB space until we can
|
|
|
145 |
size_t willSend;
|
|
|
146 |
while (bufferPos < BUFFER_SIZE && (willSend = client.write(buffer + bufferPos, BUFFER_SIZE - bufferPos))) {
|
|
|
147 |
Serial.printf("Will send %u bytes...\n", willSend);
|
|
|
148 |
bufferPos += willSend;
|
|
|
149 |
waitingAck += willSend;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
// we have sent the whole buffer ?
|
|
|
153 |
if (bufferPos >= BUFFER_SIZE) {
|
|
|
154 |
// wait for acks, or send again after 2 sec
|
|
|
155 |
if (waitingAck) {
|
|
|
156 |
Serial.printf("Waiting for acks...\n");
|
|
|
157 |
delay(100);
|
|
|
158 |
} else {
|
|
|
159 |
Serial.printf("Buffer received - next send in 2 sec\n");
|
|
|
160 |
delay(2000);
|
|
|
161 |
bufferPos = 0;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
break;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
default: break;
|
|
|
168 |
}
|
|
|
169 |
}
|