It distributes any incoming messages to all connected clients.
The incoming messages are also printed to the serial monitor.
Hardware Required
- Arduino Uno or Mega
- USB Cable for Arduino
- PHPoC Shield (P4S-347) or PHPoC WiFi Shield (P4S-348)
Circuit
- Stack PHPoC Shield or PHPoC WiFi Shield on Arduino Uno or Mega
Note that: Arduino communicates with PHPoC [WiFi] Shield via pins 10, 11, 12 and 13 on the Uno, and pins 10, 50, 51 and 52 on the Mega. Therefore, these pins CANNOT be used for general I/O.
Install Arduino IDE
If you have not install Arduino IDE yet, please download and install Arduino IDE .
Install Library
- Run Arduino IDE.
- Navigate to Sketch > Include Library > Manage Libraries
- Search "Phpoc" on search bar of the Library Manager.
- Select the PHPoC library and press the [Install] button.
- Restart Arduino IDE for the next step.
Setup Network Information
This part is needed only for the first use.
1. If Ethernet is used
In case of using PHPoC Shield (P4S-348), you have two options to connect to network: Ethernet or WiFi.
If using Ethernet, please follow this instruction to connect the shield to Ethernet.
2. If WiFi is used
WiFi is available in both P4S-347 and P4S-348. Please follow:
- This instruction to access the setup page.
- This instruction if you want to connect shield to WLAN router or Access Point (AP)
- This instruction if you want to operate shield as Access Point (AP)
Source Code
- Open "TelnetServer" example on Arduino IDE
Code:// Arduino Telnet Server - 4 Listening Sessions // // PHPoC Shield and PHPoC WiFi Shield are Internet Shields for Arduino Uno and // Mega. // // This is an example of using Arduino Uno/Mega and PHPoC [WiFi] Shield to // create a Telnet server that can connect up to 4 Telnet clients // simultaneously. It distributes any incoming messages to all connected // clients. The incoming messages are also printed to the serial monitor. // // Arduino communicates with PHPoC [WiFi] Shield via pins 10, 11, 12 and 13 on // the Uno, and pins 10, 50, 51 and 52 on the Mega. Therefore, these pins CANNOT // be used for general I/O. // // This example code was written by Sollae Systems. It is released into the // public domain. // // Tutorial for the example is available here: // https://forum.phpoc.com/articles/tutorials/1237-arduino-telnet-server #include <Phpoc.h> PhpocServer server(23); boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { Serial.begin(9600); while(!Serial) ; // initialize PHPoC [WiFi] Shield: Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); //Phpoc.begin(); // beginTelnet() enables telnet option negotiation & "character at a time". // In "character at a time" mode, text typed is immediately sent to server. server.beginTelnet(); // print IP address of PHPoC [WiFi] Shield to serial monitor: Serial.print("Telnet server address : "); Serial.println(Phpoc.localIP()); } void loop() { // wait for a new client: PhpocClient client = server.available(); // when the client sends the first byte, say hello: if (client) { if (!alreadyConnected) { // clear out the transmission buffer: client.flush(); Serial.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 all connected clients: server.write(thisChar); // echo the bytes to the server as well: Serial.write(thisChar); } } }
- The line-by-line explaination of code is presented inside the code.
For more detail of functions's reference, please refer to PHPoC Shield for Arduino Library Reference - Compile the example code and upload to Arduino by clicking "Upload" button on Arduino IDE
- Open Serial Monitor tool on Arduino IDE to see the output log.
- Copy IP address of PHPoC Shield.
Test and Result
In this example, Arduino acts as Telnet server. It needs to use Telnet client to test. Telnet client can be any kind of Telnet client software/program on PC or smartphone.
In this tutorial, I use a Windows-based software, called Tera Term
- Open four Tera Term windows
- Type IP address of PHPoC Shield and port number (23)
- Click "OK" button.
- Send "Hello 1" message from the 1st Tera Term windows to Arduino.
- Send "Hello 2" message from the 2nd Tera Term windows to Arduino.
- Send "Hello 3" message from the 3rd Tera Term windows to Arduino.
- Send "Hello 4" message from the 4th Tera Term windows to Arduino.
- We can see the echo message in receiving area of each Tera Term.
- See message in serial monitor
See Also
- Arduino - TCP Chat Server
- Arduino - TCP IPv6 Chat Server
- Arduino - Telnet Server
- Arduino - SSH Server
- Arduino - SSL Server
- Arduino - Email Client
- Arduino - Gmail Client
- Arduino - Web Client
- Arduino - IPv6 Web Client
- Arduino - SSL Web Client
- Arduino - SSL IPv6 Web Client
- Arduino - Web Server - Remote Push
- Arduino - Web Server - Remote Slide
- Arduino - Web Server - Remote Pad
- Arduino - Web Serial Plotter
- Arduino - Web Serial Monitor
- Arduino - RTC Date and Time
References