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
2021char ssid[] = SECRET_SSID; // your network SSID (name)
@@ -39,7 +40,7 @@ WiFiSSLClient client;
3940void 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
6364void connectToWifi () {
@@ -91,56 +92,54 @@ void read_response() {
9192/* -------------------------------------------------------------------------- */
9293void 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 (" \n Starting 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 (" \n Starting 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/* -------------------------------------------------------------------------- */
0 commit comments