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

Commit

Permalink
v1.2.0 to add complex examples
Browse files Browse the repository at this point in the history
### Releases v1.2.0

1. Add complex auto-reconnect `AsyncTCPClient` and `AsyncTCP_Server` examples
2. Improve `README.md` so that links can be used in other sites, such as `PIO`
  • Loading branch information
khoih-prog committed Feb 2, 2023
1 parent b67b965 commit f6ed4d3
Show file tree
Hide file tree
Showing 22 changed files with 1,413 additions and 1,109 deletions.
14 changes: 10 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ However, before reporting a bug please check through the following:

If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AsyncTCP_RP2040W/issues/new).

---

### How to submit a bug report

Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `RP2040` Core Version (e.g. RP2040 core v2.5.4)
* `RP2040` Core Version (e.g. RP2040 core v2.7.1)
* `RP2040` Board type (e.g. RASPBERRY_PI_PICO_W)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
Expand All @@ -24,17 +26,21 @@ Please ensure to specify the following:
* Network configuration


Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted.

---

### Example

```
Arduino IDE version: 1.8.19
RP2040 core v2.5.4
RP2040 core v2.7.1
RASPBERRY_PI_PICO_W Module
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using Async_AdvancedWebServer
I encountered a crash while using this library
Steps to reproduce:
1. ...
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.2.0](#Releases-v120)
* [Releases v1.1.0](#Releases-v110)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

Expand All @@ -23,6 +24,11 @@

## Changelog

### Releases v1.2.0

1. Add complex auto-reconnect `AsyncTCPClient` and `AsyncTCP_Server` examples
2. Improve `README.md` so that links can be used in other sites, such as `PIO`

### Releases v1.1.0

1. Fix issue with slow browsers or network
Expand Down
200 changes: 200 additions & 0 deletions examples/ClientServer/AsyncTCP_Client/AsyncTCP_Client.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/****************************************************************************************************************************
AsyncTCPClient.ino
For RP2040W with CYW43439 WiFi
AsyncTCP_RP2040W is a library for the RP2040W with CYW43439 WiFi
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncTCP_RP2040W
Licensed under GPLv3 license
*****************************************************************************************************************************/

#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();
}
}
Loading

0 comments on commit f6ed4d3

Please sign in to comment.