Programming Wiren Board Controller using CODESYS
General Information
CODESYS is an industrial automation software suite that allows writing programs in the IEC 61131-3 languages: FBD, ST, LD, SFC.
To use with the Wiren Board controller, you need to purchase the necessary licenses, otherwise you will only be able to work in demo mode, which is relevant only for familiarization with the product.
In demo mode, the software operates without a license for two hours. A manual restart is required afterward. The license is for a single device: it can be used on the target device / PLC where the CODESYS runtime system is installed.
What licenses are required? There are several options for using the CODESYS executive system on the Wiren Board controller:
- Implementation with access to the controller's MQTT broker. In this case, there will be maximum integration with the controller, because all controller services communicate with each other via a common "bus", which is the MQTT broker. The following licenses are required:
- basic license CODESYS Control Basic S or higher. Select depending on additional system requirements;
- package CODESYS IIoT Libraries SL.
- Implementation using the Modbus RTU protocol. In this case, there will be integration with the controller only for working with the RS-485 bus. The following license is required:
- basic license CODESYS Control Basic M or higher. Select depending on additional system requirements.
CODESYS is a third-party product, for technical support please contact the software manufacturer.
Installation
The process of installing the development environment and necessary packages:
- Download CODESYS Development System V3, install, and run it.
- Go to the Tools menu → CODESYS Installer. Close CODESYS Development System V3.
- In the Add-ons panel, go to the Browse tab. Enter CODESYS Control for Linux ARM in the search bar and select the package CODESYS Control for Linux ARM64 SL (for Wiren Board 8) or CODESYS Control for Linux ARM SL (for Wiren Board 6, 7) for installation. Dependencies will be automatically selected: CODESYS Control SL Deploy Tool, CODESYS Edge Gateway for Linux, CODESYS Control SL Extension Package.
- Click the Install Selected button. In the pop-up window, confirm the action — OK. Then accept the license agreement — Continue.
- Installing packages to work with the Wiren Board controller
To install the runtime system on the Wiren Board controller:
- Start CODESYS Development System V3.
- Go to the Tools menu → Update Linux ARM64 for WB8. For WB6 and WB7, choose Update Linux ARM.
- In the opened Linux ARM64 panel, enter:
- User name — root,
- Password — ssh password,
- IP address — controller's IP address.
- Click the Install button.
- Setup the runtime environment on the Wiren Board controller
The development environment is configured, the runtime system is installed on the controller, and you can move on to development.
Creating a project
The project creation process:
- Select File → New Project from the menu.
- In the project settings specify:
- Device — CODESYS Control for Linux ARM64 SL for WB8. For WB6 and WB7 select CODESYS Control for Linux ARM SL.
- PLC_PRG in — the preferred development language.
- Go to Devices. Double-click to select Devices (CODESYS Control for Linux ARM64 SL). Go to the Scan Network tab and select the controller.
- Authorize.
- Project Creation
Example of working with MQTT
Let's create a minimal project to demonstrate the operation of Wiren Board controller with CODESYS. Suppose we need to control turning on/off the load using a non-locking button. Pressing the first button should turn on the load, pressing the second one should turn it off. We will control the load using the WB-MR6C v.3 module. For programming, we will use the FBD language.
Preparation:
- Run the CODESYS Installer and install IIoT Libraries SL.
- Add the MQTT Client SL library.
- If there are missing libraries, download them.
- Add a new Action object to the project.
- Creat a Project
Variable definition:
PROGRAM PLC_PRG
VAR
MQTTClient: MQTT.MQTTClient;
MQTTPub_K1: MQTT.MQTTPublish;
MQTTSub_In1: MQTT.MQTTSubscribe;
MQTTSub_In2: MQTT.MQTTSubscribe;
//MQTT Client
xEnable : BOOL;
wsUsername : WSTRING;
wsPassword : WSTRING;
uiPort : UINT := 1883;
eMQTTVersion : MQTT.MQTT_VERSION := MQTT.MQTT_VERSION.V3_1_1;
sHostname : STRING(255) := 'localhost';
//MQTT Publisher
xPublish_K1 : BOOL;
sPayload_K1 : STRING;
wsTopic_K1 : WSTRING(1024) := "/devices/wb-mr6cv3_1/controls/K1/on";
//MQTT Subscriber
xSubscribe : BOOL;
sPayloadSub_In1 : STRING;
sPayloadSub_In2 : STRING;
wsTopicSub_In1 : WSTRING(1024) := "/devices/wb-mr6cv3_1/controls/Input 1";
wsTopicSub_In2 : WSTRING(1024) := "/devices/wb-mr6cv3_1/controls/Input 2";
// MAIN ROUNTINE
R_TRIG_On: R_TRIG;
R_TRIG_Off: R_TRIG;
RS_On: RS;
RS_Off: RS;
END_VAR
Setup MQTT client:
Control algorithm:
In this example, we subscribe to the topics of the module's inputs: "/devices/wb-mr6cv3_1/controls/Input 1" and "/devices/wb-mr6cv3_1/controls/Input 2", and after the algorithm execution, we publish a message about turning on/off the load to the topic "/devices/wb-mr6cv3_1/controls/K1/on".
Operation logic:
- Closing input 1 leads to the closure of relay K1.
- Closing input 2 leads to the opening of relay K1.
Example of working via Modbus RTU
Let's set the same task as in the example above with the MQTT client.
Preparation
To ensure the correct operation of the ports, they need to be configured. In the configuration file /etc/codesyscontrol/CODESYSControl.cfg, specify the ports:
[SysCom]
Linux.Devicefile.1=/dev/ttyRS485-1
Linux.Devicefile.2=/dev/ttyRS485-2
By default, the controller ports are busy and operate through their wb-mqtt-serial driver, so the ports used in the CODESYS runtime system must be disabled through the controller's web interface.
Restart services:
systemctl restart codesyscontrol.service
systemctl restart codesysedge.service
Add Modbus Device:
- Add Modbus COM device. Specify port parameters.
- Add Modbus-master (Client) device.
- Add Modbus-slave (Server) device. Specify device address.
- Add necessary channels. Associate channels with program variables.
- Add Modbus Device
Control Algorithm:
In this example, we read the input states from the Discrete input registers (Input 1 and Input 2) and after the algorithm execution, we write the necessary state of relay K1 to the Coil register (Channel 1).
Operation logic:
- Closing input 1 leads to the closure of relay K1.
- Closing input 2 leads to the opening of relay K1.
Register map of the used module.
Useful links
- CODESYS Website
- Topic on the support portal for IEC 61131-3, ST, CoDeSys & Co
- Project examples: WB_CODESYS.zip