Introduction
This post shows how to reconnect your PLC Arduino to the server that it is usually connected to. This solution works for different related kinds of issues; on the one hand, if the server starts later than the Arduino controller and the PLC controller shows clear signs of connection errors and, on the other hand, if the industrial PLC loses the connection with the server at any time.
Solution
Code
/*Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ #if defined(MDUINO_PLUS)
#include <Ethernet2.h>
#else
#include <Ethernet.h>
#endif // PLC MAC address
byte mac[] = {0xBE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress plcAddress(10, 10, 10, 2); // Server IP address and TCP port
IPAddress serverAddress(10, 10, 10, 1);
const uint16_t serverPort = 6543; // Ethernet client
EthernetClient client; ////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600L);
Serial.println("ethernet-reconnect started"); Ethernet.begin(mac, plcAddress); Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Update client connection status
updateConnectionStatus();
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void updateConnectionStatus() {
static uint32_t lastReconnectionTime = 0UL; // Reconnect to server if not connected
if (!client.connected()) {
// Prevent fast reconnections
if (millis() - lastReconnectionTime > 1000) {
Serial.println("Client is not connected to server");
reconnect(); // Update last reconnection time to prevent fast tries
lastReconnectionTime = millis();
}
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////
void reconnect() {
// Force client disconnection
client.stop(); // Connect to server
Serial.println("Trying to connect...");
client.connect(serverAddress, serverPort); // Check connection status
if (client.connected()) {
Serial.println("Client connected to server");
} else {
Serial.println("Impossible to connect to server");
}
}
Explanation
Having a look at the code solution we can see that, first of all, there is a function that updates the connection status every moment, reconnecting to the server if is not even connected. This function tries to reconnect to the server every 1 second, but this is an arbitrary time and you can change it as you want (it is good to set a time of at least 1 second to not overcharge the server connection). This one uses another function called "reconnect()" which forces client disconnection, connects to the server and checks connection status.
How to upload this code
3.-Select Tools > Port > COM65 (or the port where you have your device connected).
5.- If everything is working right, your problems should be solved as we saw in the previous sections and, if you want to check out all the processes you can see the confirmation messages (as "ethernet-reconnect started", "IP address: ", "Client connected to server", etc.) in the serial monitor. The Serial Monitor icon is located in the right top corner of the window (make sure to configure the baud rate to 9600 to sync up with the code configuration).
PLC Ethernet Reconnection