Skip to main content
All CollectionsConnect your Devices
Connect a WAGO PFC200 to Ubidots using Codesys
Connect a WAGO PFC200 to Ubidots using Codesys

This technical guide explores the required steps to connect your WAGO PFC200 to Ubidots by using Codesys.

Sergio M avatar
Written by Sergio M
Updated over a week ago

Requirements

1. Ubidots setup

  • Head to the "Devices" section your Ubidots account.

  • Click on the "+" symbol to create the device which will receive the data.

  • Select "Blank Device".

  • Set a device name according to your requirements. Then, the device label will be set automatically.

  • You should see the new device with no variables for the moment.

  • Click on the newly created device.

  • Copy the device's API Label, ID, and Token to the clipboard. You'll need them later.

  • Since security is important, you'd want TLS encryption to secure messages going to the cloud. Head to the following link; there you'll find the Ubidots PEM certificate.

  • Copy all the content to a text editor and save it as "root.crt".

2. PFC200 setup

  • Log in to the WBM of your PFC200 by using any web browser of your choice. Type the IP address of your PFC200 controller on the search bar.

  • If prompted for credentials, type in the default ones:

    • Username: admin

    • Password: wago

  • Make sure that the firmware on your PFC200 is 04.01.10(23) or above.

  • Head to the Configuration tab and to configure the device for outbound connections by setting a DNS as follows:

  • Check that the clock on the PFC200 is formatted to the correct timezone.

  • Verify that you can access the PFC200 by starting a SSH session to the device. Use the same IP address and credentials as above.

  • From within the device, make a ping to www.google.com in order to check internet connectivity status. If a response with the connection statistics is received, the device is correctly configured. The SSH session can now be finished.

  • Go to the Cloud Connectivity section under the Configuration tab and then select Connection 1. Edit the following settings as the GIF shows:

    • On Cloud platform select "MQTT Anycloud".

    • Set the hostname to industrial.api.ubidots.com

    • Click the TLS checkbox and set the port number to 8883.

    • Input the Ubidots device ID obtained earlier into the Cliend ID field.

    • Input the FULL Ubidots Token into the User field.

    • Leave the Password field empty.

    • In the Data protocol setting, select Native MQTT.

  • Enable FTP via the Ports and Services tab.

  • Insert the root.crt certificate in the path /etc/ssl/certs/ within the WAGO controller. You can do that by FTP into the WAGO or by using SCP.

  • Hit the "Submit" button and then reboot the WAGO PFC200 Controller by hitting the "Reboot" button at the top right corner of the WBM.

  • After the reboot is finished, visit the "Connection Status" page on the WBM. Then, you should now see the connection running.

3. Codesys setup

  • Head to the WAGO website to download the newest version of Codesys. Once you have downloaded, install it and launch it.

  • Create a new project, select Empty Project and give it a name.

  • Add the WAGO PFC200 to the project by right clicking on the recently created project and selecting Add Device option:

  • Search the PFC200 WAGO Controller and then click Add Device.

Add-Device-in-Codesys
  • Once added, the project will look like this:

  • Create a POU by right clicking the application option on your project, then select Add Object → POU. Name it as Main.

  • Create a Task Configuration by clicking on application → Add Object → Task Configuration. Leave the default name for this and then click the Add button.

  • Click the Add Call button and add Main on the popping window.

POU-Task-Config
  • Notice that your project has two new tabs:

    • Main

    • Task

  • Set up a Gateway and attempt to go online by clicking on "Online" option  "Login". Codesys will prompt you to create an "Active path".

  • Scan the network by clicking the "Scan Network" button and select the PFC200. When prompted for a username and a password, use the credentials mentioned earlier in this guide. Now the device is online.

  • Go back offline and then go to the Library Manager.

  • Click on "Add Library" and install WagoAppCloud and WagoAppJSON.

