Skip to Content
Technical Articles
Author's profile photo Dries Van Vaerenbergh

Smart Coffee Machine – Set Up the Internet of Things Edge Platform (MQTT)

Introduction

 

Hello Community,

Welcome to the “Set Up the Internet of Things Edge Platform – MQTT” blog.

This blog will be the second part of a journey of blogs written by Wouter Lemaire , Thomas Swolfs and myself.

In the previous blog Wouter Lemaire talked about the functional case of this coffee machine project and provided a high-level overview of this IoT scenario.

This blog will cover the installation and setup of the SAP IoT Edge Platform MQTT service.

This installation will be done on a Raspberry Pi 3 Model B+ device.

Next we will cover the installation of the Paho MQTT-Client in a virtual environment to publish data to the SAP IoT Service Cockpit.

After this installation we will show you how to build a Python Script to use this Paho MQTT-Client to publish the data.

Eventually you can verify the data in the SAP IoT Service Cockpit under the Data Visualization section.

NOTE:

It’s recommended to have a know-how on the setup of devices and sensors and their capabilities in the SAP IoT Service Cockpit. This properties will be used later in the blog to publish the data.

 

Blog series about the Smart Coffee Machine, here you have an overview:


Useful documentation links

 

SAP Help Portal: Set Up the Internet of Things Edge Platform – MQTT

Python Software Foundation: Install Paho-MQTT

 


Download, install and set up IoT edge gateway

 

Download the Internet of Things Edge Platform (and Adapters)

Download the Internet of Things Edge Platform from the SAP Software Center. This software is available here in the SAP Software Center.

 

Install the IoT Edge Platform (and Adapters)

Extract the downloaded .zip folder.

Inside the extracted folder, you can find another ZIP file. Please extract this gateway-<version>.zip. 

Navigate to the extracted gateway directory and open a terminal on this location.

Depending on what OS you are working on launch the right script to install the gateway. On Windows launch the (build.bat), on MacOS (build.sh) and on Linux and Raspberry Pi also the (build.sh). We use the Raspberry Pi in this blog.

Dont forget to pass the parameter MQTT.

So your command should look like this:

./build.sh MQTT

A new file config_gateway_mqtt.xml is created in the /config folder.

 

Set up the IoT Edge Platform (and Adapters)

Open the config_gateway_mqtt.xml with a text editor.

Enter the host name for your connection-string :

(update the {SAMPLE} part with your tenant host name)

