WiFiChatServer

Description


In this example is shown how to realize a simple Chat Server, using the WiFi Link library on the Arduino STAR OTTO.
This Chat uses a Telnet protocol to exchange the messages. If you are a Windows user then you could use Putty.


Warning:
Use the Arduino IDE 1.8.x  and import the WiFi Link library using the Library Manager.

WiFi Link library


Hardware


Circuit


Connect the board to PC
Star Otto


Code

Now, modify the below sketch inserting the name and the password of your network and after upload the code using Arduino IDE.
This example is written for a WPA encryption network. If you want to use it  for WEP or WPA, change the Wifi.begin() call accordingly.

Note:

If you use the IDE sketch then replace the Serial with SerialUSB.

Warning:

Use the Arduino IDE 1.8.x and check that you have already downloaded the corresponding platform (Arduino STM32F4 Boards) from the Board Manager

/*
Chat Server

A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the SerialUSB monitor as well.

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.


Circuit:
* Arduino Primo or STAR OTTO or Uno WiFi Developer Edition (with WiFi Link firmware running)

created 18 Dec 2009
by David A. Mellis
modified 31 May 2012
by Tom Igoe
modified 10 March 2017
by Sergio Tomasello and Andrea Cannistrá
*/


#include <WiFiLink.h>

char ssid[] = ""; // your network SSID (name)
char pass[] = ""; // 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 status = WL_IDLE_STATUS;

WiFiServer server(23);

boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
//Initialize SerialUSB and wait for port to open:
SerialUSB.begin(9600);
while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } //Check if communication with wifi module has been established
if (WiFi.status() == WL_NO_WIFI_MODULE_COMM) {
SerialUSB.println("Communication with WiFi module not established.");
while (true); // don't continue:
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
SerialUSB.print("Attempting to connect to SSID: ");
SerialUSB.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// start the server:
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}


void loop() {
// wait for a new client:
WiFiClient client = server.available();


// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
SerialUSB.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}

if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
SerialUSB.write(thisChar);
}
}
}


void printWifiStatus() {
// print the SSID of the network you're attached to:
SerialUSB.print("SSID: ");
SerialUSB.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
SerialUSB.print("IP Address: ");
SerialUSB.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
SerialUSB.print("signal strength (RSSI):");
SerialUSB.print(rssi);
SerialUSB.println(" dBm");
}

Output

Open the Serial Monitor, check the ip address and use it to open a Telenet chat:

Open Telnet


Type the correct ip address and enter in the chat:

Opening chat


Now, write on the chat and good fun:

Chat

/*    This example connects to an unencrypted Wifi network.  Then it prints the  MAC address of the board,  the IP address obtained, and other network details.    Circuit:  * Arduino Primo or STAR OTTO or Uno WiFi Developer Edition (with WiFi Link firmware running)    created 13 July 2010  by dlf (Metodo2 srl)  modified 31 May 2012  by Tom Igoe  modified 10 March 2017  by Sergio Tomasello and Andrea Cannistrá  */   #include <WiFi.h>   char ssid[] = "yourNetwork";     // the name of your network int status = WL_IDLE_STATUS;     // the Wifi radio's status   void setup() {   //Initialize SerialUSB and wait for port to open:   SerialUSB.begin(9600);    while (!Serial) {     ; // wait for serial port to connect. Needed for native USB port only   }     //Check if communication with the wifi module has been established   if (WiFi.status() == WL_NO_WIFI_MODULE_COMM) {     SerialUSB.println("Communication with WiFi module not established.");     while (true); // don't continue:   }     // attempt to connect to Wifi network:   while ( status != WL_CONNECTED) {     SerialUSB.print("Attempting to connect to open SSID: ");     SerialUSB.println(ssid);     status = WiFi.begin(ssid);       // wait 10 seconds for connection:     delay(10000);   }     // you're connected now, so print out the data:   SerialUSB.print("You're connected to the network");   printCurrentNet();   printWifiData(); }   void loop() {   // check the network connection once every 10 seconds:   delay(10000);   printCurrentNet(); }   void printWifiData() {   // print your WiFi shield's IP address:   IPAddress ip = WiFi.localIP();   SerialUSB.print("IP Address: ");   SerialUSB.println(ip);   SerialUSB.println(ip);     // print your MAC address:   byte mac[6];   WiFi.macAddress(mac);   SerialUSB.print("MAC address: ");   SerialUSB.print(mac[5], HEX);   SerialUSB.print(":");   SerialUSB.print(mac[4], HEX);   SerialUSB.print(":");   SerialUSB.print(mac[3], HEX);   SerialUSB.print(":");   SerialUSB.print(mac[2], HEX);   SerialUSB.print(":");   SerialUSB.print(mac[1], HEX);   SerialUSB.print(":");   SerialUSB.println(mac[0], HEX);     // print your subnet mask:   IPAddress subnet = WiFi.subnetMask();   SerialUSB.print("NetMask: ");   SerialUSB.println(subnet);     // print your gateway address:   IPAddress gateway = WiFi.gatewayIP();   SerialUSB.print("Gateway: ");   SerialUSB.println(gateway); }   void printCurrentNet() {   // print the SSID of the network you're attached to:   SerialUSB.print("SSID: ");   SerialUSB.println(WiFi.SSID());     // print the MAC address of the router you're attached to:   byte bssid[6];   WiFi.BSSID(bssid);   SerialUSB.print("BSSID: ");   SerialUSB.print(bssid[5], HEX);   SerialUSB.print(":");   SerialUSB.print(bssid[4], HEX);   SerialUSB.print(":");   SerialUSB.print(bssid[3], HEX);   SerialUSB.print(":");   SerialUSB.print(bssid[2], HEX);   SerialUSB.print(":");   SerialUSB.print(bssid[1], HEX);   SerialUSB.print(":");   SerialUSB.println(bssid[0], HEX);     // print the received signal strength:   long rssi = WiFi.RSSI();   SerialUSB.print("signal strength (RSSI):");   SerialUSB.println(rssi);     // print the encryption type:   byte encryption = WiFi.encryptionType();   SerialUSB.print("Encryption Type:");   SerialUSB.println(encryption, HEX); }