14
14
15
15
created 18 Dec 2009
16
16
by David A. Mellis
17
- modified 4 Mar 2012
17
+ modified 12 Mar 2012
18
18
by Tom Igoe
19
19
20
20
*/
21
21
22
-
23
22
#include < SPI.h>
24
23
#include < WiFi.h>
25
24
26
25
char ssid[] = " YourNetwork" ; // your network SSID (name)
27
26
char pass[] = " password" ; // your network password (use for WPA, or use as key for WEP)
27
+
28
28
int keyIndex = 0 ; // your network key Index number (needed only for WEP)
29
29
30
30
int status = WL_IDLE_STATUS;
31
31
32
32
WiFiServer server (23 );
33
33
34
- boolean gotAMessage = false ; // whether or not you got a message from the client yet
34
+ boolean alreadyConnected = false ; // whether or not the client was connected previously
35
35
36
36
void setup () {
37
37
// initialize serial:
@@ -55,20 +55,25 @@ void loop() {
55
55
// wait for a new client:
56
56
WiFiClient client = server.available ();
57
57
58
+
58
59
// when the client sends the first byte, say hello:
59
60
if (client) {
60
- if (!gotAMessage) {
61
+ if (!alreadyConnected) {
62
+ // clead out the input buffer:
63
+ client.flush ();
61
64
Serial.println (" We have a new client" );
62
65
client.println (" Hello, client!" );
63
- gotAMessage = true ;
64
- }
66
+ alreadyConnected = true ;
67
+ }
65
68
66
- // read the bytes incoming from the client:
67
- char thisChar = client.read ();
68
- // echo the bytes back to the client:
69
- server.write (thisChar);
70
- // echo the bytes to the server as well:
71
- Serial.print (thisChar);
69
+ if (client.available () > 0 ) {
70
+ // read the bytes incoming from the client:
71
+ char thisChar = client.read ();
72
+ // echo the bytes back to the client:
73
+ server.write (thisChar);
74
+ // echo the bytes to the server as well:
75
+ Serial.write (thisChar);
76
+ }
72
77
}
73
78
}
74
79
0 commit comments