|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncTCPClient.ino |
| 3 | +
|
| 4 | + For RP2040W with CYW43439 WiFi |
| 5 | +
|
| 6 | + AsyncTCP_RP2040W is a library for the RP2040W with CYW43439 WiFi |
| 7 | +
|
| 8 | + Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) |
| 9 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncTCP_RP2040W |
| 10 | + Licensed under GPLv3 license |
| 11 | + *****************************************************************************************************************************/ |
| 12 | + |
| 13 | +#include <AsyncTCP_RP2040W.h> |
| 14 | + |
| 15 | +char ssid[] = "your_ssid"; // your network SSID (name) |
| 16 | +char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+ |
| 17 | + |
| 18 | +int status = WL_IDLE_STATUS; |
| 19 | + |
| 20 | +// Change the Server IPAddress accordingly |
| 21 | +IPAddress serverIP(192, 168, 2, 128); |
| 22 | + |
| 23 | +#define TCP_PORT 5698 |
| 24 | + |
| 25 | +#define CHECK_INTERVAL_MS 1000L // Check connection |
| 26 | +#define SEND_INTERVAL_MS 10000L // delay between updates, in milliseconds |
| 27 | + |
| 28 | +unsigned long lastCheck = SEND_INTERVAL_MS; // last time you connected to the server, in milliseconds |
| 29 | + |
| 30 | +AsyncClient* client = nullptr; |
| 31 | + |
| 32 | +bool clientConnected = false; |
| 33 | + |
| 34 | +bool dataReceived = false; |
| 35 | + |
| 36 | +#define REPLY_SIZE 64 |
| 37 | + |
| 38 | +static void replyToServer(void* arg) |
| 39 | +{ |
| 40 | + (void) arg; |
| 41 | + |
| 42 | + Serial.println("\n********************"); |
| 43 | + Serial.println("New replyToServer"); |
| 44 | + |
| 45 | + AsyncClient* client = reinterpret_cast<AsyncClient*>(arg); |
| 46 | + |
| 47 | + // send reply |
| 48 | + if (client->space() > REPLY_SIZE && client->canSend()) |
| 49 | + { |
| 50 | + char message[REPLY_SIZE]; |
| 51 | + sprintf(message, "This is from AsyncTCPClient @ %s", WiFi.localIP().toString().c_str()); |
| 52 | + client->add(message, strlen(message)); |
| 53 | + client->send(); |
| 54 | + |
| 55 | + dataReceived = false; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/* event callbacks */ |
| 60 | +static void handleData(void* arg, AsyncClient* client, void *data, size_t len) |
| 61 | +{ |
| 62 | + (void) arg; |
| 63 | + |
| 64 | + Serial.printf("\nData received from %s \n", client->remoteIP().toString().c_str()); |
| 65 | + Serial.write((uint8_t*)data, len); |
| 66 | + |
| 67 | + lastCheck = millis(); |
| 68 | + |
| 69 | + dataReceived = true; |
| 70 | +} |
| 71 | + |
| 72 | +void onConnect(void* arg, AsyncClient* client) |
| 73 | +{ |
| 74 | + (void) arg; |
| 75 | + |
| 76 | + clientConnected = true; |
| 77 | + |
| 78 | + Serial.printf("\nAsyncTCPClient has been connected to Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT); |
| 79 | + |
| 80 | + replyToServer(client); |
| 81 | +} |
| 82 | + |
| 83 | +void onDisconnect(void* arg, AsyncClient* client) |
| 84 | +{ |
| 85 | + (void) arg; |
| 86 | + (void) client; |
| 87 | + |
| 88 | + Serial.printf("\nAsyncTCPClient has been disconnected from Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT); |
| 89 | + |
| 90 | + clientConnected = false; |
| 91 | +} |
| 92 | + |
| 93 | +void printWifiStatus() |
| 94 | +{ |
| 95 | + // print the SSID of the network you're attached to: |
| 96 | + Serial.print("SSID: "); |
| 97 | + Serial.println(WiFi.SSID()); |
| 98 | + |
| 99 | + // print your board's IP address: |
| 100 | + IPAddress ip = WiFi.localIP(); |
| 101 | + Serial.print("Local IP Address: "); |
| 102 | + Serial.println(ip); |
| 103 | +} |
| 104 | + |
| 105 | +bool connectServer() |
| 106 | +{ |
| 107 | + if (client) |
| 108 | + delete(client); |
| 109 | + |
| 110 | + client = new AsyncClient; |
| 111 | + |
| 112 | + if (client) |
| 113 | + { |
| 114 | + client->onData(&handleData, client); |
| 115 | + client->onConnect(&onConnect, client); |
| 116 | + |
| 117 | + client->onDisconnect(&onDisconnect, client); |
| 118 | + |
| 119 | + client->connect(serverIP, TCP_PORT); |
| 120 | + |
| 121 | + return true; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + Serial.println("\nError, NULL client"); |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void setup() |
| 132 | +{ |
| 133 | + Serial.begin(115200); |
| 134 | + |
| 135 | + while (!Serial && millis() < 5000); |
| 136 | + |
| 137 | + delay(200); |
| 138 | + |
| 139 | + Serial.print("\nStart AsyncTCP_Client on "); |
| 140 | + Serial.print(BOARD_NAME); |
| 141 | + Serial.print(" with "); |
| 142 | + Serial.println(SHIELD_TYPE); |
| 143 | + Serial.println(ASYNCTCP_RP2040W_VERSION); |
| 144 | + |
| 145 | + /////////////////////////////////// |
| 146 | + |
| 147 | + // check for the WiFi module: |
| 148 | + if (WiFi.status() == WL_NO_MODULE) |
| 149 | + { |
| 150 | + Serial.println("Communication with WiFi module failed!"); |
| 151 | + |
| 152 | + // don't continue |
| 153 | + while (true); |
| 154 | + } |
| 155 | + |
| 156 | + Serial.print(F("Connecting to SSID: ")); |
| 157 | + Serial.println(ssid); |
| 158 | + |
| 159 | + status = WiFi.begin(ssid, pass); |
| 160 | + |
| 161 | + delay(1000); |
| 162 | + |
| 163 | + // attempt to connect to WiFi network |
| 164 | + while ( status != WL_CONNECTED) |
| 165 | + { |
| 166 | + delay(500); |
| 167 | + |
| 168 | + // Connect to WPA/WPA2 network |
| 169 | + status = WiFi.status(); |
| 170 | + } |
| 171 | + |
| 172 | + printWifiStatus(); |
| 173 | + |
| 174 | + /////////////////////////////////// |
| 175 | + |
| 176 | + connectServer(); |
| 177 | + |
| 178 | + lastCheck = millis(); |
| 179 | +} |
| 180 | + |
| 181 | +void loop() |
| 182 | +{ |
| 183 | + static unsigned long lastConnectCheck = CHECK_INTERVAL_MS; |
| 184 | + |
| 185 | + if (millis() - lastCheck > SEND_INTERVAL_MS) |
| 186 | + { |
| 187 | + if (clientConnected && dataReceived) |
| 188 | + { |
| 189 | + replyToServer(client); |
| 190 | + } |
| 191 | + else if ( !clientConnected || !dataReceived ) |
| 192 | + { |
| 193 | + Serial.printf("\nReconnecting to Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT); |
| 194 | + |
| 195 | + connectServer(); |
| 196 | + } |
| 197 | + |
| 198 | + lastCheck = millis(); |
| 199 | + } |
| 200 | +} |
0 commit comments