Announcement

Collapse
No announcement yet.

Arduino - TCP Chat Server

Collapse
X
Collapse
  •  

  • Arduino - TCP Chat Server

    What is TCP?

    TCP is abbreviation of Transmission Control Protocol. TCP is one of the popular protocols in Internet. To exchange data between two devices using TCP protocol, one device acts as TCP server, the other acts as TCP client. TCP server listens to wait for TCP connection request from TCP client. TCP Client actively make request for TCP connection to TCP server.


    This tutorial show how to use Arduino Uno/Mega and PHPoC [WiFi] Shield to create a TCP server that can connect up to 4 TCP clients simultaneously.
    It distributes any incoming messages to all connected clients.
    The incoming messages are also printed to the serial monitor.


    Hardware Required


    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:

    Source Code
    • Open "ChatServer" example on Arduino IDE
    • 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.

    Code:
    [COLOR=#434F54]// Arduino Chat Server - 4 Listening Sessions[/COLOR]
    [COLOR=#434F54]//[/COLOR]
    [COLOR=#434F54]// PHPoC Shield and PHPoC WiFi Shield are Internet Shields for Arduino Uno and[/COLOR]
    [COLOR=#434F54]// Mega.[/COLOR]
    [COLOR=#434F54]//[/COLOR]
    [COLOR=#434F54]// This is an example of using Arduino Uno/Mega and PHPoC [WiFi] Shield to[/COLOR]
    [COLOR=#434F54]// create a TCP server that can connect up to 4 TCP clients simultaneously. It[/COLOR]
    [COLOR=#434F54]// distributes any incoming messages to all connected clients. The incoming[/COLOR]
    [COLOR=#434F54]// messages are also printed to the serial monitor.[/COLOR]
    [COLOR=#434F54]//[/COLOR]
    [COLOR=#434F54]// Arduino communicates with PHPoC [WiFi] Shield via pins 10, 11, 12 and 13 on[/COLOR]
    [COLOR=#434F54]// the Uno, and pins 10, 50, 51 and 52 on the Mega. Therefore, these pins CANNOT[/COLOR]
    [COLOR=#434F54]// be used for general I/O.[/COLOR]
    [COLOR=#434F54]//[/COLOR]
    [COLOR=#434F54]// This example code was written by Sollae Systems. It is released into the[/COLOR]
    [COLOR=#434F54]// public domain.[/COLOR]
    [COLOR=#434F54]//[/COLOR]
    [COLOR=#434F54]// Tutorial for the example is available here:[/COLOR]
    [COLOR=#434F54]// https://forum.phpoc.com/articles/tutorials/1232-arduino-tcp-chat-server[/COLOR]
    
    #[COLOR=#5E6D03]include[/COLOR] <[B][COLOR=#d35400]Phpoc[/COLOR][/B].h>
    
    [B][COLOR=#d35400]PhpocServer[/COLOR][/B] server(23);
    [COLOR=#00979C]boolean[/COLOR] alreadyConnected = [COLOR=#00979C]false[/COLOR]; [COLOR=#434F54]// whether or not the client was connected previously[/COLOR]
    
    [COLOR=#00979C]void[/COLOR] [COLOR=#5E6D03]setup[/COLOR]() {
    [B][COLOR=#d35400]Serial[/COLOR][/B].[COLOR=#D35400]begin[/COLOR](9600);
    [COLOR=#5E6D03]while[/COLOR](![B][COLOR=#d35400]Serial[/COLOR][/B])
        ;
    
    [COLOR=#434F54]// initialize PHPoC [WiFi] Shield:[/COLOR]
    [B][COLOR=#d35400]Phpoc[/COLOR][/B].[COLOR=#D35400]begin[/COLOR]([COLOR=#00979C]PF_LOG_SPI[/COLOR] | [COLOR=#00979C]PF_LOG_NET[/COLOR]);
    [COLOR=#434F54]//Phpoc.begin();[/COLOR]
    
    [COLOR=#434F54]// start listening for TCP clients:[/COLOR]
      server.[COLOR=#D35400]begin[/COLOR]();
    
    [COLOR=#434F54]// print IP address of PHPoC [WiFi] Shield to serial monitor:[/COLOR]
    [B][COLOR=#d35400]Serial[/COLOR][/B].[COLOR=#D35400]print[/COLOR]([COLOR=#005C5F]"Chat server address : "[/COLOR]);
    [B][COLOR=#d35400]Serial[/COLOR][/B].[COLOR=#D35400]println[/COLOR]([B][COLOR=#d35400]Phpoc[/COLOR][/B].[COLOR=#D35400]localIP[/COLOR]());
    }
    
    [COLOR=#00979C]void[/COLOR] [COLOR=#5E6D03]loop[/COLOR]() {
    [COLOR=#434F54]// wait for a new client:[/COLOR]
    [B][COLOR=#d35400]PhpocClient[/COLOR][/B] client = server.[COLOR=#D35400]available[/COLOR]();
    
    [COLOR=#434F54]// when the client sends the first byte, say hello:[/COLOR]
    [COLOR=#5E6D03]if[/COLOR] (client) {
    [COLOR=#5E6D03]if[/COLOR] (!alreadyConnected) {
    [COLOR=#434F54]// clear out the transmission buffer:[/COLOR]
          client.[COLOR=#D35400]flush[/COLOR]();
    [B][COLOR=#d35400]Serial[/COLOR][/B].[COLOR=#D35400]println[/COLOR]([COLOR=#005C5F]"We have a new client"[/COLOR]);
          client.[COLOR=#D35400]println[/COLOR]([COLOR=#005C5F]"Hello, client!"[/COLOR]);
          alreadyConnected = [COLOR=#00979C]true[/COLOR];
        }
    
    [COLOR=#5E6D03]if[/COLOR] (client.[COLOR=#D35400]available[/COLOR]() > 0) {
    [COLOR=#434F54]// read the bytes incoming from the client:[/COLOR]
    [COLOR=#00979C]char[/COLOR] thisChar = client.[COLOR=#D35400]read[/COLOR]();
    [COLOR=#434F54]// echo the bytes back to all connected clients:[/COLOR]
          server.[COLOR=#D35400]write[/COLOR](thisChar);
    [COLOR=#434F54]// echo the bytes to the server as well:[/COLOR]
    [B][COLOR=#d35400]Serial[/COLOR][/B].[COLOR=#D35400]write[/COLOR](thisChar);
        }
      }
    }


    Test and Result

    In this example, Arduino acts as TCP server. It needs to use TCP client to test. TCP client can be any kind of TCP client software/program on PC or smartphone.
    In this tutorial, I use a Windows-based software, namely ezTerm
    • Open four ezTerm windows
    • Type IP address of PHPoC Shield and port number (23)
    • Click "Connect" button.
    • Send "Client 1: Hello" message from the 1st ezTerm windows to Arduino.
    • Send "Client 2: Hello" message from the 2nd ezTerm windows to Arduino.
    • Send "Client 3: Hello" message from the 3rd ezTerm windows to Arduino.
    • Send "Client 4: Hello" message from the 4th ezTerm windows to Arduino.
    • We can see the echo message in receiving area of each ezTerm.
    • See echo message in serial monitor



    See Also



    References
    Last edited by support; 12-14-2022, 07:26 AM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 02:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 02:39 PM
    Working...
    X