Introduction
On this post is showed how to connect to a server TCP from an industrial Arduino based PLC controller. The requirements of this post is just an M-Duino PLC for industrial automation with Ethernet connection and available TCP server.
Requirements
Ethernet PLC: Ethernet PLC
Industrial Shields boards: Industrial Shields Boards
Description
Before starting with this post, take a look at this example. It consists on implementing a TCP SERVER ON TOUCHBERRY PI 3 WITH NODE.JS
Once the server is running, the M-Duino industrial PLC can connect to the server. On this example it is used an industrial controller 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.
Example
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 website 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 screenshots with the response of this operation, one of the server.js on the Raspberry and second of the M-Duino serial monitor: