# Step 2 - Distribute 4DIAC applications

This page is part of a guide that gives a walkthrough over the major 4DIAC features.

0. Overview
1. Use 4DIAC locally
2. Create a distributed application (YOU ARE HERE!)
3. Deploy remotely
4. Create your own Function Blocks
5. Other basic features
6. Advanced Features [optional]

This guide adds a Start and Stop control to the Blinking application created in Step 1. Normally you would put two buttons that would serve as inputs to the PLC and control the Blink application. In this following example, it will be shown how to detach the buttons from the same Hardware and make them run separately. For now, the example still uses the local computer. But the Blink application will run on a FORTE instance and the input buttons will run on a FBRT instance (if you don't know what it is or you've forgotten, here the quick access). The two different program running on the local machine emulate two PLCs. See the architecture below.

## Add another device to System Configuration

First, add a new device to the System Configuration. Go to it, and Drag and Drop a FBRT_WINDOW (1) next to the FORTE_PC, and connect it to the Ethernet sement. It already comes with a resource called PANEL_RESOURCE. The MGR_ID (2) is very important to change, since by default it has the same IP:PORT as the FORTE_PC. Change it to localhost:61505. This means that the FBRT will run locally (localhost) and will listen at the port 61505, a different one from the 61499 from FORTE_PC.

## Add buttons FBs to the Blink application

Back in the Application, because FBRT is written in Java and supports graphical interface and other FBs, there's a folder called FBRT in the Palette that contains special FBs that only work on it and not in FORTE. In a Subfolder is the hmi folder and inside of it you can find the IN_EVENT FB.

1. Drag two of them at the left of the blink application. Change the names to START_BUTTON and STOP_BUTTON for better reference.
2. Set the QI of both FBs to 1, and in the LABEL input data, set to the text that you want to see on them. We'll use START and STOP. The 1 in QI means that when an INIT event arrives, the FB will initialize, and if it was zero it would have been de-initialized.
3. Connect the IND event of the START_BUTTON to the START input event of E_CYCLE, and similarly for the STOP_BUTTON and the STOP event. Also, connect the INITO of START_BUTTON to the INIT of STOP_BUTTON. See the image below.
4. Select both FBs, and map them to the resource of the FBRT_WINDOW device that you added before.

You can see the stripped line instead of the solid that connect FBs that run in diffent devices. This means that the event doesn't go directly. Since the communication of these events is not implicit, we must complete the application in both devices to handle this broken connections.

## Fix broken connections between devices

1. Go to the System Configuration, and double click on the Resource of the FBRT_WINDOW to access the Resource. You see here the two functions block that you added to the application and START FB that comes with the resource.
2. First, connect the COLD and WARM events from the START FB to the INIT event of START_BUTTON. (The order they're initialized is the order they appear in the GUI)
3. Add two PUBLISH_0 FBs from the Palette in the same Resource view. Call them PUBLISH_START and PUBLISH_STOP.
4. Keep the flow of the INIT events by connecting the INITO to the next INIT.
5. Set QI of both PUBLISH to 1.
6. Connect the IND output event of START_BUTTON to the REQ of a PUBLISH_START. Do the same for the other pair of FBs.
7. Set the ID input data of START_BUTTON to 239.0.0.1:61000 and the ID of PUBLISH_STOP to 239.0.0.1:61001.

Here's what the last steps means. Every time you click the button in the FBRT, an IND event is generated. The PUBLISH_X FBs are used to send messages throught the network. Every time a REQ is triggered, a message is sent according to the ID. In the ID you can specify if you want to use a specific network protocol like MQTT, OPC, etc. If you don't specify anything, the default defined in the Compliance Profile is used. The number X in PUBLISH_X is the number of data that you want to send as message, and since we are sending only an event here, and no data we used PUBLISH_0.

The selected ID specifies an IP:PORT pair. The used Compliance Profile says that we should use UDP sockets in a certain range. The selected 239.0.0.1 and port 61000/1 have nothing to do with the localhost:61499 or localhost:61505 used for the device. The first are a UDP connections used to send events between devices, and the latter are TCP connections to connect to 4DIAC to upload applications and manage them. Take care of not using the same pair for different UDP sockets.

## Fix broken connections in the other resource

1. Go to the System Configuration, and double click on the Resource of the FORTE_PC to access the Resource. You see here the blink application as before.
2. First, remove the COLD and WARM events from the START of E_CYCLE, since this will be triggered by the buttons.
3. Add two SUBSCRIBE_0 FBs from the Palette in the same Resource view. Call them SUBSCRIBE_START and SUBSCRIBE_STOP.
4. Connect the WARM and COLD from the START FB to the INIT of SUBSCRIBE_START, and then continue the flow to SUBSCRIBE_STOP.
5. Set QI of both SUBCRIBE to 1.
6. Connect the IND output event of SUBSCRIBE_START to the START of E_CYCLE. Do the same for the other pair of FBs.
7. Set the ID input data of SUBSCRIBE_START to 239.0.0.1:61000 and the ID of SUBSCRIBE_STOP to 239.0.0.1:61001.

This is like the mirror of the step before. Each subscriber listen to a specific UDP IP:PORT. These are the same as used in the GUI. This means that when you press the START button, an event will be sent to a specific IP:PORT using UDP, and since the Blinking device is listening to the same IP:PORT, it will get the event, triggering in the application.

## Deploy both devices

Similar on how it was done to deploy the Blink application is done for two. The only difference is that in this case we launch 1 FORTE instance and 1 FBRT instance in the Deployment Perspective. First set Windows → Preferences → 4DIAC → FBRT Preferences to the fbrt.jar file located under runtime/FBRT in your 4DIAC folder.

1. Select both devices in the Donwload Selection
2. Launch one FORTE instance. The port should be one specified in the System Configuration (has nothing to do with the IP:PORT used with UDP to send events)
3. Launch one FBRT instance. The same care must be taken regarding the port. This will open a GUI frame.
4. Download the application
5. Check that you don't get any error.
6. You should see the buttons in the GUI.

## Test it!

Do the debugging similar on how it was done in the Blink example. You should see the blinking only after you pressed the start button, and it should stop when pressing the other button. Attention!: You cannot monitor FBRT, only FORTE, because it's a feature only present on it.

# Where to go from here?

In the next step you will see how FORTE runs in another machine.

Step 3 - Deploy remotely

If you want to go back to the original Blinking application without buttons, here's a link

Step 1 - Use 4DIAC-IDE locally

If you want to go back to the Start Here page, we leave you here a fast access

Start Here page

Go to top