Activity Forums Tutorials TCP connection tutorial Reply To: TCP connection tutorial

  • 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);
    }
    }