Skip to content

Commit 4aa71f6

Browse files
committed
WiFiClient connect timeout
1 parent 12c33f1 commit 4aa71f6

File tree

5 files changed

+80
-41
lines changed

5 files changed

+80
-41
lines changed

docs/api.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,72 @@ client.remotePort()
26382638
#### Returns
26392639
The port of the remote host that the client is connected to
26402640

2641+
### `client.setConnectionTimeout()`
2642+
2643+
#### Description
2644+
2645+
Set the timeout for client.connect(). With timeout value not set, a connect attempt times out after the time determined by the firmware which is more than 18 seconds. You might prefer to set a lower timeout value to make your program more responsive in the event something goes wrong.
2646+
2647+
2648+
#### Syntax
2649+
2650+
```
2651+
client.setConnectionTimeout(milliseconds)
2652+
2653+
```
2654+
2655+
#### Parameters
2656+
- milliseconds: the timeout duration for client.connect() (uint16_t)
2657+
2658+
#### Returns
2659+
Nothing
2660+
2661+
#### Example
2662+
2663+
```
2664+
#include <WiFiNINA.h>
2665+
2666+
#include "arduino_secrets.h"
2667+
char ssid[] = SECRET_SSID;
2668+
char pass[] = SECRET_PASS;
2669+
2670+
IPAddress server(192,168,0,177);
2671+
WiFiClient client;
2672+
2673+
void setup() {
2674+
2675+
Serial.begin(115200);
2676+
while (!Serial) {}
2677+
2678+
Serial.print("Attempting to connect to SSID: ");
2679+
Serial.println(ssid);
2680+
int status = WiFi.begin(ssid, pass);
2681+
if ( status != WL_CONNECTED) {
2682+
Serial.println("Couldn't get a WiFi connection");
2683+
while(true);
2684+
}
2685+
2686+
client.setConnectionTimeout(3000); // 3 seconds
2687+
2688+
Serial.println("\nStarting connection to server...");
2689+
unsigned long startTime = millis();
2690+
if (client.connect(server, 80)) {
2691+
client.println("Connected");
2692+
} else {
2693+
client.println("Not Connected");
2694+
}
2695+
2696+
Serial.print("connect time (milliseconds): ");
2697+
Serial.println(millis()- startTime);
2698+
2699+
client.stop();
2700+
}
2701+
2702+
void loop() {
2703+
}
2704+
2705+
```
2706+
26412707
## Server Class
26422708

26432709
### `Server()`

src/WiFiClient.cpp

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
5959
_sock = ServerDrv::getSocket();
6060
if (_sock != NO_SOCKET_AVAIL)
6161
{
62-
ServerDrv::startClient(uint32_t(ip), port, _sock);
63-
64-
unsigned long start = millis();
65-
66-
// wait 4 second for the connection to close
67-
while (!connected() && millis() - start < 10000)
68-
delay(1);
62+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TCP_MODE, _connTimeout);
6963

7064
if (!connected())
7165
{
@@ -88,13 +82,7 @@ int WiFiClient::connectSSL(IPAddress ip, uint16_t port)
8882
_sock = ServerDrv::getSocket();
8983
if (_sock != NO_SOCKET_AVAIL)
9084
{
91-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_MODE);
92-
93-
unsigned long start = millis();
94-
95-
// wait 4 second for the connection to close
96-
while (!connected() && millis() - start < 10000)
97-
delay(1);
85+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_MODE, _connTimeout);
9886

9987
if (!connected())
10088
{
@@ -117,13 +105,7 @@ int WiFiClient::connectSSL(const char *host, uint16_t port)
117105
_sock = ServerDrv::getSocket();
118106
if (_sock != NO_SOCKET_AVAIL)
119107
{
120-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE);
121-
122-
unsigned long start = millis();
123-
124-
// wait 4 second for the connection to close
125-
while (!connected() && millis() - start < 10000)
126-
delay(1);
108+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE, _connTimeout);
127109

128110
if (!connected())
129111
{
@@ -146,13 +128,7 @@ int WiFiClient::connectBearSSL(IPAddress ip, uint16_t port)
146128
_sock = ServerDrv::getSocket();
147129
if (_sock != NO_SOCKET_AVAIL)
148130
{
149-
ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_BEARSSL_MODE);
150-
151-
unsigned long start = millis();
152-
153-
// wait 4 second for the connection to close
154-
while (!connected() && millis() - start < 10000)
155-
delay(1);
131+
ServerDrv::startClient(nullptr, 0, uint32_t(ip), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
156132

157133
if (!connected())
158134
{
@@ -175,13 +151,7 @@ int WiFiClient::connectBearSSL(const char *host, uint16_t port)
175151
_sock = ServerDrv::getSocket();
176152
if (_sock != NO_SOCKET_AVAIL)
177153
{
178-
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE);
179-
180-
unsigned long start = millis();
181-
182-
// wait 4 second for the connection to close
183-
while (!connected() && millis() - start < 10000)
184-
delay(1);
154+
ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_BEARSSL_MODE, _connTimeout);
185155

186156
if (!connected())
187157
{

src/WiFiClient.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class WiFiClient : public Client {
3232
WiFiClient(uint8_t sock);
3333

3434
uint8_t status();
35+
void setConnectionTimeout(uint16_t timeout) {_connTimeout = timeout;}
36+
3537
virtual int connect(IPAddress ip, uint16_t port);
3638
virtual int connect(const char *host, uint16_t port);
3739
virtual int connectSSL(IPAddress ip, uint16_t port);
@@ -61,8 +63,8 @@ class WiFiClient : public Client {
6163

6264
private:
6365
static uint16_t _srcport;
64-
uint8_t _sock; //not used
65-
uint16_t _socket;
66+
uint8_t _sock;
67+
uint16_t _connTimeout = 0;
6668
bool _retrySend;
6769
};
6870

src/utility/server_drv.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
110110
SpiDrv::spiSlaveDeselect();
111111
}
112112

113-
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
113+
void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode, uint16_t timeout)
114114
{
115115
WAIT_FOR_SLAVE_SELECT();
116116
// Send Command
117-
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_5);
117+
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_6);
118118
SpiDrv::sendParam((uint8_t*)host, host_len);
119119
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
120120
SpiDrv::sendParam(port);
121121
SpiDrv::sendParam(&sock, 1);
122-
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
122+
SpiDrv::sendParam(&protMode, 1);
123+
SpiDrv::sendParam(timeout, LAST_PARAM);
123124

124125
// pad to multiple of 4
125126
int commandSize = 17 + host_len;

src/utility/server_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ServerDrv
3737

3838
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
3939

40-
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
40+
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE, uint16_t timeout = 0);
4141

4242
static void stopClient(uint8_t sock);
4343

0 commit comments

Comments
 (0)