TCP CLIENT ON ARDUINO BASED PLC

TCP CLIENT ON ARDUINO BASED PLC

On this post is showed how to connect to a server TCP from Arduino based PLC. The requirements of this post is just an M-Duino PLC with Ethernet connection and available TCP server . 

Take a look an example of how to implement a TCP SERVER ON TOUCHBERRY PI 3 WITH NODE.JS

Once the server is running, M-Duino can connect to the server. On this example it is used an M-Duino to connect with the Node.js server called server.js, the same as used on previous example link.

To configure the M-Duino, this post just follows the TCP example from Arduino web site with a few changes. To be able to connect to the server we must know the TCP server IP and the port where this server is listening. 

Next it is showed the Arduino code:

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

byte mac[] = { 0xBE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 100 };
byte server[] = { 192, 168, 1, 105 }; // Touchberry Pi Server
int tcp_port = 5566;

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("Connecting...");

  if (client.connect(server, tcp_port)) { // Connection to server.js
    Serial.println("Connected to server.js");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    if(Serial.available()){
      char s = Serial.read();
      client.write(s); // Send what is reed on serial monitor
      char c = client.read();
      Serial.print(c); // Print on serial monitor the data from server 
    }
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

 

For more information see on Arduino web site the TCP functions Ethernet / Ethernet 2 library

Once the sketch is running on the M-Duino, through the serial monitor it’s possible to send data to the server and this data is replied and printed also on serial monitor. On this example is typed “Hello server.js” to the serial monitor. Next is showed two screenshot with the response of this operation, one of the server.js on the Raspberry and second of the M-Duino serial monitor:

 

See also:

TCP SERVER ON TOUCHBERRY PI 3 WITH NODE.JS

HOW TO PROGRAM ARDUINO BASED PLC THROUGH ETHERNET

HOW TO CONNECT THE ETHERNET PORT USING W5100 LIBRARY

HOW TO MODBUS-ETHERNET

HOW TO IMPLEMENT AN ARDUINO WEB SERVER

 

 

Comments are closed.