Connect Raspberry Pi with ctrlX CORE using Node-RED

Send Data from Raspberry Pi to ctrlX CORE with Node-RED

sanX
New Contributor

Overview

In this article, it will be shown how to send data from the Raspberry Pi to the ctrlX CORE with the use of Node-RED. Random data will be generated in the Raspberry Pi with a “random” node and it will be named as “Temperature”. This data will be send with one communication protocol and read from the ctrlX CORE. In this tutorial, two ways will be shown: With MQTT broker or TCP.

The Node-RED app will be used to simplify the communication and data acquisition between both devices, and in the case the user wants to make more complex uses, it is a very convenient platform.

On the other hand, the Mosquitto MQTT Broker acts like an intermediate between devices that send and collect data. Devices that send data are called “Publishers” and devices that collect and read the data are called “Subscribers”. Subscribers can decide which data they want to read in the case there are many publishers. In this case the ctrlX CORE will act as a Subscriber and the Raspberry Pi as a Publisher.

MQTT Broker Overview

Finally, TCP (Transmission Control Protocol) is the worldwide most used protocol and establishes the communication between a client and a server. It is used for reliable and accurate communication purposes, but it is slower than other protocols such as MQTT. Basically, it breaks the message in packets to avoid resending the entire message again in case of transmission failure and in destination the packets are reassembled. In this case, the Raspberry Pi will act as client and the ctrlX CORE as server.

Prerequisites

MQTT Broker connection

The MQTT broker has the advantage to be faster and more reliable, but it is required to have a license to be able to install it in the ctrlX CORE and the configuration process is longer.

MQTT Broker with Raspberry Pi and ctrlX CORE connection

Step 1: ctrlX CORE configuration

1. First, ctrlX CORE will be connected to the Engineering computer and the ctrlX WORKS program will be started. 

2. Now, the Node-RED and the Mosquitto apps will be installed. Firstly, it is required to download both apps from the ctrlX Store.

  • To perform these steps, it is recommended to follow the instructions shown in this article

3. The apps are installed. In the ctrlX CORE web interface the “Settings” menu es clicked ->  Access “Apps” -> Change to “Service mode” because otherwise, it will not be possible to install the apps, and the “Install from file” button is clicked. A new window will be opened where the previously downloaded files are selected.

ctrlX CORE apps installation

Once installed, the apps will be visible as shown in the previous figure.

4. Mosquitto app configuration

  • Access the ctrlX CORE homepage, select "Mosquitto" and press "Management Center

MQTT Broker Management Center Access

  • In the new window, press over the "Clients"

MQTT Broker Client Configuration Access

  • Create new clients. In this part, the clients that will be connected to the broker are defined. A MQTT client is any device that runs a MQTT library and connects to an MQTT broker over a network. In this case, the MQTT Broker is the Mosquitto MQTT Broker App installed in the ctrlX CORE. On the other hand, the clients will be the Raspberry Pi where the MQTT libraries will also be installed, and the ctrlX CORE where with Node-RED another client will be defined. 

First, the Raspberry Pi client will be defined with the following configuration:

  • Username: tutorialmqtt
  • Password: tutorialmqtt
  • Client ID: tutorialmqtt
  • Text name: tutorialmqtt

MQTT Broker Client configuration

  • Once the client is defined, it is required to define a role. In this case, a “client” role will be used.

Client selection

Second, the Node-RED client that will later be defined in the ctrlX CORE is also defined:

  • Username: nodered
  • Password: nodered
  • Client ID: nodered
  • Text name: nodered

Node-RED for the ctrlX CORE Client Configuration

  • As done for the Rapsberry Pi client, a "client" role is defined
  • The clients window should look as follows:

Clients configuration Overview

NOTE: In the Roles section it is possible to read the roles description.

Roles check

  • When the clients are connected, in the main section it will be possible to see if they are connected or not. Once everything is configured, an example will be shown.

MQTT Broker Clients check

Step 2: Raspberry Pi configuration

Once everything has been configured from the ctrlX CORE side, the Raspberry Pi needs to also be configured.

1. Install the Node-RED app in Raspberry Pi. To perform this step, it is possible to follow the guidelines of the Official Node-RED webpage 

  • First, make sure that the system is updated
sudo apt update && sudo apt upgrade
  • To download and install the app, write the following command, and follow all the steps:
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Node-RED installation in the Raspberry Pi

2. Start Node-RED and configure MQTT.

  • It is possible to open the Node-RED web interface from the icon shown in the following figure or from the applications Menu -> Programming -> Node-RED

