Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Asynchronous TCP Library for RASPBERRY_PI_PICO_W using CYW43439 WiFi with arduino-pico core. This library is the base for future and more advanced Async libraries, such as AsyncWebServer_RP2040W, AsyncHTTPRequest_RP2040W, AsyncHTTPSRequest_RP2040W

License

khoih-prog/AsyncTCP_RP2040W

Repository files navigation

AsyncTCP_RP2040W Library

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of contents



Why do we need this AsyncTCP_RP2040W library

Features

This library is based on, modified from:

  1. Hristo Gochkov's ESPAsyncTCP

to apply the better and faster asynchronous feature of the powerful ESPAsyncTCP Library into RASPBERRY_PI_PICO_W with CYW43439 WiFi, and will be the base for future and more advanced Async libraries, such as AsyncWebServer_RP2040W, AsyncHTTPRequest_RP2040W, AsyncHTTPSRequest_RP2040W

Why Async is better

  • Using asynchronous network means that you can handle more than one connection at the same time
  • You are called once the request is ready and parsed
  • When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background
  • Speed is OMG
  • Easy to use API, HTTP Basic and Digest MD5 Authentication (default), ChunkedResponse
  • Easily extensible to handle any type of content
  • Supports Continue 100
  • Async WebSocket plugin offering different locations without extra servers or ports
  • Async EventSource (Server-Sent Events) plugin to send events to the browser
  • URL Rewrite plugin for conditional and permanent url rewrites
  • ServeStatic plugin that supports cache, Last-Modified, default index and more
  • Simple template processing engine to handle templates

Currently supported Boards

  1. RASPBERRY_PI_PICO_W with CYW43439 WiFi using arduino-pico core


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Earle Philhower's arduino-pico core v2.7.1+ for RASPBERRY_PI_PICO_W with CYW43439 WiFi, etc. GitHub release


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for AsyncTCP_RP2040W, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to AsyncTCP_RP2040W page.
  2. Download the latest release AsyncTCP_RP2040W-main.zip.
  3. Extract the zip file to AsyncTCP_RP2040W-main directory
  4. Copy whole AsyncTCP_RP2040W-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncTCP_RP2040W library by using Library Manager. Search for AsyncTCP_RP2040W in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Original documentation

Check AsyncTCP Library

AsyncClient and AsyncServer

The base classes on which everything else is built. They expose all possible scenarios, but are really raw and require more skills to use.

AsyncPrinter

This class can be used to send data like any other Print interface (Serial for example).

The object then can be used outside of the Async callbacks (the loop) and receive asynchronously data using onData. The object can be checked if the underlying AsyncClientis connected, or hook to the onDisconnect callback.

AsyncTCPbuffer

This class is really similar to the AsyncPrinter, but it can buffer some of the incoming data.

SyncClient

It is exactly what it sounds like. This is a standard, synchronous blocking TCP Client you're used to.



Examples

  1. AsyncTCP_Client
  2. AsyncTCP_Server

Please take a look at other examples, as well.

#include <AsyncTCP_RP2040W.h>
char ssid[] = "your_ssid"; // your network SSID (name)
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+
int status = WL_IDLE_STATUS;
// Change the Server IPAddress accordingly
IPAddress serverIP(192, 168, 2, 128);
#define TCP_PORT 5698
#define CHECK_INTERVAL_MS 1000L // Check connection
#define SEND_INTERVAL_MS 10000L // delay between updates, in milliseconds
unsigned long lastCheck = SEND_INTERVAL_MS; // last time you connected to the server, in milliseconds
AsyncClient* client = nullptr;
bool clientConnected = false;
bool dataReceived = false;
#define REPLY_SIZE 64
static void replyToServer(void* arg)
{
(void) arg;
Serial.println("\n********************");
Serial.println("New replyToServer");
AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);
// send reply
if (client->space() > REPLY_SIZE && client->canSend())
{
char message[REPLY_SIZE];
sprintf(message, "This is from AsyncTCPClient @ %s", WiFi.localIP().toString().c_str());
client->add(message, strlen(message));
client->send();
dataReceived = false;
}
}
/* event callbacks */
static void handleData(void* arg, AsyncClient* client, void *data, size_t len)
{
(void) arg;
Serial.printf("\nData received from %s \n", client->remoteIP().toString().c_str());
Serial.write((uint8_t*)data, len);
lastCheck = millis();
dataReceived = true;
}
void onConnect(void* arg, AsyncClient* client)
{
(void) arg;
clientConnected = true;
Serial.printf("\nAsyncTCPClient has been connected to Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT);
replyToServer(client);
}
void onDisconnect(void* arg, AsyncClient* client)
{
(void) arg;
(void) client;
Serial.printf("\nAsyncTCPClient has been disconnected from Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT);
clientConnected = false;
}
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("Local IP Address: ");
Serial.println(ip);
}
bool connectServer()
{
if (client)
delete(client);
client = new AsyncClient;
if (client)
{
client->onData(&handleData, client);
client->onConnect(&onConnect, client);
client->onDisconnect(&onDisconnect, client);
client->connect(serverIP, TCP_PORT);
return true;
}
else
{
Serial.println("\nError, NULL client");
return false;
}
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print("\nStart AsyncTCP_Client on ");
Serial.print(BOARD_NAME);
Serial.print(" with ");
Serial.println(SHIELD_TYPE);
Serial.println(ASYNCTCP_RP2040W_VERSION);
///////////////////////////////////
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(1000);
// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);
// Connect to WPA/WPA2 network
status = WiFi.status();
}
printWifiStatus();
///////////////////////////////////
connectServer();
lastCheck = millis();
}
void loop()
{
static unsigned long lastConnectCheck = CHECK_INTERVAL_MS;
if (millis() - lastCheck > SEND_INTERVAL_MS)
{
if (clientConnected && dataReceived)
{
replyToServer(client);
}
else if ( !clientConnected || !dataReceived )
{
Serial.printf("\nReconnecting to Server %s, port %d \n", serverIP.toString().c_str(), TCP_PORT);
connectServer();
}
lastCheck = millis();
}
}