4. MQTT setup

  • On the project explorer, select the FbPublishMQTT_2 function block.

  • The following is the description of the FbPublishMQTT_2 function block:

    • sTopic is a string representing the MQTT topic: /v2.0/devices/{DEVICE_LABEL}’

    • eQualityOfService indicates the amount of times a message will be published to the MQTT broker (Ubidots' broker in this case). Set it to 1.

    • dwSize is a dynamic variable that stores the length in bytes of the message string that will be sent to the broker.

    • aData is the pointer of the JSON payload data to be published to the broker. The data must be copied over from the JSON string to this aData array.

    • xTrigger will act as the trigger to begin publishing data.

    • xBusy, xError and oStatus are troubleshooting indicators.

  • Create a timer to act as the publishing interval, which, in this case, is every 1 second.

  • Copy the following code to the project's code editor.

TimerOn(IN := TRUE, PT := T#1S);
sPayload := '{"var1": 23, "var2": 46, "var3": 1}';

IF TimerOn.Q THEN
xTrigger := TRUE;
TimerOn(IN := FALSE);

IF NOT oFbPublishMQTT_2.xBusy THEN

// Copy payload string
dwBytesCount := Length(sPayload);
MemCopy(pDest := ADR(aBuffer), pSource := ADR(sPayload), udiSize := dwBytesCount);

// Trigger the transmission
xTrigger := TRUE;
ELSE
// Busy statistics counter
dwBusyCounter := dwBusyCounter + 1;
END_IF

IF oFbPublishMQTT_2.xError THEN
// Error statistics counter
dwErrorCounter := dwErrorCounter + 1;
END_IF

END_IF

oFbPublishMqtt_2(sTopic:= '/v2.0/devices/joes_test',
eQualityOfService:= 1,
dwSize := dwBytesCount,
aData := aBuffer,
xTrigger := xTrigger);
  • Create the program that will initialize the MQTT publish function block. Copying the following code to the project's code editor will do so:

PROGRAM Main
VAR
oFbPublishMQTT_2 : WagoAppCloud.FbPublishMQTT_2(eConnection := eConnectionId.Connection1);
aBuffer : ARRAY[0..1999] OF BYTE;
dwBytesCount : DWORD;
sPayload : STRING(1024);
xTrigger : BOOL;
TimerOn: TON;
dwBusyCounter: DWORD;
dwErrorCounter: DWORD;
END_VAR
  • At the end, it should look like this:

  • Next is to create the JSON message that will be published. To do so, you'll need the Fb_JSON_Writer_02 function block from the WagoAppJSON library previously installed. The following is the description of the components of this function block:

    • sJSON_BaseFrame will house the template of the JSON string. Any dynamic variables will have the placeholder ‘#Parameter’ within the template string.

    • aParameterValues is an array of all variables that are represented in the template string as #Parameter. Since this array is a string, you’ll need to convert your values into strings before putting them into the array.

    • xTrigger will cause the JSON function block to trigger its execution and create a JSON string represented by the string template and value array.

    • sOutput will be the final JSON string result.

  • To create the JSON message payload, it’s important to note that the type is a string with the format of quotes around the variable name and nothing around the value, so follow this format:

‘{“var_name_1”: var_1_val, “var_name_2”: var_2_val}’
  • With that in mind, the variable declaration which composes the setup code for MQTT publish and JSON writer is as shown below:

PROGRAM Main
VAR

//Variables for MQTT publish
oFbPublishMQTT_2 : WagoAppCloud.FbPublishMQTT_2(eConnection := eConnectionId.Connection1);
aBuffer : ARRAY[0..1999] OF BYTE;
dwBytesCount : DWORD;

dwBusyCounter: DWORD;
dwErrorCounter: DWORD;
xTrigger : BOOL;

//Variables for MQTT subscribe
oFbSubMQTT_2 : WagoAppCloud.FbSubscribeMQTT_2(eConnection := eConnectionId.Connection1);
aSubBuffer : ARRAY[0..1999] OF BYTE;
xDataReceived: BOOL;
dwSubSize: DWORD;

//Variables for JSON_Writer
oFbJSON : WagoappJSON.Fb_JSON_Writer_02;
MyTemplateString : STRING(JSON_MAX_STRING):= '{"val1": #Parameter, "val2" : #Parameter, "val3": #Parameter}'; //String template
MyValueArray : ARRAY[0..2] OF STRING; //Array of values, must start at index 0
xJSONTrigger : BOOL;

value_var1 : INT; //Raw values to be transmitted. Must be converted to string first.
value_var2 : INT;
value_var3 : INT;

TimerOn: TON;
sPayload : STRING(2000);

END_VAR

  • Set up the JSON trigger and, in general, the code flow as follows:

TimerOn(IN := TRUE, PT := T#1S);

value_var1 := 40;
value_var2 := 50;
value_var3 := 2;

MyValueArray[0] := INT_TO_STRING(value_var1);
MyValueArray[1] := INT_TO_STRING(value_var2);
MyValueArray[2] := INT_TO_STRING(value_var3);


IF TimerOn.Q THEN
xTrigger := TRUE;
xJSONTrigger := TRUE;
TimerOn(IN := FALSE);

IF NOT oFbPublishMQTT_2.xBusy THEN

// Copy payload string
dwBytesCount := Length(sPayload);
MemCopy(pDest := ADR(aBuffer), pSource := ADR(sPayload), udiSize := dwBytesCount);

// Trigger the transmission
xTrigger := TRUE;

ELSE
// Busy statistics counter
dwBusyCounter := dwBusyCounter + 1;
END_IF

IF oFbPublishMQTT_2.xError THEN
// Error statistics counter
dwErrorCounter := dwErrorCounter + 1;
END_IF

END_IF

oFbJson(sJSON_BaseFrame := MyTemplateString,
aParameterValues := MyValueArray,
xTrigger := xJSONTrigger,
sOutput := sPayload);

oFbPublishMqtt_2(sTopic:= '/v2.0/devices/joes_test',
eQualityOfService:= 1,
dwSize := dwBytesCount,
aData := aBuffer,
xTrigger := xTrigger);

  • Go to the toolbar and click Online → Login. This will run the program and send the data to Ubidots.

  • You can check the official repository for this project on the following link.

Did this answer your question?