Node-RED app access

  • When the Node-RED app is started, a Node-RED interface is available. This interface is reachable with an IP-address. This IP-address is visible as shown in the next figure when opening the Node-RED app.

Node-RED web interface access

  • Now it is necessary to add nodes to configure our system as desired. Theses nodes can be added from the left bar, and we can design a similar system as shown in the images below.

Raspberry Pi nodes overview

  • In this case, an “inject” node will be added with the purpose to add values in an interval of time. On the other hand, a “debug” node will be added to be able to see the messages sent to the ctrlX CORE. Additionally, a “random” node is also added with the objective to generate a random value in a predefined range. Finally, a “mqtt out” node is used to send the messages to ctrlX CORE. All these nodes are connected as shown below.

    The inject node generates a signal every “X” second, and activates the random node. Each time the random node is activated, this node generates a random value and sends it to the “debug” and “mqtt out” nodes. The “debug” node gives the value in the debug tab on the right side of the interface and the “mqtt out” node sends the value to the MQTT broker. 

  • To configure the random node, it is just needed to double click over it, and define the range of the random values. The default configuration is between 1 and 10 and once decided which interval we want, click over the “Done” button.

Random node configuration

  • To configure the inject node, double click over the node and the default configuration is “timestamp”. If we want to send messages in a time interval, we can select “interval” next to the “Repeat” icon, and select every how many seconds we want to send a message.

Inject node interval configuration

  • Now it is required to configure the MQTT node:
  1. Double click over the mqtt node
  2. In the Server tab: “Add new mqtt-broker
  3. Give a name to the Node
  4. Enter in the Server configuration

MQTT Broker configuration

  • In the server configuration window, it is needed to fill the following blank spaces:

MQTT Broker configuration

  1. Server: Introduce the ctrlX CORE IP address
  2. Port: Usually, the default port is 1883 and it is needed to change to 8883
  3. Protocol: Select MQTT V3.1.1
  4. Client ID: Introduce the previously defined client ID in the Mosquitto app in the ctrlX CORE
  5. Security: Enter in the Menu shown in the figure below and enter the previously defined credentials in the Mosquitto app in ctrlX CORE.

Credentials introduction in the Security section

6  Use TLS: Check the space and select the pencil to enter in the TLS configuration window. There, it is just required to uncheck “Verify server certificate” and click “Update” in the top right corner.

Server certificate uncheck

  • Once everything has been configured, click on the right top corner on “Add”, and a similar window as shown below should appear. In “Topic” we just write the desired name and click over “Done”.

MQTT Broker configuration

  • The window will be closed, and in the node, a blue dot will be visible because it is required to deploy. For this purpose, just click over “Deploy” in the top right corner of the interface.

Raspberry Pi deploy button

  • Once deployed, the “Connected” status should be visible in the configured node.

Raspberry Pi Nodes

  • On the right bar, the debug menu is visible and from there it is possible to see all the sent values thanks to the use of the “Debug” node.

Raspberry Pi debug Menu from Node-RED

Step 3: Connection check and final configuration

1. Now we can return back to ctrlX CORE, and check that there is a new client in the Cedalo Mosquitto App.

MQTT Broker Clients Counter

2. In the Topic Tree section, we can check the received values in ctrlX CORE. As the “Temperature” topic was given in the Raspberry Pi, this is the name seen in the Topic Tree.

MQTT Broker Topic Tree

  • Once we have checked that all the values are being received by the broker, we can subscribe to it to get the values in the ctrlX CORE Node-RED app.

3. In the ctrlX CORE web interface, access the Node-Red App. A new window will be opened, and now a similar process as done in the Node-RED app in the Raspberry Pi will be followed: 

  • Insert a “MQTT in” and “debug” nodes from the nodes bar. The result should be as follows.

ctrlX CORE Node-RED Nodes

  • Configure the “MQTT in” node by double clicking over it and fill the blank spaces as shown in the next figure.

MQTT Node configuration

  1. Server: Click over the pencil and enter the values shown in the next figure. NOTE: In "Security" introduce the credentials defined for Node-RED for the ctrlX CORE and not the ones defined for the Raspberry Pi. Otherwise, they would not be able to connect at the same time because the broker only accepts one connection per client. On the other hand, activate the TLS configuration as performed in the Raspberry Pi configuration, and do not forget to uncheck “Verify server certificate” in the TLS configuration menu.
  2. Action: Subscribe to single topic
  3. Topic: “Temperature” as it is what was defined in the Raspberry Pi. It is very important to write here the same topic because otherwise it will not be possible to read the values coming from the Raspberry Pi.
  4. QoS: Defines the quality of the connection
  5. Output: We leave as it is per default
  6. Name: We can give any name

