Saturday, February 16, 2019

Simple ICS Lab - Part 4 - OpenPLC


It is time to turn the Rapberry Pi 3 into a PLC. This will be accomplished by installing OpenPLC.  check out the OpenPLC web site to learn more about the project: http://www.openplcproject.com. A couple of things are going to be needed to successfully follow along.  The Lubuntu ICS VM and a running RPi will be needed to complete the tasks below.



Installing OpenPLC

  • If the Lubuntu ICS VM is not already on, start it up.
  • Log in if needed so that the desktop is displayed.
  • Open up LXTerminal by clicking on the menu button in the lower right of the desktop, selecting "System Tools", and then clicking on "LXTerminal".  Or open it using the desktop shortcut if you've set it up.
  • Next connect to the RPi using SSH by entering the command below replacing x.x.x.x with the IP address of your RPi. (Refer to the nmap section in "Simple ICS Lab Part 2" to find your RPi's IP address if needed.)
    • ssh -l pi x.x.x.x
  • Below is an example of the command I run to connect using my RPi's IP address.
    • ssh -l pi 192.168.0.33
  • You should be prompted for the password of the 'pi' user account. If you have not changed the default password enter raspberry which will then bring you to a command prompt on the RPi.

  • Now is a good time to change the default password of the 'pi' user account. As noted in the displayed message type the following to set a new password. Follow the prompts to set a new password.
    • passwd

  • Update the software on the RPi using the command below.
    • sudo apt-get update && sudo apt-get -y upgrade
  • The update may take a little while, but you should eventually be returned to the command prompt.

  • Next install GIT with the following command:
    • sudo apt-get -y install git
  • Again, you will be returned to a command prompt once the installation is finished.
  • With GIT now installed we can get OpenPLC by entering the following at the command prompt:
    • git clone https://github.com/thiagoralves/OpenPLC_v3.git

  • Now we can install the RPi specific parts using the following commands:
    • cd OpenPLC_v3
    • ./install.sh rpi
  • Have patience as this will take a long time to complete.

  • When the script completes you will be returned to a command prompt.

  • Reboot the RPi to ensure OpenPLC runs properly at startup.  Enter the following at the command prompt and press "Enter"
    • sudo reboot

  • You will need to wait a few minutes for the RPi to reboot.
  • After waiting, attempt to connect by opening a web browser and going to the OpenPLC web page replacing x.x.x.x with the IP address of your RPi:
    • http://x.x.x.x:8080

  • Enter openplc for the username and also openplc for the password then click the LOGIN button.
  • We need to change the default user's password.  Click on "Users" at the left.

  • Click on the row containing "OpenPLC User".

  • Update the password and then click on "Save changes".

  • To enable the RPi driver click on "Hardware" at the left and then select "Raspberry Pi" from the drop-down menu.

  • Scroll to the bottom of the page and click the "Save changes" button.

  • A new page will display showing the progress of compiling for RPi.  Once you see "Compilation finished successfully!" near the bottom of the output, you can click the "Go to Dashboard" button.

Testing OpenPLC

Finally we are ready to test out the OpenPLC installation. To do this we are going to need to get out some electronic components.
(The links above are for examples of the components.  They are not affiliate links.  I don't receive any money if you buy anything.)

Many of these parts are available in various RPi starter kits.  You need the components listed above and could use other methods to make connections with jumper wires or ribbon cables, depending on the kit.

With the RPi and all parts available, make the connections as shown and detailed below.




Part Connection
RPi Pin 1 Breadboard +
RPi Pin 6 Breadboard -
RPi Pin 7 Breadboard a7
RPi Pin 8 Breadboard a17
10k Leg 1 Breadboard +
10k Leg 2 Breadboard b5
330 Leg 1 Breadboard -
330 Leg 2 Breadboard c20
Switch Leg 1 Breadboard e5
Switch Leg 2 Breadboard e7
Red LED + Breadboard e17
Red LED - Breadboard e20

Here is a real-life picture of the boards and connections:


With the RPi and components connected up we will now test the Hello_World.st project.

  • Return to the Lubuntu ICS VM and go to the OpenPLC interface in the web browser.
    • http://x.x.x.x:8080
  • Login if necessary.
  • Click on "Programs" at the left, then click the "Browse" button.


  • In the "File Upload" dialog click "Home" on the left and then double-click on "PLCopen Editor" in the middle.

  • Scroll down if necessary and click on "Hello_World.st".  Then click the "Open" button.

  • Once back at the web page click on the "Upload Program" button.

(If you get returned to the login screen, enter the username and password to return to the Dashboard.  I had this happen to me.  Attempt to browse again to the program and then upload.)
  • On the "Program Info" page enter appropriate text into each dialog.  For the "Hello_World.st" project it can be:
    • Name: Hello World
    • Description: Basic example
  • Click "Upload program".

  • The project will be compiled and you will see a page displaying output until "Compilation finished successfully!" appears.
  • Click the "Go to Dashboard" button.

  • You may notice that the dashboard indicates a status of "Stopped".  You should also see at the top of the screen "Stopped: Hello World".  Our project file is compiled and loaded, but OpenPLC is not actively running.
  • Click on the "Start PLC" button.

  • A number of updates should occur after OpenPLC begins to run the project.
  • "Running: Hello World" should display at the top along with the status now indicating "Running".
  • The "Runtime Logs" should have populated with updated entries.
    • Notice that both Modbus and DNP3 are in use.
  • Finally, you should see that the "Start PLC" button is now a "Stop PLC" button.

  • Check that your RPi running OpenPLC is functioning correctly with your electronic components now.
  • Momentarily press the Push-Button Switch and release it.
    • You should see your LED light up and remain lit for 2 seconds.
This completes our testing of OpenPLC on the RPi.

Before we finish entirely let's adjust the OpenPLC settings so that it runs when the RPi starts.  This prevents us from having to visit the Dashboard each time to start the PLC.
  • Click on "Settings" at the left.
  • On the "Settings" page, click the box next to "Start OpenPLC in RUN mode" and then click the "Save Changes" button.

Recommended - Create a PLCOpen Editor Shortcut
  • Open LXTerminal in the Lubuntu ICS VM.
  • Enter the following at the command prompt:
    • lxshortcut -o ~/Desktop/PLCOpen\ Editor



    • A new window should appear titled "File Properties".
    • There should now also be a new icon on the Desktop.

    • Click on the gear icon image.  This will open up a "Choose An Icon..." window.
    • Select "image files".
    • Click on "icsuser" at the left.
    • Double-click on "PLCopen Editor" near the middle.

    • Scroll down and double-click on "images".

    • Click on "aboutlogo.png" and then click the "OK" button.

    • Click on the "Desktop Entry" tab within the "File Properties" window.
    • Enter the following into the "Command:" box:
      • python /home/icsuser/PLCopen\ Editor/PLCOpenEditor.py
    • Click the "OK" button.

    • Back at the desktop, double-click the new PLCOpen Editor icon and verify it starts the program.


    Troubleshooting
    If you had any trouble, review your connections between the RPi and electronic components.  There are a number of beginner electronics web sites you can seek help from if needed.  Try the friendly folks at Adafruit or SparkFun.  Also great places to get an RPi and components.

    Search terms that may help when troubleshooting or learning:
    • How to use a breadboard
    • How to connect an LED
    • How to connect a tactile switch
    • How to connect an LED with a tactile switch
    Spend some time reviewing YouTube video results as well as web pages and you will be on your way to success!

    References

    https://www.sparkfun.com/
    https://www.jameco.com
    https://www.digikey.com/