1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

Updated WifiChatServer to clear out garbage characters

This commit is contained in:
Tom Igoe
2012-03-12 13:57:42 -04:00
parent bd4a9a6816
commit d09655d478

View File

@ -14,24 +14,24 @@
created 18 Dec 2009 created 18 Dec 2009
by David A. Mellis by David A. Mellis
modified 4 Mar 2012 modified 12 Mar 2012
by Tom Igoe by Tom Igoe
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "YourNetwork"; // your network SSID (name) char ssid[] = "YourNetwork"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
WiFiServer server(23); WiFiServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() { void setup() {
// initialize serial: // initialize serial:
@ -55,20 +55,25 @@ void loop() {
// wait for a new client: // wait for a new client:
WiFiClient client = server.available(); WiFiClient client = server.available();
// when the client sends the first byte, say hello: // when the client sends the first byte, say hello:
if (client) { if (client) {
if (!gotAMessage) { if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
Serial.println("We have a new client"); Serial.println("We have a new client");
client.println("Hello, client!"); client.println("Hello, client!");
gotAMessage = true; alreadyConnected = true;
} }
if (client.available() > 0) {
// read the bytes incoming from the client: // read the bytes incoming from the client:
char thisChar = client.read(); char thisChar = client.read();
// echo the bytes back to the client: // echo the bytes back to the client:
server.write(thisChar); server.write(thisChar);
// echo the bytes to the server as well: // echo the bytes to the server as well:
Serial.print(thisChar); Serial.write(thisChar);
}
} }
} }