MQTT Broker Configuration

4. Once everything has been configured click over “Update” and “Deploy” to check if the node is able to subscribe to the MQTT broker.

ctrlX CORE Node-RED Nodes

5. By clicking over the debug menu, it will be possible to see the values sent from the Raspberry Pi.

Note: As now there are two additional clients connected to the Broker, it we return back to the MQTT Broker App in the ctrlX CORE, the following will be visible:

Connected clients check

TCP connection

The TCP configuration is much easier because everything is already integrated, and it is just needed to introduce the configuration parameters in both devices.

Raspberry Pi with ctrlX CORE TCP/IP connection

Step 1: Raspberry Pi configuration

As explained in the MQTT Broker case, it is required to install the Node-RED app.

1. Install the Node-RED app in Raspberry Pi. To perform this step, it is possible to follow the guidelines of the Official Node-RED webpage 

To download and install the app, write the following command, and follow all the steps:

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Node-RED installation in the Raspberry Pi

2. Start Node-RED and configure TCP.

  • It is possible to open the Node-RED web interface from the icon shown in the following figure or from the applications Mneu -> Programming -> Node-RED

Node-RED app access

  • When the Node-RED app is started, a Node-RED interface is available. This interface is reachable with an IP-address. This IP-address is visible as shown in the next figure when opening the Node-RED app.

Node-RED web interface access

  • In this case, an inject node will be added with the purpose to add values in an interval of time. On the other hand, a debug node will be added to be able to see the messages sent to the ctrlX CORE. Additionally, a random node is also added with the objective to generate a random value in a predefined range. Finally, a TCP out node is used to send the messages to ctrlX CORE. All these nodes are connected as shown below.

Nodes selection

Note: The TCP out node can be found in the Network section as shown in the following image.

TCP out node selection

  • To configure the random node, it is just needed to double click over it and define the range of the random values. The default configuration is between 1 and 10 and once decided which interval we want, click over the “Done” button.

  • To configure the inject node, double click over the node and the default configuration is “timestamp”. If we want to send messages in a time interval, we can select “interval” next to the “Repeat” icon and select every how many seconds we want to send a message.

Inject node Interval selection

  • Once all the nodes have been added, TCP can be configured. For this purpose, double click over the “TCP out” node and it is just required to configure what is shown below. The port can be chosen as desired and in the “at host” space, it is just required to write the ctrlX CORE’s IP address. When finished, click over “Done”.

TCP Configuration from the Raspberry Pi side

  • Finally, in the TCP node, a blue dot will be visible because it is required to deploy. For this purpose, just click over “Deploy” in the top right corner of the interface and the Raspberry should connect to the ctrlX CORE.

Node-RED deploy

If everything is correct, the “Connected” status should be visible as shown below.

Raspberry Pi Nodes

Note: In the case TCP has still not been configured in the ctrlX CORE, it will not be able to connect and the “Disconnected” status will be shown.

Step 2: ctrlX CORE configuration

1. First, ctrlX CORE will be connected to the Engineering computer and the ctrlX WORKS program will be started.

2. Now, the Node-RED will be installed. Firstly, it is required to download the app from the Bosch Rexroth store.

  • To perform this step, it is recommended to follow the intructions shown in the following article.

3. The app is installed. In the ctrlX CORE web interface the “Settings” menu es clicked ->  Access “Apps” -> Change to “Service mode” because otherwise it will not be possible to install the app, and the “Install from file” button is clicked. A new window will be opened where the previously downloaded file is selected.

ctrlX CORE apps installation

Once installed, the app will be visible as shown in the previous figure.

4. Node-RED app access. Click over the Node-RED section as shown and click on “Flow editor”.

Node-RED app access

5. Insert the TCP in and debug nodes and connect them.

ctrlX CORE nodes

6. Configure the TCP in node as shown below. Include the port used in the Raspberry Pi and select how we want the data to be received. Close the window by clicking over “Done”.

TCP configuration

7. Click over “Deploy” and check that there is one connection as shown below.

ctrlX CORE nodes

8. Check the received values in the right tab.

Sent values check

sanX
sanX
Hi, I am Nicolas from the DCEM Application Team. I am temporary not available, if you have any questions, do not hesitate to contact LunatiX.
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist