|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov |
| 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 | + |
| 42 | +#include <functional> |
| 43 | +#include <string> |
| 44 | + |
| 45 | +#define WIFI_SSID "IoT" |
| 46 | +#define WIFI_PASSWORD "" |
| 47 | + |
| 48 | +#define REMOTE_IP "192.168.125.116" |
| 49 | +#define REMOTE_PORT 1234 |
| 50 | + |
| 51 | +#define BUFFER_SIZE 8 * 1024 |
| 52 | + |
| 53 | +static char buffer[BUFFER_SIZE] = {0}; |
| 54 | +static size_t bufferPos = 0; |
| 55 | + |
| 56 | +// 0 == disconnected |
| 57 | +// 1 == connecting |
| 58 | +// 2 == connected |
| 59 | +static uint8_t state = 0; |
| 60 | + |
| 61 | +// number of bytes waiting for a ack |
| 62 | +static size_t waitingAck = 0; |
| 63 | + |
| 64 | +static AsyncClient client; |
| 65 | + |
| 66 | +void setup() { |
| 67 | + Serial.begin(115200); |
| 68 | + while (!Serial) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // connect to WiFi |
| 73 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 74 | + while (WiFi.status() != WL_CONNECTED) { |
| 75 | + delay(500); |
| 76 | + } |
| 77 | + Serial.println("Connected to WiFi!"); |
| 78 | + Serial.println(WiFi.localIP()); |
| 79 | + |
| 80 | + // fill buffer |
| 81 | + buffer[0] = '['; |
| 82 | + for (size_t i = 1; i < BUFFER_SIZE - 1; i++) { |
| 83 | + buffer[i] = '.'; |
| 84 | + } |
| 85 | + buffer[BUFFER_SIZE - 1] = ']'; |
| 86 | + |
| 87 | + // register a callback when the client disconnects |
| 88 | + client.onDisconnect([](void *arg, AsyncClient *client) { |
| 89 | + Serial.printf("Disconnected.\n"); |
| 90 | + state = 0; |
| 91 | + }); |
| 92 | + |
| 93 | + // register a callback when an error occurs |
| 94 | + client.onError([](void *arg, AsyncClient *client, int8_t error) { |
| 95 | + Serial.printf("Error: %s\n", client->errorToString(error)); |
| 96 | + }); |
| 97 | + |
| 98 | + // register a callback when data arrives, to accumulate it |
| 99 | + client.onData([](void *arg, AsyncClient *client, void *data, size_t len) { |
| 100 | + Serial.printf("Received %u bytes...\n", len); |
| 101 | + Serial.write((uint8_t *)data, len); |
| 102 | + }); |
| 103 | + |
| 104 | + // register a callback when we are connected |
| 105 | + client.onConnect([](void *arg, AsyncClient *client) { |
| 106 | + Serial.printf("Connected!\n"); |
| 107 | + state = 2; |
| 108 | + }); |
| 109 | + |
| 110 | + client.onAck([](void *arg, AsyncClient *client, size_t len, uint32_t time) { |
| 111 | + Serial.printf("Acked %u bytes in %" PRIu32 " ms\n", len, time); |
| 112 | + assert(waitingAck >= len); |
| 113 | + waitingAck -= len; |
| 114 | + }); |
| 115 | + |
| 116 | + client.setRxTimeout(20000); |
| 117 | + client.setNoDelay(true); |
| 118 | +} |
| 119 | + |
| 120 | +void loop() { |
| 121 | + switch (state) { |
| 122 | + case 0: |
| 123 | + { |
| 124 | + Serial.printf("Connecting...\n"); |
| 125 | + if (!client.connect(REMOTE_IP, REMOTE_PORT)) { |
| 126 | + Serial.printf("Failed to connect!\n"); |
| 127 | + delay(1000); // to not flood logs |
| 128 | + } else { |
| 129 | + state = 1; |
| 130 | + } |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + case 1: |
| 135 | + { |
| 136 | + Serial.printf("Still connecting...\n"); |
| 137 | + delay(500); // to not flood logs |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + case 2: |
| 142 | + { |
| 143 | + // fill PCB space until we can |
| 144 | + size_t willSend; |
| 145 | + while (bufferPos < BUFFER_SIZE && (willSend = client.write(buffer + bufferPos, BUFFER_SIZE - bufferPos))) { |
| 146 | + Serial.printf("Will send %u bytes...\n", willSend); |
| 147 | + bufferPos += willSend; |
| 148 | + waitingAck += willSend; |
| 149 | + } |
| 150 | + |
| 151 | + // we have sent the whole buffer ? |
| 152 | + if (bufferPos >= BUFFER_SIZE) { |
| 153 | + // wait for acks, or send again after 2 sec |
| 154 | + if (waitingAck) { |
| 155 | + Serial.printf("Waiting for acks...\n"); |
| 156 | + delay(100); |
| 157 | + } else { |
| 158 | + Serial.printf("Buffer received - next send in 2 sec\n"); |
| 159 | + delay(2000); |
| 160 | + bufferPos = 0; |
| 161 | + } |
| 162 | + } |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + default: break; |
| 167 | + } |
| 168 | +} |
0 commit comments