Skip to content

Commit 61b798f

Browse files
committed
Update examples and minor tweaks
1 parent 2d6b21e commit 61b798f

File tree

4 files changed

+55
-55
lines changed

4 files changed

+55
-55
lines changed

libraries/WiFiS3/examples/WiFiWebClient/WiFiWebClient.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "arduino_secrets.h"
2828

29-
#define MaximumConnections 1
29+
#define MaximumConnections 2
3030

3131
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
3232
char ssid[] = SECRET_SSID; // your network SSID (name)
@@ -51,7 +51,7 @@ WiFiClient client;
5151
void setup() {
5252
/* -------------------------------------------------------------------------- */
5353
//Initialize serial and wait for port to open:
54-
Serial.begin(115200);
54+
Serial.begin(9600);
5555
while (!Serial) {
5656
; // wait for serial port to connect. Needed for native USB port only
5757
}
@@ -108,7 +108,7 @@ void loop() {
108108

109109
// only allowed to connect n times
110110
if (connectionCount >= MaximumConnections) {
111-
delay(300);
111+
delay(2000);
112112
return;
113113
}
114114

@@ -149,7 +149,7 @@ void loop() {
149149
Serial.println();
150150
Serial.println("disconnecting from server.");
151151
client.stop();
152-
status = WL_CONNECTED;
152+
status = WL_IDLE_STATUS;
153153
}
154154
}
155155
}

libraries/WiFiS3/examples/WiFiWebClientSSL/WiFiWebClientSSL.ino

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#include "arduino_secrets.h"
1616

17-
#define MaximumConnections 1
17+
// maximum number of times the uri will be checked
18+
#define MaximumConnections 2
1819

1920
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
2021
char ssid[] = SECRET_SSID; // your network SSID (name)
@@ -39,7 +40,7 @@ WiFiSSLClient client;
3940
void setup() {
4041
/* -------------------------------------------------------------------------- */
4142
//Initialize serial and wait for port to open:
42-
Serial.begin(115200);
43+
Serial.begin(9600);
4344
while (!Serial) {
4445
; // wait for serial port to connect. Needed for native USB port only
4546
}
@@ -57,7 +58,7 @@ void setup() {
5758
}
5859

5960
// 3 second wait for connection
60-
modem.setTimeout(3000);
61+
client.setTimeout(3000);
6162
}
6263

6364
void connectToWifi() {
@@ -91,56 +92,54 @@ void read_response() {
9192
/* -------------------------------------------------------------------------- */
9293
void loop() {
9394
/* -------------------------------------------------------------------------- */
94-
// do some processing
95-
Serial.println("loop processing");
96-
97-
// only allowed to connect n times
98-
if (connectionCount >= MaximumConnections) {
99-
delay(300);
100-
return;
101-
}
95+
Serial.println("loop processing");
96+
97+
// only allowed to connect n times
98+
if (connectionCount >= MaximumConnections) {
99+
delay(2000);
100+
return;
101+
}
102102

103-
//connect and wait for connection to be made
104-
connectToWifi();
105-
status = WiFi.isConnected();
103+
// Connect to WiFi if not already connected
104+
connectToWifi();
105+
status = WiFi.isConnected();
106106

107-
if (status == WL_CONNECTING) {
108-
Serial.println("Connecting to wifi");
109-
delay(200);
110-
}
107+
if (status == WL_CONNECTING) {
108+
Serial.println("Connecting to wifi");
109+
delay(200);
110+
return;
111+
}
111112

112-
// If connected to Wifi then send a request to a server
113-
if (status == WL_CONNECTED) {
114-
Serial.println("Connected to WiFi");
115-
printWifiStatus();
116-
117-
Serial.println("\nStarting connection to server...");
118-
clientConnected = client.connect(server, 443);
119-
120-
if (clientConnected) {
121-
connectionCount++;
122-
123-
// if you get a connection, report back via serial:
124-
Serial.println("connected to server");
125-
// Make a HTTP request:
126-
client.println("GET /search?q=arduino HTTP/1.1");
127-
client.println("Host: www.google.com");
128-
client.println("Connection: close");
129-
client.println();
130-
131-
Serial.println("Reading response");
132-
read_response();
133-
134-
if (clientConnected) {
135-
// if the server's disconnected, stop the client:
136-
if (!client.connected()) {
137-
Serial.println();
138-
Serial.println("disconnecting from server.");
139-
client.stop();
113+
// If connected to Wifi then send a request to a server
114+
if (status == WL_CONNECTED) {
115+
Serial.println("Connected to WiFi");
116+
printWifiStatus();
117+
118+
Serial.println("\nStarting connection to server...");
119+
120+
if (client.connect(server, 443)) {
121+
connectionCount++;
122+
123+
Serial.println("connected to server");
124+
125+
// Make HTTP request
126+
client.println("GET /search?q=arduino HTTP/1.1");
127+
client.println("Host: www.google.com");
128+
client.println("Connection: close");
129+
client.println();
130+
131+
Serial.println("Reading response");
132+
read_response();
133+
134+
Serial.println("disconnecting from server.");
135+
client.stop();
136+
137+
// Reset status so we don't immediately reconnect
138+
status = WL_IDLE_STATUS;
139+
} else {
140+
Serial.println("Connection to server failed!");
140141
}
141-
}
142142
}
143-
}
144143
}
145144

146145
/* -------------------------------------------------------------------------- */

libraries/WiFiS3/src/WiFi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ int CWifi::begin(const char* ssid, const char *passphrase) {
6363
return WL_CONNECTING;
6464
}
6565

66-
66+
/* -------------------------------------------------------------------------- */
6767
int CWifi::isConnected()
68+
/* -------------------------------------------------------------------------- */
6869
{
6970
uint8_t current_status = status();
7071

libraries/WiFiS3/src/WiFi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CWifi {
6363
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
6464
void _sortAPlist(uint8_t num);
6565
unsigned long _timeout;
66-
unsigned long _start_connection_time;
66+
unsigned long _start_connection_time = millis();
6767
CAccessPoint access_points[WIFI_MAX_SSID_COUNT];
6868
uint8_t _apsFound = 0;
6969
std::string ssid;
@@ -449,7 +449,7 @@ class CWifi {
449449

450450
/**
451451
* @brief Sets the timeout value for the WiFi connection.
452-
*
452+
*
453453
* @param `timeout` The timeout value in milliseconds.
454454
*/
455455
void setTimeout(unsigned long timeout);

0 commit comments

Comments
 (0)