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

Commit f6ed4d3

Browse files
authored
v1.2.0 to add complex examples
### 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`
1 parent b67b965 commit f6ed4d3

22 files changed

+1413
-1109
lines changed

CONTRIBUTING.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ However, before reporting a bug please check through the following:
1010

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

13+
---
14+
1315
### How to submit a bug report
1416

1517
Please ensure to specify the following:
1618

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

2628

29+
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.
30+
31+
---
32+
2733
### Example
2834

2935
```
3036
Arduino IDE version: 1.8.19
31-
RP2040 core v2.5.4
37+
RP2040 core v2.7.1
3238
RASPBERRY_PI_PICO_W Module
3339
OS: Ubuntu 20.04 LTS
34-
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
40+
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
3541
3642
Context:
37-
I encountered a crash while using Async_AdvancedWebServer
43+
I encountered a crash while using this library
3844
3945
Steps to reproduce:
4046
1. ...

changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
## Table of Contents
1616

1717
* [Changelog](#changelog)
18+
* [Releases v1.2.0](#Releases-v120)
1819
* [Releases v1.1.0](#Releases-v110)
1920
* [Initial Releases v1.0.0](#Initial-Releases-v100)
2021

@@ -23,6 +24,11 @@
2324

2425
## Changelog
2526

27+
### Releases v1.2.0
28+
29+
1. Add complex auto-reconnect `AsyncTCPClient` and `AsyncTCP_Server` examples
30+
2. Improve `README.md` so that links can be used in other sites, such as `PIO`
31+
2632
### Releases v1.1.0
2733

2834
1. Fix issue with slow browsers or network
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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

Comments
 (0)