Setting-up OpenPLC

Setting-up OpenPLC

The OpenPLC is a opensource Programmable Logic Controllers (PLC) alternative.
Due to this, it is possible to understand the black box of these systems.
It could be easily used with a Raspberry Pi.

Install Raspbian

Download the current Raspbian Lite Image from https://www.raspberrypi.org/downloads/raspbian/.
Unzip the Image and copy it to the SD card:

$ unzip 2017-09-07-raspbian-stretch-lite.zip
$ sudo dd if=2017-09-07-raspbian-stretch-lite.img of=/dev/mmcblk0 bs=4M

Install Wiringpi

$ sudo apt-get update
$ sudo apt-get dist-upgrade
$ sudo apt-get install git-core

Clone the GIT and build the WiringPi Software

$ git clone git://git.drogon.net/wiringPi
$ cd ~/wiringPi
$ ./build

Test the output with WiringPi.
With the write command the output could be set to high or low.

$ gpio mode 4 out
$ gpio write 4 1
$ gpio write 4 0

Pin Layout of the Raspberry Pi.

e         +-----+
d         | o o <-  GND
g         | o o |
e         | o o |
          | o o |
o         | o o |
f         | o o |
          | o o |
R         | o o |
a         | o o |
s         | o o |
p         | o o |
b         | o o |
e  WiPi4 -> o o |
r         | o o |
r         | o o |
y         | o o |
          | o o |
P         | o o |
i         | o o |
          | o o |
          +-----+

Install OpenPLC on the Raspberry Pi.

$ sudo apt-get install build-essential pkg-config bison flex
$ sudo apt-get install autoconf automake libtool make nodejs git
$ cd 
$ git clone https://github.com/thiagoralves/OpenPLC_v2.git

Change into the OpenPLC folder and build it.
This will take some time.

$ cd OpenPLC_v2
$ ./build.sh

Enable DNP3 on the controller.

OpenPLC can talk Modbus/TCP and DNP3 SCADA protocols. Modbus/TCP is already
added to the system. Do you want to add support for DNP3 as well (Y/N)?
Y

Select the device, where to run OpenPLC.
In this case RaspberryPi (4).

The OpenPLC needs a driver to be able to control physical or virtual hardware.
Please select the driver you would like to use:
1) Blank                  5) UniPi                 9) Arduino+RaspberryPi
2) Modbus                 6) PiXtend              10) Simulink
3) Fischertechnik         7) Arduino
4) RaspberryPi            8) ESP8266
#? 4

Add OpenPLC to rc.local to start it at boot up.

$ sudo nano /etc/rc.local

Add the following two lines before exit 0 in the rc.local.

cd /home/pi/OpenPLC_v2
su root -c 'nodejs server.js > /dev/null &'

Reboot the Raspberry Pi.

ยง sudo reboot

Open a web browser on :8080 to view the fronted of the OpenPLC.

Now it is time to upload the first Structured Text program to the PLC.
There is also the possibility to use the OpenPLC Editor http://www.openplcproject.com/plcopen-editor.

PROGRAM My_Program
  VAR
    button AT %IX0.0 : BOOL := 0;
    output AT %M0.0 : BOOL := 0;
    out2 AT %QX0.2 : BOOL := 0;
  END_VAR
  VAR
    NOT9_OUT : BOOL;
  END_VAR

  IF output = 1 THEN
    output := 0;
  else
    output := 1;
  END_IF;

  out2 := output;
END_PROGRAM


CONFIGURATION Config0

  RESOURCE Res0 ON PLC
    TASK TaskMain(INTERVAL := T#50ms,PRIORITY := 0);
    PROGRAM Inst0 WITH TaskMain : My_Program;
  END_RESOURCE
END_CONFIGURATION

Pin Layout of the Raspberry Pi.

e         +-----+
d  QX1.2 -> o o <-  GND
g  QX1.1 -> o o |
e  QX1.0 -> o o |
   GND   -> o o |
o  QX0.7 -> o o |
f  GND   -> o o |
   N/A   -> o o |
R  QX0.6 -> o o |
a  QX0.5 -> o o |
s  QX0.4 -> o o |
p  GND   -> o o |
b  QX0.3 -> o o |
e  QX0.2 -> o o |
r  GND   -> o o |
r  QW0   -> o o |
y  QX0.1 -> o o |
   QX0.0 -> o o |
P  GND   -> o o |
i  5V    -> o o |
   5V    -> o o |
          +-----+

After the upload the program will be compiled.
If everything is fine, there should be the output: compiled without errors.

Acknowledgement

Leave a Reply

Your email address will not be published. Required fields are marked *

*