In this tutorial you will learn about controlling the I/O of a programmable logic controller (PLC) with a mobile app and Modbus TCP/IP protocol. PLCs are basic components of any industrial automation process. In some deployments they are used as stand-alone computers to control a machine while in other settings they can be connected to and controlled remotely by a layer of servers running SCADA and EMI softwares.
Demo Video: youtube.com/watch?v=yDBI9hlQAJU
You can think of a PLC as a specialized controller that comes with built-in software and configuration options to support a wide-range of industrial processes. These controllers are also made to withstand high ranges of temperature, pressure and electromagnetic noise, all of which can be a part of any industrial setting. A transformer reaching a surface temperature of +110° Celcius (230F) is not considered out of the ordinary.
There are several PLC manufacturers; most common brands are General Electric, ABB, Schneider, Siemens, Fatek, Mitsubishi and Rockwell; Rockwell dominates in the US, Siemens typically in Europe and in Japan Mitsubishi is the obvious choice. There are different models and form factors available to support a wide range of processes, such as you may want to use a specialized PLC to control a multi-million dollar steam turbine while you may choose a less expensive PLC to control a small motor.
The standard way of configuring a PLC is through its serial and USB port, present by default in most PLCs while nearly all of them support at least one additional ethernet port. Through this ethernet port, the PLC can be connected to a network hub and then with the controlling and monitoring servers and the outside world. When we talk about connecting and controlling a PLC, nearly all of them support industry standard communication protocols, most famous of them are Modbus TCP/IP and OPC-UA.
In this tutorial, we will connect a Fatek PLC with a supported ethernet module, that will in turn be connected to a Wi-Fi access point. We will then use Evothings Studio to develop a mobile app for controlling I/O terminals of the PLC.
For the purpose of this tutorial I am using following equipment:
- 1 x FATEK PLC (FBs-14MAR2-AC)
- 1 x FATEK Ethernet Module (FBs-CBE)
- A standard air blower (or you can use a lava lamp, or something else of your choise)
- Some wires to connect the air blower with the PLC and the power socket
Please note: You can use any PLC for this tutorial, the only requirement is that you should be able to connect to it using Wi-Fi and also the PLC should be able support Modbus TCP/IP protocol. If you are using a different PLC or brand, than please refer to its documentation for enabling the Modbus TCP/IP, ethernet configuration and program the PLC for a simple I/O control using input registers. Following configuration steps may give you an idea on how to do that with your own model and brand of PLC.
Step 1: Configure the PLC
For the load purposes, I am using a air blower (that simulates a turbine) with two speeds, you can also use a standard lamp to see if the I/O is working as intended.
After connecting the ethernet module on top of PLC, its time to configure the PLC through a supported software. I use Fatek’s Winproladder to program the PLC using ladder logic programming. Download the software, connect your computer with PLC using standard serial cable, open Windproladder and drag & drop control units according to following scenarios:
- If R1 = 1, turn Y1 on.
- If R1 = 5, turn Y2 on.
- If R1 = 10, turn Y1 on then after 10 seconds turn Y2 on and shut both of them down after 20 seconds.
R1 is the register we will be writing on through Modbus TCP/IP.
Y1 and Y2 are output terminals.
Beginner’s Tip: If above scenarios feel complex, just use the first scenario and in Windproladder, set a comparison register. If the value is 1 on R1, turn the Y0 on!
For configuration, I am using PLCs internal mapping; where memory registers are mapped from M0…Mn and output is mapped from Y0…Yn. In this example, I am using memory registers to control the output.
Using comparison and timer block
We will drag & drop 3 comparison blocks, to handle 3 scenarios. If register R1 will get the value of 1, we will turn the memory register M1 on. In the second rung, we are checking if M1 is on from 30 seconds, turn the M2 off. Similarly, if R1 gets the value of 5, we turn M3 on and if it’s 10, we turn M4 on.
That’s all setting variables like we do in programming. The controlling segment of ladder is coming next.
Setting the outputs
Now, we have the memory registers ready and we need to put logic to turn the Y1 and Y2 on/off according to our above scenarios. First two rungs are two timers to control the 3rd scenario, they simply mean turn the second speed of air blower on after 10 seconds and shut-down the output after 20 seconds.
The other two rungs are the basic logic that is controlling the Y0 and Y1 output. That is all a play on the memory registers we were setting above. As with all programming languages, you can set your own logic here. The purpose is to set memory registers to control the output. Basically, if M3 is on and M4 is off, that means we have 5 on the R1 register, turn the Y1 on!
Step 2: Make terminal connections
Use a standard power cable to power the PLC and the load. Connect the two wires (red and black, in no respective order) to the connection terminals marked as L and N. Your PLC is now powered and has a common ground with all other connection terminals. The two connections I am going to use to control two modes of my air blower are Y0 and Y1.
Setting the terminal’s connections
After connecting the load with the PLC, write 0 and 1 on R1 register using Winproladder to test if the PLC is working as intended, then connect the ethernet cable with the Wi-Fi hub.
Step 3: Developing the mobile app
So far, we have configured the PLC to use ethernet and to control the I/O using internal memory registers, then we have connected it to the local Wi-Fi network. Now, it’s the time to control it through a mobile app and Modbus TCP/IP protocol. The Modbus TCP/IP is enabled by-default in the PLC I am using (please refer to the documentation of the PLC if you are using a different brand). So, all we need now is to develop the mobile app and connect to the PLC. For developing the mobile app, we will be using Evothings Studio. It’s a rapid mobile app prototyping tools that comes with many useful libraries and examples to get you started with your own mobile app for IoT, automation, visualization and cloud connection needs.
A useful feature of Evothings Studio is that you don’t need to download and learn mobile app development SDKs from individual mobile operating system. Instead, you can just learn and use the Evothings Studio to develop a cross-platform IoT app using JavaScript and HTML. There are plenty of examples for various purposes, and often you’ll find a template which is a good starting point for your development, much easier than starting from scratch making a mobile app.
- Download Evothings Studio on your computer from evothings.com/download. From this web page, also fetch a Cloud Token to register on the network (yes, there is a free tier as well).
- Download Evothings Viewer app (Android, iOS app stores, search for “Evothings Viewer”)
- Connect Evothings Viewer with the Evothings Studio using the connection key, found in the Connect tab of the computer app
- Open “Examples” tab in Evothings Studio and press “Run” on any example, e.g the “Hello World”, to see everything up and running
That’s it! You have learned how Evothings Studio works! For your ease, I have already developed an example app needed for this tutorial. Simply browse to this Github repository and download/clone the example app code; here’s the zip archive. Once downloaded, find the Control PLC Modbus app example, and drag & drop its “index.html” file to the “My Apps” tab of Evothings Studio to add it. Then press RUN in the project entry just created. Boom – the example app loads directly into the Evothings Viewer!
On the mobile device, provide the IP address and port of your PLC and press the CONNECT button in the app. Once connected, provide the register number and value to write on the PLC, as per our configurations; writing 1 on register R1 (just write a 1, not the R) will turn the PLC on and writing 0 on register R1, will turn it off, similarly, writing 5 on R1 will make our air blower go in double speed (second mode).
Congratulations! You have successfully controlled a PLC using your mobile phone!
Mobile App Code commentary
To communicate with Modbus TCP/IP, I am using a modified version of chrome-modbus JavaScript library and I have added a slightly modified version of this library into the example app code, so you don’t need to download it separately.
To understand the code, open “index.html” in your favourite code editor and analyze the following segment of the JavaScript code:
var client = new ModbusClient();
This is how we are getting a new ModbusClient object from the chrome-modbus library. Next, important segment of code is:
var host = $('#host').val(), var port = parseInt($('#port').val()); client.connect(host, port);
Through the above segment we are connecting to the PLC. Next is writing a single value to a register of the PLC, which is executed through following segment of the code:
var reg = $("#register").val(); var val = $("#regvalue").val(); client.writeSingleRegister(reg, val).then(function () { console.log('Writing Register done!'); }).fail(function () { console.log('Writing Register failed.'); });
Next steps
In our next tutorial, we will develop and discuss the use a mobile app to receive debugging and alarm information from a PLC. In the meantime, feel free to have a look at our other tutorials at http://evothings.com/developer and join our Gitter chat channel https://gitter.im/evothings/evothings to hangout with other like-minded IoT developers.