Debug Terminal Output Samples

1. AsyncTCP_Server on RASPBERRY_PI_PICO_W with CYW43439 WiFi

Start AsyncTCP_Server on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
AsyncTCP_RP2040W v1.2.0
Connecting to SSID: HueNet
SSID: HueNet
Local IP Address: 192.168.2.128
AsyncTCPServer is @ IP: 192.168.2.128, port: 5698

New client has been connected to server, IP: 192.168.2.118
Data received from client 192.168.2.118 
This is from AsyncTCPClient @ 192.168.2.118

2. AsyncTCP_Client on RASPBERRY_PI_PICO_W with CYW43439 WiFi

Following is debug terminal output when running example AsyncTCP_Client on RASPBERRY_PI_PICO_W using CYW43439 WiFi, to demo the AsyncTCP_Client auto-reconnects to AsyncTCP_Server if connection is lost (network, power-recycle, etc.)

Start AsyncTCP_Client on RASPBERRY_PI_PICO_W with RP2040W CYW43439 WiFi
AsyncTCP_RP2040W v1.2.0
Connecting to SSID: HueNet
SSID: HueNet
Local IP Address: 192.168.2.118

AsyncTCPClient has been connected to Server 192.168.2.128, port 5698 

********************
New replyToServer

Data received from 192.168.2.128 
You've connected to AsyncTCPServer @ 192.168.2.128
********************
New replyToServer

Data received from 192.168.2.128 
You've connected to AsyncTCPServer @ 192.168.2.128
********************
New replyToServer

Data received from 192.168.2.128 
You've connected to AsyncTCPServer @ 192.168.2.128
********************
New replyToServer

Reconnecting to Server 192.168.2.128, port 5698 

AsyncTCPClient has been disconnected from Server 192.168.2.128, port 5698 

Reconnecting to Server 192.168.2.128, port 5698 

Reconnecting to Server 192.168.2.128, port 5698 

Reconnecting to Server 192.168.2.128, port 5698 

AsyncTCPClient has been connected to Server 192.168.2.128, port 5698 

********************
New replyToServer

Data received from 192.168.2.128 
You've connected to AsyncTCPServer @ 192.168.2.128
********************
New replyToServer

Data received from 192.168.2.128 
You've connected to AsyncTCPServer @ 192.168.2.128


Libraries currently depend on this library

  1. AsyncWebServer_RP2040W GitHub release
  2. AsyncHTTPRequest_RP2040W GitHub release


Debug

Debug is enabled by default on Serial.

You can also change the debugging level _ASYNCTCP_RP2040W_LOGLEVEL_ from 0 to 4 in the library cpp files

#define _ASYNCTCP_RP2040W_LOGLEVEL_     1

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: AsyncTCP_RP2040W issues


TO DO

  1. Search for bug and improvement.

DONE

  1. RASPBERRY_PI_PICO_W with CYW43439 WiFi
  2. Add astyle using allman style. Restyle the library
  3. Add complex auto-reconnect AsyncTCPClient and AsyncTCP_Server examples
  4. Improve README.md so that links can be used in other sites, such as PIO


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.


Contributions and Thanks

  1. Based on and modified from Hristo Gochkov's ESPAsyncTCP. Many thanks to Hristo Gochkov for great ESPAsyncTCP Library
me-no-dev
⭐️⭐️ Hristo Gochkov


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library


License

  • The library is licensed under GPLv3

Copyright

  • Copyright (c) 2016- Hristo Gochkov
  • Copyright (c) 2022- Khoi Hoang