26
26
27
27
#include " arduino_secrets.h"
28
28
29
+ #define MaximumConnections 1
30
+
29
31
// /////please enter your sensitive data in the Secret tab/arduino_secrets.h
30
32
char ssid[] = SECRET_SSID; // your network SSID (name)
31
33
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
32
34
int keyIndex = 0 ; // your network key index number (needed only for WEP)
33
35
36
+ int connectionCount = 0 ;
37
+ bool clientConnected = false ;
34
38
int status = WL_IDLE_STATUS;
39
+
35
40
// if you don't want to use DNS (and reduce your sketch size)
36
41
// use the numeric IP instead of the name for the server:
37
42
// IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
@@ -46,7 +51,7 @@ WiFiClient client;
46
51
void setup () {
47
52
/* -------------------------------------------------------------------------- */
48
53
// Initialize serial and wait for port to open:
49
- Serial.begin (9600 );
54
+ Serial.begin (115200 );
50
55
while (!Serial) {
51
56
; // wait for serial port to connect. Needed for native USB port only
52
57
}
@@ -62,30 +67,19 @@ void setup() {
62
67
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
63
68
Serial.println (" Please upgrade the firmware" );
64
69
}
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);
89
83
}
90
84
91
85
/* just wrap the received data up to 80 columns in the serial print*/
@@ -109,16 +103,55 @@ void read_response() {
109
103
/* -------------------------------------------------------------------------- */
110
104
void loop () {
111
105
/* -------------------------------------------------------------------------- */
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
+ }
113
114
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 ();
119
118
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
+ }
122
155
}
123
156
}
124
157
0 commit comments