books books
books
>
PLC Programming
ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266 Introduction to PLC programming with OpenPLC, the fi rst fully open source Programmable Logic Controller on the Raspberry Pi, and Modbus examples with Arduino Uno and ESP8286 PLC programming is very common in industry and home automation. This book describes how the Raspberry Pi 4 can be used as a Programmable Logic Controller. Before taking you into the programming, the author starts with the software installation on the Raspberry Pi and the PLC editor on the PC, followed by a description of the hardware. You'll then find interesting examples in the different programming languages complying with the IEC 61131-3 standard. This manual also explains in detail how to use the PLC editor and how to load and execute the programs on the Raspberry Pi. All IEC languages are explained with examples, starting with LD (Ladder Diagram) over ST (Structured Control Language) to SFC (Special Function Chart). All examples can be downloaded from the author's website. Networking gets thorough attention too. The Arduino Uno and the ESP8266 are programmed as ModbusRTU or ModbusTCP modules to get access to external peripherals, reading sensors and switching electrical loads. I/O circuits complying with the 24V industry standard may also be of interest for the reader. The book ends with an overview of commands for ST and LD. After reading the book, the reader will be able to create his own controllers with the Raspberry Pi.
Josef Bernhardt became interested in electronics at a very young age, when he built his first detector receiver, followed over the years by many other circuits. He gained his first programming experience in the 1980s with the CommodoreVC20. He became familiar with assembler programming on the 8088 processor. Josef can look back on more than 30 years of electronics and software development at University of Regensburg. With his own SMD production, he also implements electronic customer projects, always driven by the pleasure of electronics and programming.
Elektor International Media www.elektor.com
PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt
with the Raspberry Pi and the OpenPLC project
PLC Programming
with the Raspberry Pi and the OpenPLC project ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266
Josef Bernhardt
Cover PLC ENGLISH Josef Bernhardt.indd Alle pagina's
29-10-2021 10:15
PLC Programming with the Raspberry Pi and the OpenPLC Project ModbusRTU and ModbusTCP examples using the Arduino Uno and the ESP8266
● Josef Bernhardt
● This is an Elektor Publication. Elektor is the media brand of Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to reproduce any part of the publication should be addressed to the publishers.
● Declaration
The Author and the Publisher have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause.
● British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
● I SBN 978-3-89576-469-1 Print
ISBN 978-3-89576-470-7 eBook
● © Copyright 2021: Elektor International Media B.V. Editor: Denis Meyer
Prepress Production: D-Vision, Julian van den Berg
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Content Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Chapter 1 • Installing the Raspberry Pi 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.1 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.2 Installing the operating system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.3 Installing the VNC Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4 Installing the File Transfer Software WinSCP . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.5 Installing the openplcproject runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Chapter 2 • OpenPLC Editor Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1 Download and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.2 Raspberry Pi pin descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.3 Additional Hardware I/O Test Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.4 Additional 24 V PLC Board hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Chapter 3 • The OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.1 Description of the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.2 Ladder Logic Example (LD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.3 Function Block example (FBD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.4 Instruction List example (IL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.5 Structured Text examples (ST, SCL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 3.5.1 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.5.2 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 3.5.3 Conversion operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 3.5.4 Standard function blocks according to IEC 61131-3 . . . . . . . . . . . . . . . . . . 88 3.5.5 First ST program example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 3.5.6 ST example for controlling a conveyor belt . . . . . . . . . . . . . . . . . . . . . . . . 93 3.5.7 Defining arrays with the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . . . . . 96 3.5.8 Defining structures with the OpenPLC Editor . . . . . . . . . . . . . . . . . . . . . . 100 3.5.9 Combining structures with arrays using the OpenPLC Editor . . . . . . . . . . . 103 3.5.10 Definition of ENUMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 3.6 Sequential Function Chart example (SFC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Chapter 4 • OpenPLC and the Modbus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 4.1 Testing PLC programs with Modbus TCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
●5
PLC Programming with the Raspberry Pi and the OpenPLC Project 4.2 Visualization of PLC programs with AdvancedHMI . . . . . . . . . . . . . . . . . . . . . . . 130 4.3 Visualization of PLC programs via the Internet . . . . . . . . . . . . . . . . . . . . . . . . . 140 Chapter 5 • Modbus I/O modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.1 Modbus RTU module with the Arduino UNO . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.2 Modbus TCP module with the ESP8266 and WLAN . . . . . . . . . . . . . . . . . . . . . . 158 5.3 Web server application with the ESP8266 I/O module . . . . . . . . . . . . . . . . . . . . 168 Chapter 6.1 • Bibliography, circuit diagrams and layouts . . . . . . . . . . . . . . . . . . 175 6.2 Web links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 6.3 Modbus commands for the ESP8266 I/O module . . . . . . . . . . . . . . . . . . . . . . . 178 6.4 Circuit diagrams and layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
6.4.1 Circuit diagram for the test board with buttons and LEDs: . . . . . . . . . . . . 181
6.4.2 Circuit diagram for the test board with 24V input outputs: . . . . . . . . . . . . 183
6.4.3 Circuit diagram for the ESP8266 board with 24V input outputs: . . . . . . . . 185
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
●6
Preface
Preface This book is intended to provide readers with a practical introduction to using the Raspberry Pi computer as a PLC (programmable logic control) for their projects. The project is indebted to programmers Edouard Tisserant and Mario de Sousa. They started the "Matiec project" after the introduction of IEC standard 61131-3 in 2003. This made it feasible to translate the programming languages introduced in the standard into C programs. Later, when the Raspberry Pi became increasingly popular, Thiago Alves started the "openplcproject". He extended the editor from the "Beremiz" project and wrote a runtime library and a web interface for the Raspberry Pi and the PC. From then on, it was possible to write programs on the PC and install them on the Raspberry Pi. Many Raspberry Pi users are now able to realize their own controls and regulation systems using their own hardware. The hardware and software are also excellent for training purposes because it abides by the IEC standard. Beginners will also learn everything about installation and programming in the five programming languages in order to build their own control systems. In a later chapter, the visualization with AdvancedHMI is discussed to display processes on the screen. Circuits with the Arduino and ESP8266, which are necessary for Modbus, are also explained. I wish you lots of success in reading and using the book. Bad Abbach, May 2021 Josef Bernhardt
●7
PLC Programming with the Raspberry Pi and the OpenPLC Project
Introduction Programmable logic controllers (PLCs) have revolutionized industrial control technology. PLCs have been used primarily in industrial control systems and home automation since their invention by Richard E. Morley about 50 years ago. Here is the definition of "PLC" according to EN61131-3: "A PLC is a digitally operating electronic system for use in industrial environments with a programmable memory for internal storage of user-oriented control instructions to implement specific functions such as logic control, sequence control, timing, counting, and arithmetic functions to control various types of machines and processes through digital or analog input and output signals." The Raspberry Pi is perfectly suitable for an application as a PLC because of its architecture with the GPIO connector, as well as its low price. Various ready-made PLCs based on the Raspberry Pi are available on the market. A big advantage of PLC programming is that the programmer does not have to learn the hardware details of I/O lines. The analog and digital outputs and inputs are like variables. This is also possible with modules that are connected to the Raspberry Pi via a network using a protocol such as Modbus-TCP. Another advantage of PLC programming is the inter-compatibility of PLC systems. Programs for a PLC from manufacturer "A" can generally be used for PLCs from manufacturer "B" without much effort.
Figure 0.1: Raspberry Pi PLC Unipi 1.1. Figure 0.1 pictures a PLC with 24 V inputs and relay outputs attached to a Raspberry Pi.
●8
Introduction
Most PLC systems support graphical and textual programming languages. The "openplcproject" fully supports the IEC 61131-3 standard, which defines basic software architecture and programming languages for PLCs.
Figure 0.2: Overview OpenPLC. The system consists of a runtime component, which is basically the software installed on the Raspberry Pi. This executes the PLC program. The program editor is installed on the PC under Windows or Linux to write the PLC program according to the IEC 61131-3 standard. The following PLC languages are supported: Programming language
Abbreviation
Ladder Logic
LD
Function Block
FBD
Instruction List
IL
Structured Text
ST
Sequential Function Chart
SFC
●9
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 0.3: IEC programming languages. Graphical languages KOP and FUP are translated to ST by the Matiec compiler. Chapter 1 starts with the installation of the runtime component on the Raspberry Pi. After downloading the installation program, you will create a micro SD card with the operating system. After commissioning, you will install the PLC Runtime and perform the first test. Next, you will deal with the editor and its user interface in Chapter 2. You'll get to download a finished example and translate it into a program for transferring to the Raspberry Pi. In Chapter 3 you start programming with the PLC editor from "openplcproject". You will create your programs in various programming languages, translate, and upload them to the Raspberry Pi for testing. Visualization should not be neglected in the process, and Chapter 4 examines the AdvancedHMI project which allows you to visualize processes running on the PLC via Modbus, on a PC. Chapter 5 examines the possibility of communicating with external modules. You'll be using the popular Modbus/RTU protocol for Arduino UNO and the Modbus/TCP protocol for ESP8266 via WLAN. Also, circuits and layouts for this hardware are presented. All program examples can be downloaded from the author's website. The links to the website are in the Appendix under "Web Links".
● 10
Chapter 1 • Installing the Raspberry Pi 4
Chapter 1 • Installing the Raspberry Pi 4 1.1 Hardware description The Raspberry Pi is now a well-known minicomputer, which, thanks of its low price, is widely used by hobbyists and industrial companies alike.
Figure 1.1: The Raspberry Pi 4. The RPi 4 has enough interfaces to be used as a PLC. In addition to standard interfaces such as HDMI, USB, Ethernet, and audio, it has a 40-pin GPIO connector strip to connect to the outside world. Relays, buttons, switches, etc. can be connected to this connector via suitable interfaces. Hardware aspects are not discussed further for now — the Raspberry Pi website is host to several detailed tutorials about this minicomputer. If you are looking for a more compact solution, you could also use the Raspberry Pi Zero W. After a first test by the author, everything turned out to work fine. If you can tolerate the lengthy installation process which takes several hours, the "W" is a good, low-priced alternative to the Raspberry Pi 4.
Figure 1.2: Raspberry Pi Zero W. To use the Raspberry Pi as a PLC, first install the operating system, which can be found on the Raspberry.org website. Link: Raspberry Pi OS – Raspberry Pi.
● 11
PLC Programming with the Raspberry Pi and the OpenPLC Project
1.2 Installing the operating system Download the Raspberry Pi Imager here. This is a simple and quick way to install the operating system on a Micro SD card. There is also a video about this on YouTube. Link: https://www.youtube.com/watch?v=J024soVgEeM After downloading, change to this directory and start the imager. Link: https://downloads.raspberrypi.org/imager/imager_1.5.exe
Figure 1.3: Raspberry Pi Imager launched. After clicking on "Install", the installation of the Imager program will commence. The Imager is used to install our Micro SD card.
Figure 1.4: Raspberry Pi Imager Setup.
● 12
Chapter 1 • Installing the Raspberry Pi 4
With a click on the checkbox "Run Raspberry Pi Imager" the program launches after successful installation. Select the Raspberry Pi OS (32-bit) as the operating system. Then select the drive where the Micro SD card is connected. Here, that's drive G:
Figure 1.5: Raspberry Pi Imager OS selection. Click the "Write" button to launch the installation.
Figure 1.6: Raspberry Pi Imager startup. Confirm the security prompt with "YES".
● 13
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.7: Writing to the SD card using the Raspberry Pi Imager. Now the operating system is downloaded and written to the micro SD card. This can take about 30 minutes, depending on the computer. After the signal that writing to the card has been completed, exit the Imager with "CONTINUE". Documentation for installation and usage can also be found here. Link: Raspberry Pi Documentation The next step is to set up the Raspberry Pi. Insert the programmed Micro SD card into the Raspberry Pi. Next, plug in a monitor, keyboard, and mouse for the first setup of the Raspberry Pi. Now connect the 5 V power supply to the Raspberry Pi. The operating system should boot. After the initial start, the operating system prompts for the country and time zone selection. Enter your details here. Now the settings are installed. After this, the password is requested. The default username is "pi", and the password is "raspberry". If there is no Ethernet connection, set up the network access via WLAN. In the upper right corner between the Bluetooth and the speaker icon, click on the WLAN icon. Select your WLAN network and enter the access code. After a few seconds, the connection to the home or office WLAN is established. Continue with the installation of the updates. This can take several minutes. After this, a reboot is performed. When you click on the WLAN icon, you will also see the IP address. Make a note of it for later use with VNC and the PLC software, which is yet to be installed. Here, the IP is: 192.168.178.89. Later, when the Raspberry Pi is used as a PLC, it will be easier to access it through VNC Viewer. Follow: Menu Settings Raspberry Pi Configuration. Next, under Interfaces, the VNC Viewer can be enabled.
● 14
Chapter 1 • Installing the Raspberry Pi 4
1.3 Installing the VNC Viewer VNC is a graphical desktop sharing system allowing you to remotely control the desktop interface of a computer, in this case the Raspberry Pi (VNC Server) from another computer or mobile device using VNC Viewer (Client). VNC Viewer transmits keyboard, mouse, or touch events to VNC Server and in return receives information for screen updates. This can be downloaded and installed on a PC for now. Link: https://www.realvnc.com/en/connect/download/viewer/. The appropriate viewer can be downloaded at this url. We're using the latest version for Windows 10, but an Android version is also available. After downloading, start the installation.
Figure 1.8: VNC Viewer installation.
● 15
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.9: VNC Viewer installation. Continue the installation by clicking on "Next". Click on "Install". This will launch the installation of the Viewer. Start VNC Viewer and connect to the Raspberry Pi.
Figure 1.10: VNC Viewer start menu. Under "File", and then "New Connection", enter the access data from your Raspberry Pi.
● 16
Chapter 1 • Installing the Raspberry Pi 4
Figure 1.11 VNC Viewer configuration. Enter the IP address and a suitable name. Afterwards, confirm the "Properties", "General" window with "OK".
Figure 1.12: VNC Viewer credentials Enter the username "pi" and password "raspberry" and confirm with "OK". Shortly, the Raspberry Pi desktop will appear on your PC screen.
● 17
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.13 Raspberry Pi OS Desktop.
1.4 Installing the File Transfer Software WinSCP WinSCP is an open-source SFTP and FTP client for Windows, needed to transfer files between a PC and Raspberry Pi. To get access, enable SSH on the Raspberry Pi under Settings Raspberry Pi Configuration Interfaces.
Figure 1.14: SSH configuration. Firstly, download and save the installer.
● 18
Chapter 1 • Installing the Raspberry Pi 4
Figure 1.15: WinSCP Download. Now change to the download directory and start the installation.
Figure 1.16: WinSCP installation Accept the license agreement by clicking "Accept".
● 19
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.17: WinSCP installation. Confirm with "Next".
Figure 1.18: WinSCP installation. Select the user interface "Commander", go to "Next" and confirm with "Install".
● 20
Chapter 1 • Installing the Raspberry Pi 4
Start WinSCP and set up a new connection with the Raspberry Pi's IP address. As an example, the IP address was 192.168.178.89 with access data.
Figure 1.19: WinSCP credentials configuration. Log in and save the connection destination.
Figure 1.20: WinSCP configuration of directories. You can now access the file directories of the Raspberry Pi.
● 21
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.21: WinSCP in action
1.5 Installing the openplcproject runtime With your PLC computer set up so far, the next important step is to install the Open PLC Runtime from the Open PLC Project website. Link: https://www.openplcproject.com/runtime/raspberry-pi/ The easiest way to copy OpenPLC files to your Raspberry Pi is to use Git. Normally, Git is preinstalled on Raspbian. If for some reason you don't have Git installed on your system, you can fix that by typing some commands. To be able to do that, open a Terminal box either on the Raspberry Pi or via the VNC viewer and enter the following command: sudo apt-get install git
and confirm with "Enter".
● 22
Chapter 1 • Installing the Raspberry Pi 4
Figure 1.22: Install GIT. For the installation of the OpenPLC Runtime (PLC operating system) you need the following commands: git clone https://github.com/thiagoralves/OpenPLC_v3.git cd OpenPLC_v3 ./install.sh rpi
These can also be copied and pasted into the terminal window. On the PC, copy the command to the clipboard and paste it on the Raspberry Pi in the Terminal window with the right-hand mouse button.
● 23
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.23: Installing OpenPLC Runtime. This process takes some time. After the installation is finished, you should restart the Raspberry Pi. The Open PLC Runtime has an integrated web server allowing you to configure OpenPLC and upload new programs. Subsequently, these can be started on the Raspberry Pi. This web server can be accessed by opening a web browser on the PC and entering the IP address of the Raspberry Pi through port 8080. For example, if your Raspberry Pi is at IP 192.168.178.89 in your network, you need to enter the following address in your browser: 192.168.178.89:8080 Of course, the web page can also be accessed on the Raspberry Pi with the web browser. Just enter "localhost:8080" in the URL line to get to the OpenPLC Runtime page.
Figure 1.24: OpenPLC Runtime web interface.
● 24
Chapter 1 • Installing the Raspberry Pi 4
The default username and password are both: "openplc". These credentials can be changed after login.
Figure 1.25: OpenPLC Runtime Dashboard. After a successful login, the start menu pops up. Now various important settings for the hardware can be made. Our own PLC programs are also uploaded and started here. Configuration of external modules is also possible as well as monitoring of the running program.
Figure 1.26: OpenPLC runtime hardware selection.
● 25
PLC Programming with the Raspberry Pi and the OpenPLC Project
This is where the extensive selection takes place of the hardware running the Open PLC Runtime. Here, choose the Raspberry Pi. Save the setting with "Save Changes".
Figure 1.27: OpenPLC Runtime settings. DNP3 and Ethernet/IP do not need to be selected for the time being, since you do not use these field buses. We leave the Modbus server installed as it's needed later for visualization using the AdvancedHMI software. In case the Raspberry Pi should run the last program after a reboot, you can click on Settings Start OpenPLC in RUN Mode. Now click "Save Changes" and the installation is finished. For an initial test, use the PLC program "PLC sample AWL running light", which can be found in the download area. To test drive the PLC program, transfer it from the PC to the new Raspberry Pi PLC by way of the web interface.
● 26
Chapter 1 • Installing the Raspberry Pi 4
Figure 1.28: OpenPLC PLC program upload. For this, click on "Browse" for the file dialog, select the ST file from your PLC project, and confirm with "Open".
Figure 1.29: Select your OpenPLC ST file.
● 27
PLC Programming with the Raspberry Pi and the OpenPLC Project
Figure 1.30: The OpenPLC PLC ST-Program upload process. Now enter a name at the top under "Name". Here it's "PLC Example Running Light" so that the Upload button is enabled. Under "Description" a description of the program function and possibly a version number, can be entered.
Figure 1.31: Compiling ST program into C.
● 28
Chapter 1 • Installing the Raspberry Pi 4
Now the ST program gets compiled into a C program for the Raspberry Pi. After that it can be started. Click on "Go to Dashboard" and change to the main menu. By clicking on "Start PLC", the program kicks off.
Figure 1.32: Dashboard Runtime Los. Now three LEDs at the output pins %QX0.0, %QX0.1 and %QX0.2 should light up one after the other. You should also be able to observe this under "Monitoring".
Figure 1.33: Connecting the LEDs The complete schematic of the test board can be found in the Appendix. If this test does not work, it's probably because of the "WiringPI" library this is responsible for accessing the GPIO pins. A likely solution to the problem is to install an up to date, Raspberry Pi 4-adapted, version. To do so, halt the running PLC program with the button "Stop PLC".
● 29
PLC Programming with the Raspberry Pi and the OpenPLC Project
Next, in the terminal window enter the following commands for an update: cd /tmp wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb
Figure 1.34: Install WiringPI library. After that, the installation can be tested. The version 2.52 should be displayed. gpio –v
It should display gpio version: 2.52 Copyright (c) 2012-2018 Gordon Henderson This is free software with ABSOLUTELY NO WARRANTY. For details type: gpio -warranty Raspberry Pi Details: Type: Pi 4B, Revision: 01, Memory: 2048MB, Maker: Sony * Device tree is enabled. *--> Raspberry Pi 4 Model B Rev 1.1 * This Raspberry Pi supports user-level GPIO access.
This completes the installation, and the PLC program is ready for starting. Now the LEDs should light up one after the other.
● 30
Chapter 1 • Installing the Raspberry Pi 4
Figure 1.35: Logout from the OpenPLC PLC program. Now you can exit and sign out of the Raspberry Pi by clicking on "Logout". The next Chapter covers installing the editor on your PC and writing your first program.
● 31
Index
Index Symbols 24 V output %IW0, %IX0, %MD0, %ML0, %MW0, %QW0, %QX0.0, %QX2.0
156 139
A ABS 87 access code 14 access data 164, 171 access structure variables 105 ACOS 87 action 51, 110, 111, 112 action block 114 actuators 120 ADC 159 A/D converter 124, 156 AddCatalog 33 Adding a contact 61, 64 Addition 87 Add new device 153 address 17, 126 address range 137 address space 139 AdvancedHMI 26, 123, 129, 130, 132, 166 AJAX 143, 169 Aktion 169 alarm contacts 168 alternative branching 119 analog input 153, 159, 167 analog-to-digital 153 analog value 152 AND 67, 80, 83, 87 Android 15 ANY_TO_ 88 ANY_TO_BOOL 88 ANY_TO_DINT 88 ANY_TO_INT () 88 ANY_TO_REAL () 88 ANY_TO_SINT () 88 ANY_TO_STRING () 88 ANY_TO_TIME () 88 ANY_TO_UDINT () 88 ANY_TO_UINT () 88 ANY_TO_USINT () 88
archive 32 ArduinoESP8266 162 Arduino HTTP 169 Arduino IDE 147, 148, 162 Arduino UNO 10, 147, 148 Arduino UNO IDE settings 149 Arduino UNO IDE upload 150 array 96, 103 AS 110 ASIN 87 assembly language 53, 76 ATAN 87 ATmega328P 147, 155 audio 11
B barcode 93 Barcode printing 94 base type 88 Basic data types 82 BCD_TO_BIN () 88 Beremiz 7 BIN_TO_BCD () 88 Bit AND 87 Bit Exclusiv OR 87 Bit OR 87 bit pattern 96 Bitwise operations 74 BlinkLed 93, 94 BlinkTimer 91, 92 Boardmanager 162 branches 55 breadboard 39 BTN_HMI 138 BTN_Modbus 122, 125 button 92, 124 button press 110, 125, 137
C CASE 86 CDC ACM 153 CH340 164 chart 55, 109, 146, 174 C_HTTP 141 Circuit diagram for the ESP8266 board 185 Class 58, 69
● 187
PLC Programming with the Raspberry Pi and the OpenPLC Project
client 15, 120 clipboard 23, 50 Codeblocks, C_HTTPPOST01 140, 141 COM6 149 Commander 20 comment 83, 110 Common operator 87 Commport 149, 164 communication error counter 128 community 47 compatibility 8 compiled, compiler 134 configuration 17, 21 connect 140, 141 Connected to Server 150 connections 65, 111 contact 110 convergence 119 conversion operator 88 conveyor belt 93, 109 copy 50, 141 COS 87 counter 88 country and time zone selection 14 coupling relay 160 C program 140 Create a new variable 110 Create Program 50 CTD, CTU, CTUD 88 current limiting 42 Cutting objects 50
D D/A 124, 167, 180 Dashboard 39 data management 81 data type 58, 59, 69, 96, 100, 103, 107 DC output 156 Debug menu 48 declare, declaring 91 Declaring variables 59, 111 default value 83 define 111 definition 94 Description 28 desktop 17, 18
● 188
desktop sharing system 15 DHT11/22 159 DINT 82 direct voltage 156 DIV 80 divergence 118 Division 80, 87 DNP3 26 DO WHILE 54 download area 26 driver, drivers 162, 164, 171 DS18B20 159
E EasyModbusClient 123, 150 EasyModbusClientAndServer 123 Editor ST file 35 Edouard Tisserant 7 EL816 optocouplers 159 electronic switch 42 elements 102 ELSE 86 ELSEIF 85 empty project 33 enable SSH on the Raspberry Pi 18 END_CASE 86 END_FOR 83 END_IF 85 END_REPEAT 90 END_WHILE 84, 90 Entering the ENUM variables 107 ENUMs 106 equal 87 ESP8266 158, 162, 165, 186 ESP8266_HTTP_POST 171 ESP8266 I/O board 158 ESP8266 I/O module 168, 171 ESP8266_Modbus_BOARD1_1 162 ESP8266 NodeMCU Board Pins 172 Ethernet 11, 120 exclusive OR 80 existing project 50 EXIT 90 EXP 87 expression 82, 83 EXPT 87
Index
F
humidity sensor
falling edge 88 Falling Edge Trigger 89 FC1 124, 126, 140, 148, 152 FC2 124, 140, 148, 153, 179 FC3 148 FC4 148, 153, 179, 180 FC5 124, 148, 151, 152, 178 FC6 126, 148, 152, 180 FC15 179 FC16 152 field buses 26 file dialog 68 File Menu 45 File menu of the OpenPLC Editor 45 File Transfer Software WinSCP 18 FOR 82, 83 F_TRIG 88, 89 full screen 47 function block 9, 51, 52, 69, 72, 80, 81, 83, 88 Function Block example 67 FUP 10
G galvanic isolation 41, 155 gate 67 gate controls 109 gaugetest.html 174 Generic ESP8266 Module 163 Git 22 Go to Dashboard 29 GPIO 8, 11, 42, 137, 159 Greater than, Greater than equal to
87
H HDMI 11 Hello_World 33, 34, 36 HelloWorld.ST 36 Help menu 47 HITFET BSP76 41, 159 home automation 8, 120, 130 house 168 HTML 143 HTML server 158 http post 168, 171
159
I I2C 159 I2S 159 Idle 167 IEC 61131-3 9, 88 IEC address 59, 122 IEC programming languages 81 IEC symbols 59 IEEE 802.11 159 IF 85, 87 IL command overview 80 IL program example 53, 77 Imager 13 Imager program 12 index.html 143, 172 inductive and capacitive loads 42 industrial controllers 41 industrial control systems 8, 120, 130, 140 industrial environments 8 Infineon 42, 159 infinite-loop condition 85 initialization value 102 initial start 14 initial test 26 Inline 112 Input 113 Input circuit 157 inputs/outputs 111 installation 12, 120, 132, 139, 141, 167 instruction list 9, 51, 53 Instruction List example 76 Internet 140 I/O board 158, 159, 162, 166 I/O pins 147 IP address 14, 165 IX0.0 41
J JavaScript function 144, 171 JavaScript-library 173 Javascript objects 145 JavaScript section 170 jump with target 115
● 189
PLC Programming with the Raspberry Pi and the OpenPLC Project
K KOP 10, 36, 37, 60
L ladder diagram 51, 52, 55, 58, 60, 72 Ladder Logic 9 Ladder Logic Example 55 lamp 37, 41 LANGUAGE_DEFAULT 33 LANGUAGE_ENGLISH 33 LED 114 LedOK 94 Less equal 87 less than or equal 81 letters 82 library 47, 53 license 19 link inputs 90 LN 87 localhost:8080 24 LOG 87 login 25 loop 84, 90 low pass 156
M machine 8 MainForm.vb 133 main menu 44 MainTask 49 Making connections 116 Mario de Sousa 7 Master-Slave principle 120 mathematical calculations 81 Matiec 7, 10, 36, 67 menu 44, 50 microcontroller 53, 76, 147 micro SD card 10, 12, 13, 14 Microsoft Visual Studio 130 minus 58, 69 MOD 87 Modbus 120 Modbus ADC 153 Modbus addresses 127 Modbus address mapping 138 Modbus client 140
● 190
Modbus commands 123 Modbus commands for the ESP8266 I/O module 178 Modbus documentation 127 Modbus ESP8266 module 166 Modbus "FC" command 144 Modbus interface 167 Modbus I/O modules 147 Modbus libraries 167 Modbus program for the Arduino 148 Modbus register 139, 152 Modbus RTU 120 Modbus RTU module 147 Modbus-RTU pin assignment 148 Modbus RTU RS485 multidrop network 158 Modbus server 26 Modbus settings 136 Modbus Slave 154 Modbus TCP 8, 120 ModbusTCP-DotNET-Master.zip 129 Modbus-TCP Master program 129 Modbus TCP module 159 Modbus TCP protocol 120 Modbus TCP Tester 129 Modicon 120 modifier 61, 62 modules 71, 72 Modulo 87 MomentaryButton 137, 138 monitoring 25, 29, 41, 79, 129, 155 monitors 120 Mono Framework 43, 130, 134, 139 motor 38, 41, 93 MotorDir 94 MotorLeft 107 MotorOff 107 MotorOn 94 MotorRight 107 MotorState 107 MUL 80 multiplexer 74HC151 166 Multiplication 80, 83, 87 my_button 37
N name 58, 69
Index
network 14, 24, 52, 55, 157 New action block 110 New branching 110 New coil 60 New connection 72, 110 New function block 60, 110 New jump 110 New power line 110 New project 50, 57, 76, 90 New variable 72 Niklaus Wirth 54 NodeMCU module 158 NOT 80, 87, 92 Notepad++ 33, 37, 74 not equal 81 NOT inverted 87
O opener 62 OpenPLC 32, 34, 36, 44, 46, 48, 57 OpenPLC Editor 32, 44 OpenPLC runtime 25 OpenPLC Runtime 23, 24, 25, 26 OpenPLC Runtime page 24 OpenPLC_Uno 148 OP LM7301 156, 159 optocoupler 41, 42, 156, 159, 179 OR 67, 80, 83, 87 output 111, 113, 124 output pins 122 overtemperature 42 overvoltage 42
P Parallel branching 118 Pascal 54, 81 password 14, 17, 25, 38 Pasting objects 50 PHP program 143, 144, 169, 170 PHP server program 168, 169 PilotLight 137 pin assignment 38, 148 PIP 167 PLCAdressClick 138 PLC Editor 136 PLC-Example-ConveyorBelt 94
PLC-Example-FBD 68 PLC-Example-FUP 68 PLC-Example-KOP 121 PLC program 9, 25, 120 PLC programming 8, 76 PLC ST-Program 28 plus symbol 97, 100, 102, 103, 104 position 93 Power Rail left and right 60 power supply rails 55 power supply socket 147 Prepare Coils 126 Prepare Registers 126 processing units (POUs) 81 process via monitors 140 process visualization 140 production lines 109 product version 46 program block 90 program comments 116 program cycle 49, 109 program flow 109 program generated by the compiler 116 program interval 49 program monitoring 129 Program Organization Unit 81 program runtime 49 program sequences 110 program step 114 program upload 164 project 33 project directory 73 project name 69 project properties, settings 46 protection mechanisms 42 proximity switch 94 Pulse Timer 89 PWM 124, 148 PWM LED 126 PWM output 38, 41, 126 PWM register 127 PyModbus 167 pymodbus_openplc_post.py 140 PythonModbus 167 python_modbus_client_tkinter_esp8266.py 167
● 191
PLC Programming with the Raspberry Pi and the OpenPLC Project
Python program Python test program
140 167
Q qualifier 112, 113 QX0.0 41
R Raspberry Pi 120 Raspberry Pi Documentation 14 Raspberry Pi Imager 12 Raspberry Pi Modbus Slave settings 154 Raspberry PI ModbusTCP server 123 Raspberry PI Monitor 128 Raspberry Pi OS (32-bit) 13 Raspberry Pi PLC 130 Raspberry Pi's IP address 21 Raspberry Pi (VNC Server) 15 Raspberry Pi Zero W 11 RaspiTestBoard 124 Read Coils 179 reading outputs 125 Real 82, 102 Reference 98, 101 register 124, 139, 180 relay 34 relay coil 56 relay contact 64 relay outputs 8 REPEAT 90 REPEAT Cycle 84 REPEAT UNTIL 54 RESET 69, 88 Reset value 80 RETURN Instruction 90 reverse-polarity protection 157 RISC CPU 159 rising edges 88 Rising Edge Trigger 89 ROL 87 ROR 87 RS 88 RS-232 120 RS-485 interface 158 RS-485 network 120 R_TRIG 88, 89
● 192
Run in terminal RUN Mode running light runtime library runtime program
142 26 26 7 38
S Save As 50 Save Changes 26 Save device 154 scan cycles 128 Schneider 120 semicolon 83, 92 sensor 120 SensorLeft 94 SensorRight 94 sequence control 8, 109 sequential function chart 9, 51, 110 Sequential Function Chart example 109 Sequential Function Chart (SFC) 55 Serial Monitor 165 server 38, 120 SET 69 settings 25, 26, 46 SFC 9, 51, 55, 109 SFC program blocks 115 SFTP client 18 SHL 87 SHR 87 simple state machine 110 SIN 87 Slave Devices 153 Small 87 software architecture 9 SPI 159 sqlquery.php 143, 172 SQRT 87 SR 88 SR flipflop 69 SSH configuration 18 ST 80 ST array type listing 100 start condition 110, 111 start screen 32, 44 start value 84 Statements 82
Index
ST code 37 step 110, 113 ST example 89, 93 ST example data type ENUM 107 ST example structure 103 ST file 27, 50 ST file listing 67 STL files 93 ST Listing 75 ST listing structure 103 ST listing with structure and arrays 106 STN 80 ST program 29 ST project save function 36 ST project (structured text) 50 Structured control language 54 structured data types 100, 103 Structured Text 9, 51 Structured Text examples 81 structure tree 44 structure variable 105 SUB 80 Substraction 87 sudo apt-get install git 22 switch 124 symbols 55, 67, 98, 109, 110 system 168 system language 33
T tabs 83 TAN 87 task settings 73 temperature 159 temperature and humidity sensor 159 Tensilica 159 terminal window 23, 30, 141, 153 test board 29, 39, 138, 181, 182, 183, 184 Test Board 40 Testing PLC programs with Modbus TCP 120 T-flip flop 110 Thiago Alves 7 time interval 113 timer 110, 137 Timer On 89 TimerReady 92
timestamp 128 time zone selection 14 TOF 89 TON 89, 91, 92 Tools 163, 165 TP 89 transistor 42, 155 transition 55, 110, 114 transition condition 113 translate 10, 35, 66, 72, 74 translation 96 translation completed 96 transmit 178, 179, 180 TRUNC 87 type 58, 69, 82
U UART 159 underscore 58, 69, 82 Unipi 1.1 8 Unix epoch 128 Unix timestamp 128 UNTIL 84 upload 10, 166 USB 11 USB ID on the Raspberry Pi 153 USB-RS485 interface 157 USB-Serial converter 164 user-defined data types 98 user interface 10, 20, 47 username 14, 17
V value 80 variable 113, 120 variable list 136 Variable names 82 version 2.52 30 version number 28 "View" menu 47 visualize processes 10 Visual Studio 43, 134 Visual Studio Community software 130 Visual Studio Installer 132 Visual Studio Toolbox 135 Visual Studio user interface 133
● 193
PLC Programming with the Raspberry Pi and the OpenPLC Project
VNC Server VNC viewer VNC Viewer credentials VNC Viewer installation VNC Viewer start menu voltage divider
15 15, 22, 141 17 15 16 42, 157, 159
W web browser 24, 140, 143, 144, 169, 170 web host 168 web interface 24, 26, 38, 41, 79, 153, 168 web page 170, 171 web server 24, 38, 140, 143, 171 Web server application 168 website 167, 172 WEP 159 WHILE 84, 90 WIFI 159 WinSCP 18, 20, 21, 22, 139, 141 WiringPI library 30 WLAN 14, 120, 158, 159, 168 working directory 162 worksheet 60, 69, 77, 111 world 140 Write 13 Write Multiple Coils 179 Write Single Coil 178 Write Single Register 180
X XOR 80, 87, 92
Z ZIP 131
● 194
books books
books
>
PLC Programming
ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266 Introduction to PLC programming with OpenPLC, the fi rst fully open source Programmable Logic Controller on the Raspberry Pi, and Modbus examples with Arduino Uno and ESP8286 PLC programming is very common in industry and home automation. This book describes how the Raspberry Pi 4 can be used as a Programmable Logic Controller. Before taking you into the programming, the author starts with the software installation on the Raspberry Pi and the PLC editor on the PC, followed by a description of the hardware. You'll then find interesting examples in the different programming languages complying with the IEC 61131-3 standard. This manual also explains in detail how to use the PLC editor and how to load and execute the programs on the Raspberry Pi. All IEC languages are explained with examples, starting with LD (Ladder Diagram) over ST (Structured Control Language) to SFC (Special Function Chart). All examples can be downloaded from the author's website. Networking gets thorough attention too. The Arduino Uno and the ESP8266 are programmed as ModbusRTU or ModbusTCP modules to get access to external peripherals, reading sensors and switching electrical loads. I/O circuits complying with the 24V industry standard may also be of interest for the reader. The book ends with an overview of commands for ST and LD. After reading the book, the reader will be able to create his own controllers with the Raspberry Pi.
Josef Bernhardt became interested in electronics at a very young age, when he built his first detector receiver, followed over the years by many other circuits. He gained his first programming experience in the 1980s with the CommodoreVC20. He became familiar with assembler programming on the 8088 processor. Josef can look back on more than 30 years of electronics and software development at University of Regensburg. With his own SMD production, he also implements electronic customer projects, always driven by the pleasure of electronics and programming.
Elektor International Media www.elektor.com
PLC Programming with the Raspberry Pi and the OpenPLC project • Josef Bernhardt
with the Raspberry Pi and the OpenPLC project
PLC Programming
with the Raspberry Pi and the OpenPLC project ModbusRTU and ModbusTCP examples with the Arduino Uno and ESP8266
Josef Bernhardt
Cover PLC ENGLISH Josef Bernhardt.indd Alle pagina's
29-10-2021 10:15