TCP connection tutorial

Community guides and tutorials
Post Reply
User avatar
Jasin
Posts: 6
Joined: Wed Jan 30, 2019 8:35 am

TCP connection tutorial

Post by Jasin » Mon Jun 24, 2019 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:
Image

So it is possible to include Ethernet library:

Code: Select all

#include <Ethernet.h>
To make the ethernet work, some parameters must be declared:

Code: Select all

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"

Code: Select all

EthernetClient client;
EthernetServer server(50001);
For EthernetServer 50001 is the port number on which Controllino will listen.

Ethernet has to be initialized:

Code: Select all

Ethernet.begin(mac, localip);
And then, a message can be sent to a remote server:

Code: Select all

    client.connect(serverip, 50001);
    delay(10);
    client.println("Execute Script: \"script A0\";");
    delay(10);
    client.stop();
Or message can be received:

Code: Select all

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

User avatar
Jasin
Posts: 6
Joined: Wed Jan 30, 2019 8:35 am

TCP connection tutorial (Master vs Slaves)

Post by Jasin » Mon Jun 24, 2019 9:42 am

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

Example scenario see:
Image

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:
Image

For listening on PC Hercules tool has been used see:
Image
Which receives messages from all 3 devices.

Master Controllino sketch:

Code: Select all

#include <Ethernet.h>
#include <SPI.h>
#include <Controllino.h>

/*
  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: Select all

#include <Ethernet.h>
#include <SPI.h>
#include <Controllino.h>

/*
  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: Select all

#include <Ethernet.h>
#include <SPI.h>
#include <Controllino.h>

/*
  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);    
  }
}

Post Reply