Programming Wiren Board Controller using CODESYS

From Wiren Board
This is the approved revision of this page, as well as being the most recent.
Other languages:
CODESYS and Wiren Board

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:

  1. 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:
  2. 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:

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:

  1. Download CODESYS Development System V3, install, and run it.
  2. Go to the Tools menu → CODESYS Installer. Close CODESYS Development System V3.
  3. 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.
  4. Click the Install Selected button. In the pop-up window, confirm the action — OK. Then accept the license agreement — Continue.

To install the runtime system on the Wiren Board controller:

  1. Start CODESYS Development System V3.
  2. Go to the Tools menu → Update Linux ARM64 for WB8. For WB6 and WB7, choose Update Linux ARM.
  3. In the opened Linux ARM64 panel, enter:
    • User name — root,
    • Password — ssh password,
    • IP address — controller's IP address.
  4. Click the Install button.

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:

  1. Select FileNew Project from the menu.
  2. In the project settings specify:
    • DeviceCODESYS 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.
  3. Go to Devices. Double-click to select Devices (CODESYS Control for Linux ARM64 SL). Go to the Scan Network tab and select the controller.
  4. Authorize.

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:

  1. Run the CODESYS Installer and install IIoT Libraries SL.
  2. Add the MQTT Client SL library.
  3. If there are missing libraries, download them.
  4. Add a new Action object to the 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:

MQTT client

Control algorithm:

FBD program

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:

  1. Add Modbus COM device. Specify port parameters.
  2. Add Modbus-master (Client) device.
  3. Add Modbus-slave (Server) device. Specify device address.
  4. Add necessary channels. Associate channels with program variables.

Control Algorithm:

FBD Program

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