source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/WiFi/examples/WiFiChatServer/WiFiChatServer.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 2.8 KB
Line 
1/*
2 Chat  Server
3
4 A simple server that distributes any incoming messages to all
5 connected clients.  To use telnet to  your device's IP address and type.
6 You can see the client's input in the serial monitor as well.
7
8 This example is written for a network using WPA encryption. For
9 WEP or WPA, change the Wifi.begin() call accordingly.
10
11
12 Circuit:
13 * WiFi shield attached
14
15 created 18 Dec 2009
16 by David A. Mellis
17 modified 31 May 2012
18 by Tom Igoe
19
20 */
21
22#include <SPI.h>
23#include <WiFi.h>
24
25char ssid[] = "yourNetwork"; //  your network SSID (name)
26char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)
27
28int keyIndex = 0;            // your network key Index number (needed only for WEP)
29
30int status = WL_IDLE_STATUS;
31
32WiFiServer server(23);
33
34boolean alreadyConnected = false; // whether or not the client was connected previously
35
36void setup() {
37  //Initialize serial and wait for port to open:
38  Serial.begin(9600);
39  while (!Serial) {
40    ; // wait for serial port to connect. Needed for native USB port only
41  }
42
43  // check for the presence of the shield:
44  if (WiFi.status() == WL_NO_SHIELD) {
45    Serial.println("WiFi shield not present");
46    // don't continue:
47    while (true);
48  }
49
50  String fv = WiFi.firmwareVersion();
51  if (fv != "1.1.0") {
52    Serial.println("Please upgrade the firmware");
53  }
54
55  // attempt to connect to Wifi network:
56  while (status != WL_CONNECTED) {
57    Serial.print("Attempting to connect to SSID: ");
58    Serial.println(ssid);
59    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
60    status = WiFi.begin(ssid, pass);
61
62    // wait 10 seconds for connection:
63    delay(10000);
64  }
65
66  // start the server:
67  server.begin();
68  // you're connected now, so print out the status:
69  printWifiStatus();
70}
71
72
73void loop() {
74  // wait for a new client:
75  WiFiClient client = server.available();
76
77
78  // when the client sends the first byte, say hello:
79  if (client) {
80    if (!alreadyConnected) {
81      // clead out the input buffer:
82      client.flush();
83      Serial.println("We have a new client");
84      client.println("Hello, client!");
85      alreadyConnected = true;
86    }
87
88    if (client.available() > 0) {
89      // read the bytes incoming from the client:
90      char thisChar = client.read();
91      // echo the bytes back to the client:
92      server.write(thisChar);
93      // echo the bytes to the server as well:
94      Serial.write(thisChar);
95    }
96  }
97}
98
99
100void printWifiStatus() {
101  // print the SSID of the network you're attached to:
102  Serial.print("SSID: ");
103  Serial.println(WiFi.SSID());
104
105  // print your WiFi shield's IP address:
106  IPAddress ip = WiFi.localIP();
107  Serial.print("IP Address: ");
108  Serial.println(ip);
109
110  // print the received signal strength:
111  long rssi = WiFi.RSSI();
112  Serial.print("signal strength (RSSI):");
113  Serial.print(rssi);
114  Serial.println(" dBm");
115}
116
117
Note: See TracBrowser for help on using the repository browser.