Using the Camera with CODESYS on the Raspberry Pi

This post is a primer on how to get started with using the Camera in CODESYS on the Raspberry Pi. The content of the post is taken from a mix of the CODESYS documentation and from reverse engineering sample projects.

Configuring the Raspberry Pi

Plug the Camera in to the connector labelled CAMERA on the Raspberry Pi.
Connect using SSH and run sudo raspi-config to enable the camera, then reboot
Now we need to install camera interface, these instructions are taken from the CODESYS documentation, which appears to have got them from
sudo apt-get update
sudo apt-get dist-upgrade
sudo rpi-update
At this point I rebooted again.
Clone the code from github and run the installer with the following commands:
git clone
cd RPi_Cam_Web_Interface
chmod u+x
./ install
When running the installer you get prompted for th name of the install folder – I accepted the default. You also get prompted for the Port to use, I stayed with the default port 80. I chose not to enable web server security. I then rebooted.
Then I browsed to (the IP address  of my RPi) to verify that everything was working.

Capturing a JPG image

Create a new CODESYS project (standard project) selecting the target device as CODESYS Control for Raspberry Pi. Beneath the Camera device right click over the <Empty> device, select plug device
and then select Raspberry Pi Camera, close the dialog.
You should end up with a Camera in the devices tree like this.
You also have a new variable in IoConfig_Globals called Raspberry_Pi_Camera of type Camera.PiCamera. The methods on this function block can be used to save a picture, or video. For documentation check out the online help in CODESYS.
At a minimum you need to specify the name of an output file, the sample below can be driven using the CODESYS debugger, set xTakeStill TRUE and a picture will be saved.
NOTE: On my system when I originally ran this udiResult was set to 70, and no picture was taken. I had to stop the Camera in the RPi Cam Control Application as warned in the CODESYS documentation, by browsing to the IP address of my Pi and selecting Stop Camera
Once I had stopped the Camera the function returned 0 (Errors.ERR_OK), and the picture was taken. Using the device file browser I was able to locate the Picture at the root of the CODESYS file system on my RPi.
NOTE: The function call Raspberry_Pi_Camera.Still takes a long time to execute, by default around 5 seconds. During this time the Program is hung. If you need to make use of this code I strongly suggest having a dedicated, low priority task to execute a program dedicated to taking the picture. I experimented with a timeout of 0, and the program hung for longer than I was prepared to wait. A timeout of 1 still took a long time to complete.

Getting started with CODESYS visualisation on the Raspberry Pi

This post is intended for those who have never generated any graphics using CODESYS.
CODESYS has a built in fully integrated visualization system. 
I have started by creating a very simple new CODESYS program using ST as the programming language.
Next step is to add a blank visualization as shown below
I name the new visualization Demo.
Adding the Demo visualisation object actually causes three objects to be added to the application.

Before progressing to actually create the visualisation, let’s have a quick look at each of these objects.

Visualization Manager

The Visualization Manager holds global settings such as the initial language, User Management, Hot Keys and language specific fonts. The Visualisation Manager provides a home to add visualization devices underneath. CODEYS supports a number of different visualization device types, the Raspberry Pi adaptation of CODESYS only supports WebVisualization and CODESYS HMI, we will only concern ourselves with WebVisualization in this post.

Web Visualization

The WebVisualization object adds a web server to the application. It also specifies which Visualisation to show when a browser first connects to a specified URL. The defaults are shown below.
Scaling Options are to do with how the visualisation is changed if the size of the window showing them changes. I personally find that the visualisations look best with a Fixed scaling. As I will be testing the visualisations using computers with keyboards and not touchscreen I also change the Default test input to Keyboard.
NOTE: It is possible to have multiple Web Visualization objects each with distinct URLs that can be mapped to different Visualizations. It is equally possible to have multiple Visualizations and access them all through a single URL.

Vizualisation (Demo)

In simplistic terms this object represents a ‘Page’ on which we can add Visualization Elements. These Elements can range from simple lines and rectangles to complex objects such as gauges, alarm tables etc. Visualizations can also be embedded in a Frame element on another Visualisation, or even used to define custom keyboards or dialogs.

Demo Visualization

To create our simple application we will add a Meter to out Demo Visualization and get it to indicate the value of the VAR a in the simple program. Double click on the Demo object in the Devices tree to open up the Visualization. Find the Meter Element under Measurement Controls in the Toolbox.
Then Left Click and drag a Meter from the Toolbox onto the top left of the Visualisation area.
At tis point we have a Visualisation including a Meter element, but it is not associated with any variable in our application, so next we must associate a variable from the program with the Meter. In the Properties box with the Meter still selected click in the Value column against the Value Property, click again and then click the ellipsis. 
This will bring up the Input Assistant, select Application->PLC_PRG->a and click OK.
That’s it, we should now be able to go online and verify that our application runs and the visualisation is working. So select Online->Login from the main CODESYS menu, and Login with download. At this pint the Demo Visualization shows as message stating The online visualization is waiting for a connection. Please start the application. Start the application running (Debug->Start, or F5) and the Meter should start to move rapidly.
We should also be able to see the same in a web browser. To do this you need to know the IP address of the Raspberry Pi that you are using. If you can see the console it may have a message that stays something like My IP address is, or in CODESYS open the Device object, open the log tab, click the refresh button (shown below), scroll down through the log messages and the IP address will be shown.
or on the Raspberry Pi console type ifconfig, the ip address is shown as inet addr:
The URL to access the Web Visualisation is, the captures below show how the meter looks in Internet Explorer and on the browser on the Raspberry Pi.