2626
2727#include " arduino_secrets.h"
2828
29+ #define MaximumConnections 1
30+
2931// /////please enter your sensitive data in the Secret tab/arduino_secrets.h
3032char ssid[] = SECRET_SSID; // your network SSID (name)
3133char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
3234int keyIndex = 0 ; // your network key index number (needed only for WEP)
3335
36+ int connectionCount = 0 ;
37+ bool clientConnected = false ;
3438int status = WL_IDLE_STATUS;
39+
3540// if you don't want to use DNS (and reduce your sketch size)
3641// use the numeric IP instead of the name for the server:
3742// IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
@@ -46,7 +51,7 @@ WiFiClient client;
4651void setup () {
4752/* -------------------------------------------------------------------------- */
4853 // Initialize serial and wait for port to open:
49- Serial.begin (9600 );
54+ Serial.begin (115200 );
5055 while (!Serial) {
5156 ; // wait for serial port to connect. Needed for native USB port only
5257 }
@@ -62,30 +67,19 @@ void setup() {
6267 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
6368 Serial.println (" Please upgrade the firmware" );
6469 }
65-
66- // attempt to connect to WiFi network:
67- while (status != WL_CONNECTED) {
68- Serial.print (" Attempting to connect to SSID: " );
69- Serial.println (ssid);
70- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
71- status = WiFi.begin (ssid, pass);
72-
73- // wait 10 seconds for connection:
74- delay (10000 );
75- }
76-
77- printWifiStatus ();
78-
79- Serial.println (" \n Starting connection to server..." );
80- // if you get a connection, report back via serial:
81- if (client.connect (server, 80 )) {
82- Serial.println (" connected to server" );
83- // Make a HTTP request:
84- client.println (" GET /search?q=arduino HTTP/1.1" );
85- client.println (" Host: www.google.com" );
86- client.println (" Connection: close" );
87- client.println ();
88- }
70+
71+ // 3 second wait for connection
72+ client.setConnectionTimeout (3000 );
73+ }
74+
75+ void connectToWifi () {
76+ if (status != WL_IDLE_STATUS)
77+ return ;
78+
79+ Serial.print (" Attempting to connect to SSID: " );
80+ Serial.println (ssid);
81+ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
82+ status = WiFi.begin (ssid, pass);
8983}
9084
9185/* just wrap the received data up to 80 columns in the serial print*/
@@ -109,16 +103,55 @@ void read_response() {
109103/* -------------------------------------------------------------------------- */
110104void loop () {
111105/* -------------------------------------------------------------------------- */
112- read_response ();
106+ // do some processing
107+ Serial.println (" loop processing" );
108+
109+ // only allowed to connect n times
110+ if (connectionCount >= MaximumConnections) {
111+ delay (300 );
112+ return ;
113+ }
113114
114- // if the server's disconnected, stop the client:
115- if (!client.connected ()) {
116- Serial.println ();
117- Serial.println (" disconnecting from server." );
118- client.stop ();
115+ // connect and wait for connection to be made
116+ connectToWifi ();
117+ status = WiFi.isConnected ();
119118
120- // do nothing forevermore:
121- while (true );
119+ if (status == WL_CONNECTING) {
120+ Serial.println (" Connecting to wifi" );
121+ delay (200 );
122+ }
123+
124+ // If connected to Wifi then send a request to a server
125+ if (status == WL_CONNECTED) {
126+ Serial.println (" Connected to WiFi" );
127+ printWifiStatus ();
128+
129+ Serial.println (" \n Starting connection to server..." );
130+ clientConnected = client.connect (server, 80 );
131+
132+ if (clientConnected) {
133+ connectionCount++;
134+
135+ // if you get a connection, report back via serial:
136+ Serial.println (" connected to server" );
137+ // Make a HTTP request:
138+ client.println (" GET /search?q=arduino HTTP/1.1" );
139+ client.println (" Host: www.google.com" );
140+ client.println (" Connection: close" );
141+ client.println ();
142+
143+ Serial.println (" Reading response" );
144+ read_response ();
145+
146+ if (clientConnected) {
147+ // if the server's disconnected, stop the client:
148+ if (!client.connected ()) {
149+ Serial.println ();
150+ Serial.println (" disconnecting from server." );
151+ client.stop ();
152+ }
153+ }
154+ }
122155 }
123156}
124157
0 commit comments