Interfacing IoT Control to Mitsubishi FX5U PLC using ESP32 Node MCU

Join Our Official PLC Tutorial Point Telegram Channel

Interfacing IoT Control to Mitsubishi FX5U PLC using ESP32 Node MCU

Interfacing IoT Control to Mitsubishi FX5U PLC

1)      Transferring a counter value from FX5U to Ubidots Cloud Dashboard

2)      Getting Button Status from Cloud Dashboard to the PLC

Hardware:

            1) Mitsubishi FX5U PLC

            2) ESP32 NodeMCU

            3) RS485 to TTL converter Module (MAX485)

            4) USB A to B micro Data Cable

            5) Twisted Pair Cable

            6) Power cable for PLC

            7) Ubidots Account with valid Token.

Software Required:

1)      GX Works3 (Version 1.054G)

2)      Arduino IDE (Version 1.8.12) & ESP32 Board Version 1.0.4

 



PLC Diagram:

Connect both SDA and RDA pins of RS485 port to pin A of converter, similarly with SDB and RDB to pin B of converter.

Procedure:

1) Start the GX Works 3, and create new project.

.

2) In the left navigation panel expand options as follows: Project > Parameter >FX5UCPU >Module Parameter >485 Serial
Port.

 

In the basic Setting windows of 485 Serial Port, Change Communication Protocol Type to ‘MODBUS_RTU Communication’ from drop down list.

 

3)Open advanced setting and set the following settings as shown. Parity Bit = Even, Stop Bit = 2, Baud Rate = 9,600 bps

4) Expand Fixed Settings and change value of Host Station No. to 1.

 

5)      Click the Check button, and the No Error Found Dialogue should pop up. If so, Click Apply and proceed further. If no, click Restore Default Settings and Set up again.

6)      List of Modbus Function codes

 

 

7)      Modbus Address list for FX5U PLC

      Address                                               read/write

0x0000 to 0x03FF                                    Y0
to 1023      (coil)

0x2000 to 0x3DFF                                   M0
to 7679     (coil)

0x0000 to 0x1F3F                                    D0
to 7999      (Holding Register)

 

8)      Start Arduino IDE and open example code. Compile and upload the code to ESP32.

9)      Verify the M0 status in the PLC memory by changing the Dashboard button status/

10)   Also Verify Value of data register D5000 with value on dashboard.

 

11)   Arduino Code:

#include <WiFi.h>

#include <WiFiClient.h>

#include <PubSubClient.h>

#include <ModbusMaster.h>

 

#define MAX485_DE      22

#define MAX485_RE_NEG  23

 

#define WIFISSID
“DKTEETRX” // Put your WifiSSID here

#define PASSWORD
“DKTE1234” // Put your wifi password here

#define TOKEN
“BBFF-51xzXZQnl7nidbag1qvWyOqxFoDMa6” // Put your Ubidots’ TOKEN

#define MQTT_CLIENT_NAME
“FX5UDKTE” // MQTT client Name

 

ModbusMaster FX5U;

WiFiClient ubidots;

PubSubClient client(ubidots);

 

int new_update;

int remote_switch;

int counter_value;

int result;

char mqttBroker[]  = “industrial.api.ubidots.com”;

char payload[100];

char topic[150];

char topicSubscribe[100];

void preTransmission()

{


digitalWrite(MAX485_RE_NEG, 1);


digitalWrite(MAX485_DE, 1);

}

void postTransmission()

{


digitalWrite(MAX485_RE_NEG, 0);


digitalWrite(MAX485_DE, 0);

}

void callback(char* topic, byte*
payload, unsigned int length)

{


char p[length + 1];


memcpy(p, payload, length);


p[length] = NULL;


String message(p);


//Serial.write(payload, length);
//Payload is in String Format


if (strcmp(topic, “/v1.6/devices/espfx5u/remote_switch/lv”) ==
0)


{


remote_switch = (char)payload[0] – 48;


/* convert ASCII into integer */


Serial.print(“remote switch changed to “);


Serial.println(remote_switch);


new_update = 1;


}

}

 

void setup()

{


Serial.begin(115200);


WiFi.begin(WIFISSID, PASSWORD);


while (WiFi.status() != WL_CONNECTED)


{


Serial.print(“.”);


delay(500);


}


Serial.println(“IP address: “);


Serial.println(WiFi.localIP());


client.setServer(mqttBroker, 1883);


client.setCallback(callback);


sprintf(topicSubscribe,
“/v1.6/devices/espfx5u/remote_switch/lv”);


client.subscribe(topicSubscribe);

Serial2.begin(9600, SERIAL_8E2);


/* Baud Rate = 9600, 8 data bits,even parity bit, 2 stop bits*/


FX5U.begin(1, Serial2);


/* Use Serial port 2 for modbus operations,Initiating Slave with station
number 1 */


new_update = 0;

 

}

void reconnect()

{


/* Loop until we’re reconnected */


while (!client.connected())




{


Serial.println(“Attempting MQTT connection…”); 


if (client.connect(MQTT_CLIENT_NAME, TOKEN, “”))


{

      Serial.println(“Connected”);


}


else


{

      Serial.print(“Failed, rc=”);

      Serial.print(client.state());

      Serial.println(” try again in 2
seconds”);

      delay(2000);


}


}

}

void loop()

{


if (new_update)


{


FX5U.writeSingleCoil(8192, remote_switch);


/* Write new button state to internal data bit M0

      with modbus address 0x2000 or 8192 in dec
*/


  new_update = 0;


/* set interlock */


}

 result = FX5U.readHoldingRegisters(5000, 1);


/*Read 1 Consecutive Data Register/s starting from D5000 including D5000
*/


if (result == FX5U.ku8MBSuccess)


{


counter_value = FX5U.getResponseBuffer(0);


/* If read operation is succesful, then extract word from first location

      in the slave response buffer and save
into variable */


sprintf(payload, “{“counter_value”:
{“value”: %d}}”, counter_value);


client.publish(topic, payload);


/* Create Payload in specified format and publish the data */


}


if (!client.connected())


{


reconnect();


client.subscribe(topicSubscribe);


}


client.loop();


delay(500);


yield();

}

 

Conclusion:

            We understood the communication between PLC and ESP32 using MODBUS RTU over RS485. Also, we verified the outputs on the IoT dashboard.

 

 

1 thought on “Interfacing IoT Control to Mitsubishi FX5U PLC using ESP32 Node MCU”

  1. Dear Vinod,

    Thanks. Well explained. I am trying similar application of data logging by integrating ESP32 with PLC FX5U-32MT-ESS.

    I want to connect HMI and ESP32 together. When I connect both, It shows error in PLC, but when it is connected individually it is able to read data from PLC.

    Can you help me to know what exactly reason could be possible for error?

    Regards,
    Samir Taral

    Reply

Leave a Comment