Your code block should look like this:

 <cnf:amq>
        <cnf:connectors>
            <cnf:client>
                <cnf:connectionString>failover:(nio+ssl://{SAMPLE}.cp.iot.sap:61616?daemon=true&amp;soTimeout=60000)</cnf:connectionString>
                <!-- <cnf:connectionString>failover:(nio://127.0.0.1:61619?daemon=true&amp;soTimeout=60000)?initialReconnectDelay=5000</cnf:connectionString> -->
                <cnf:streamTTL>5000</cnf:streamTTL>
                <cnf:TTL>200000</cnf:TTL>
                <cnf:soap_over_jms_tmo>60000</cnf:soap_over_jms_tmo>
                <cnf:concurrentProducers>5</cnf:concurrentProducers>
                <cnf:concurrentConsumers>5</cnf:concurrentConsumers>
                <cnf:ackMode>1</cnf:ackMode><!-- AUTO_ACKNOWLEDGE = 1 / SESSION_TRANSACTED = 0 -->
                <cnf:nonPersistent />
                <cnf:maxConnections>8</cnf:maxConnections>
            </cnf:client>
        </cnf:connectors>
    </cnf:amq>

Next provide the host name for the address in the core connection:

(update the {SAMPLE} part with your tenant host name)

<cnf:coreConnection>
        <cnf:address>https://{SAMPLE}.cp.iot.sap:443/iot/core/api/v1/</cnf:address>
        <cnf:timeout>60000</cnf:timeout>
        <cnf:authentication>BASIC_AUTH</cnf:authentication>
        <cnf:mutual>false</cnf:mutual>
</cnf:coreConnection>

Set the first transport connector for your MQTT-broker equal to your device IP -address:

(update the {YOUR-DEVICE-IP-ADDRESS} part with your device IP -address)

<cnf:transportConnectors>
    <cnf:transportConnector name="mqtt" uri="mqtt://{YOUR-DEVICE-IP-ADDRESS}:61618?transport.soTimeout=60000"/>
    <cnf:transportConnector name="mqtt+mutualAuth" uri="mqtt+nio+ssl://127.0.0.1:61628?wantClientAuth=true&amp;transport.soTimeout=60000"/>
    <cnf:transportConnector name="nio" uri="nio://127.0.0.1:61617?transport.soTimeout=60000"/>
    <cnf:transportConnector name="tcp-connector" uri="tcp://127.0.0.1:61627?transport.soTimeout=60000"/>
</cnf:transportConnectors>

 

 


Download the IoT service cockpit certificate

 

You can find the certificate when you log on to the Internet of Things Service Cockpit.

This under your user information:

Download the certificate

Next you will have to create a new folder named certificates inside the config folder:

In this folder you extract the downloaded certificate file.

Your certificates folder will contain the following files:

Open the pswd.properties file and change the following line with your password:

password={YOUR-PASSWORD}

Save it.

 

 


Run the IoT edge MQTT gateway

 

To run the IoT edge gateway navigate to the directory /gateway.

In here we will try to run the edge gateway.

This with the following command (.sh linux, .bat windows):

./gateway.sh

 

Depending on what device you want to run the gateway on you could get the following error:

Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap

This because the the VM cannot reserve enough space to launch and run the gateway.

This can be solved by running the following command, which will increase the available memory:

export _JAVA_OPTIONS=’-Xms64M -Xmx128m’

 

When we try to run the gateway again with:

./gateway.sh

We will see the gateway is starting.

If the Internet of Things Edge Platform is started successfully, you see the following message Initialization successfully finished.

 

 


Install the Paho-MQTT client

 

To send the data to the SAP IoT Service Cockpit you will need a MQTT-Client.

We will use the Paho MQTT-Client.

You can download the GUI MQTT-Client tools here.

We will use the command line in this blog since we are working on the Raspberry Pi.

This command line installation is explained here.

We will use the virtual environment to start the MQTT-Client.

You can create and start this environment as follows:

Run the following commands:

sudo pip install virtualenv

virtualenv paho-mqtt

source paho-mqtt/bin/activate

pip install paho-mqtt

After executing this commands you will see your command line starts with the name of the environment in front of it.

 

 


Develop a Python script

 

To send our data to the IoT Service Cockpit we will build a Python-script thats uses the MQTT-Client to send the data.

NOTE:

Provide your id’s in the following variables:

YOUR-IP-ADDRESS = The device its IP-Address

deviceId = “YOUR-DEVICE-ID”
sensorId = “YOUR-SENSOR-ID”
capabilityId = “YOUR-CAPABILITY-ID”

Change {YOUR-VALUE} to a value you want.

This publish payload is build according to the capabilities you configured in you SAP IoT Service Cockpit.

The Script:

#-----------------------------
#    IMPORT LIBRARIES
#-----------------------------
import paho.mqtt.client as mqtt
import sys


#-----------------------------
#    CONNECTION SETUP
#-----------------------------

# Create new instance
client = mqtt.Client("P1")

# Connect to the ip-address of the DELL IoT Edge Gateway with the specified portnumber and timeout
client.connect("YOUR-IP-ADDRESS", 61618, 60)

try:
    #-----------------------------
    #      Variables
    #-----------------------------
    # sensor and capability id -> use alternate id
    deviceId = "YOUR-DEVICE-ID"
    sensorId = "YOUR-SENSOR-ID"
    capabilityId = "YOUR-CAPABILITY-ID"

    #This is a special variable that is send with the payload while publishing.
    #The use of this variable will be explained in a later blog of Wouter Lemaire about Interceptors.
    ct='ct' 

    #Fill in the publish payload with values for {YOUR-VALUE}

    client.publish("measures/"+ str(deviceId) +"","{ \"capabilityAlternateId\": \"" +str(capabilityId) + "\",\"sensorAlternateId\":\""+
                   str(sensorId)+"\", \"measures\":[["+ "\"" + str({YOUR-VALUE}) +"\"" +","+
                   str({YOUR-VALUE}) +","+ "\"" + str({YOUR-VALUE}) +"\"" +
                   ","+ str({YOUR-VALUE}) +"," + str({YOUR-VALUE}) +
                   ","+ "\"ct\"" +"]]}")
        
except KeyboardInterrupt:
    print('\ncaught keyboard interrupt!, bye')
    sys.exit()

Save the script with a name and the .py extension.

We used the same script to publish our data over MQTT.

Our script is extended with some extra code to read our waterflow sensor data.

 


Run the Python script

 

Finally the moment to run the Python-script. This can be done by navigating to the folder where your script is in and running the following command:

python {YOUR-SCRIPT-NAME}.py

Here you can see the script measuring the waterflow.

The script will publish the data to the SAP IoT Service Cockpit. Time to verify!

 


Verify the data in SAP IoT service cockpit

 

Navigate to your device and scroll down to the Data Visualization section.

Select your sensor and the desired properties.

As we can see the data successfully entered the SAP IoT Service Cockpit into our device.

The Interceptors also recognized the type of coffee and they marked it as an Espresso.

More about Interceptors in the next blog by Wouter Lemaire.

 

 


What did we do in this blog

 

We learned a lot of things about the SAP IoT Service Cockpit and the setup and installation in the EDGE MQTT component.

Some bullets to recap our learning path and what we’ve learned:

  • We downloaded the Internet of Things Edge Platform (and Adapters)
  • We installed the Internet of Things Edge Platform (and Adapters)
  • We configured the Internet of Things Edge Platform (and Adapters)
  • Found out where to download the IoT Service Cockpit Certificate
  • We ran the IoT Edge MQTT Gateway
  • We installed the Paho MQTT-Client
  • We developed a Python script
  • We ran the Python script
  • We verified the data in the IoT Service Cockpit

 

We really hope this blog was interesting and helpful to you and we really enjoyed working on this project!

We are happy to announce our next blog about Interceptors, by Wouter Lemaire.

A big thanks to Wouter Lemaire for the nice teamwork and a big thanks to the Flexso Digital company for making this project possible!

 

Greetings, Wouter Lemaire , Thomas Swolfs & Dries Van Vaerenbergh

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.