Activity Forums Tutorials TCP connection tutorial

  • TCP connection tutorial

     jasin updated 5 years, 6 months ago 1 Member · 2 Posts
  • jasin

    Member
    June 24, 2019 at 7:41 am

    Controllino MAXI and Controllino MEGA come with Ethernet controller WIZnet W5100 builtin.

    There is Built-In library compatible with Controllino MAXI and Controllino MEGA see:

    [img]https://i.postimg.cc/Nj6n4Zmr/Ethernet-library.png[/img]

    So it is possible to include Ethernet library:

    #include "Ethernet.h"

    To make the ethernet work, some parameters must be declared:

    byte mac[] = {0xE8, 0x2A, 0xEA, 0x4B, 0x1F, 0xC3};
    IPAddress localip(192, 168, 6, 191);
    IPAddress serverip(192, 168, 6, 2);

    mac is HW MAC address, localip is IP address of Controllino itself, serverip is IP address of remote client (can be other Controllino).

    Ethernet library provides EthernetClient and EthernetServer classes, EthernetClient for connecting to remote and EthernetServer for setup “TCP listener”

    EthernetClient client;
    EthernetServer server(50001);

    For EthernetServer 50001 is the port number on which Controllino will listen.

    Ethernet has to be initialized:

    Ethernet.begin(mac, localip);

    And then, a message can be sent to a remote server:

        client.connect(serverip, 50001);
        delay(10);
        client.println("Execute Script: "script A0";");
        delay(10);
        client.stop();

    Or message can be received:

      EthernetClient proxyserver = server.available();
      if (proxyserver)
      {
        while (proxyserver.connected())
        {
          if (proxyserver.available())
          {
            char c = proxyserver.read();
            message += c;
          }
        }
        proxyserver.stop();
      }
  • jasin

    Member
    June 24, 2019 at 9:42 am

    It is also possible to use several Controllino devices in a Master-Slave Scenario.

    Example scenario see:

    [img]https://i.postimg.cc/3rkZ3XYJ/Controllino-net.jpg%5B/img%5D

    Where two “slave” Controllinos are connected to one “Master” Controllino and these 3 devices acts like “one”.

    When “Master” acts like “proxy” for TCP messages from “slaves”.

    Real setup with logical “1” connected to A0 of each Controllino see:

    [img]https://i.postimg.cc/8cxNFhG3/Controllino-setup.jpg%5B/img%5D

    For listening on PC Hercules tool has been used see:

    [img]https://i.postimg.cc/13tbxgZV/Controllino-Hercules-listen.jpg%5B/img%5D

    Which receives messages from all 3 devices.

    Master Controllino sketch:

    Code:


    #include
    #include
    #include

    /*
    CONTROLLINO – Demonstration of TCP protocol usage, Version 01

    Compatible with CONTROLLINO MAXI, MAXI Automation and MEGA.
    IMPORTANT INFORMATION!
    Please, select proper target board in Tools->Board->Controllino MAXI/MEGA before Upload to your CONTROLLINO.

    (Refer to https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library if you do not see the CONTROLLINOs in the Arduino IDE menu Tools->Board.)

    Created 14 Jun 2019
    by Jasin
    https://controllino.biz/

    Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.
    Visit our forum here: https://forum.controllino.biz/
    */

    // Set MAC adress
    byte mac[] = {0xE8, 0x2A, 0xEA, 0x4B, 0x1F, 0xC3};
    // Set local IP
    IPAddress localip(192, 168, 6, 191);
    // Set remote IP of server
    IPAddress serverip(192, 168, 6, 2);

    EthernetClient client;
    EthernetServer server(50001);

    String message = “”;

    void setup()
    {
    pinMode(CONTROLLINO_A0, INPUT);

    //initialize Ethernet
    Ethernet.begin(mac, localip);

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware)
    {
    while (true)
    {
    delay(1); // do nothing, no point running without Ethernet hardware
    }
    }

    server.begin();
    }

    void loop()
    {

    message = “”;
    // check incoming connection
    EthernetClient proxyserver = server.available();
    if (proxyserver)
    {
    //read incomming message
    while (proxyserver.connected())
    {
    if (proxyserver.available())
    {
    char c = proxyserver.read();
    message += c;
    }
    }

    //Send received message to server (serverip)
    client.connect(serverip, 50001);
    delay(10);
    client.print(message);
    delay(10);
    //close both TCP connection
    client.stop();
    proxyserver.stop();
    }
    delay(400);
    //handle local input
    if (digitalRead(CONTROLLINO_A0))
    {
    //connect to server
    client.connect(serverip, 50001);
    delay(10);
    //send a message to server (serverip)
    client.println(“Execute Script: “script A0″;”);
    delay(10);
    //close TCP connection
    client.stop();
    delay(400);
    }
    }

    Client 1 Controllino sketch:

    Code:


    #include
    #include
    #include

    /*
    CONTROLLINO – Demonstration of TCP protocol usage, Version 01

    Compatible with CONTROLLINO MAXI, MAXI Automation and MEGA.
    IMPORTANT INFORMATION!
    Please, select proper target board in Tools->Board->Controllino MAXI/MEGA before Upload to your CONTROLLINO.

    (Refer to https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library if you do not see the CONTROLLINOs in the Arduino IDE menu Tools->Board.)

    Created 14 Jun 2019
    by Jasin
    https://controllino.biz/

    Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.
    Visit our forum here: https://forum.controllino.biz/
    */

    // Set MAC adress
    byte mac[] = { 0xE8, 0x2A, 0xEA, 0x4B, 0x1F, 0xC3 };
    // Set local IP
    IPAddress localip(192, 168, 6, 192);
    // Set remote IP of server
    IPAddress server(192, 168, 6, 191);

    EthernetClient client;

    void setup(){
    pinMode(CONTROLLINO_A0, INPUT);

    //initialize Ethernet
    Ethernet.begin(mac, localip);

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    while (true) {
    delay(1); // do nothing, no point running without Ethernet hardware
    }
    }

    }

    void loop(){
    //handle local input
    if(digitalRead(CONTROLLINO_A0))
    {
    //connect to server
    client.connect(server, 50001);
    delay(10);
    //send a message to server
    client.println(“Execute Script: “script A10″;”);
    delay(10);
    //close TCP connection
    client.stop();
    delay(1000);
    }
    }

    Client 2 Controllino sketch:

    Code:


    #include
    #include
    #include

    /*
    CONTROLLINO – Demonstration of TCP protocol usage, Version 01

    Compatible with CONTROLLINO MAXI, MAXI Automation and MEGA.
    IMPORTANT INFORMATION!
    Please, select proper target board in Tools->Board->Controllino MAXI/MEGA before Upload to your CONTROLLINO.

    (Refer to https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library if you do not see the CONTROLLINOs in the Arduino IDE menu Tools->Board.)

    Created 14 Jun 2019
    by Jasin
    https://controllino.biz/

    Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.
    Visit our forum here: https://forum.controllino.biz/
    */

    // Set MAC adress
    byte mac[] = { 0xE8, 0x2A, 0xEA, 0x4B, 0x1F, 0xC4 };
    // Set local IP
    IPAddress localip(192, 168, 6, 193);
    // Set remote IP of server
    IPAddress server(192, 168, 6, 191);

    EthernetClient client;

    void setup(){
    pinMode(CONTROLLINO_A0, INPUT);

    //initialize Ethernet
    Ethernet.begin(mac, localip);

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    while (true) {
    delay(1); // do nothing, no point running without Ethernet hardware
    }
    }

    }

    void loop(){
    //handle local input
    if(digitalRead(CONTROLLINO_A0))
    {
    //connect to server
    client.connect(server, 50001);
    delay(10);
    //send a message to server
    client.println(“Execute Script: “script A20″;”);
    delay(10);
    //close TCP connection
    client.stop();
    delay(1000);
    }
    }

Viewing 1 - 2 of 2 replies

Log in to reply.

Original Post
0 of 0 posts June 2018
Now