Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
SeungjoonLee
Advisor
Advisor
0 Kudos
Hello everyone, again. This is the fifth blog post based on the following table.






HCP IoT Showcase using Sphero
Part 1: Overview
Part 2: Controlling Sphero using Raspberry Pi and Xbox 360 controller
Part 3: Maze Setup (Hardware)
Part 4: Unit Test (Gathering & Sending Data)
Part 5: Unit Test (MQTT)
Part 6: Integration Test
Part 7: XS Application
Part 8: UI

This blog explains the highlighted section in the overall architecture below.



In the previous blog, I introduced 2 unit tests in regard to gathering and sending data.

The remaining test is about MQTT broker which is for displaying real-time data in the dashboard.

MQTT broker does not directly communicate with HCP IoT Services here but it will play a role for supplementing HCP IoT Services from a displaying real-time data perspective.

So, technically, it is a separated data source for our dashboard and its purpose is different from HCP IoT Services in the architecture.

  • HCP IoT Services: Gathering and processing all data using HANA

  • MQTT Broker: Displaying real-time data without storing them in any databases


SAP Korea team used Mosquitto as our MQTT broker and we installed it on Raspberry Pi since we don’t need a public MQTT broker.

Let’s do the last unit test.

Unit Test 3: MQTT


The first thing you need to do is installing Mosquitto on Raspberry Pi with WebSocket support.

Since peter6960 (Peter van der Walt)’s blog explains the detailed steps in regard to the installation, I’ll not explain it here.

Please refer the “Installation Mosquitto with WebSocket Support” and “Enable WebSocket Support” sections in his blog.

However, I didn’t install mosquitto client and there are slight differences in editing mosquitto.conf file and loading it as below.
sudo apt-get install mosquitto mosquitto-clients

mosquitto.conf
sudo nano /etc/mosquitto/mosquitto.conf

I added 3 lines as below.
listener 1883
listener 9001
protocol websockets

Then I load config using following command.
mosquitto -c /etc/mosquitto/mosquitto.conf

The next thing is installing Chrome extension for subscribing and publishing topics.

I installed MQTTLens but if you have any favorite tools other than Chrome extension, you can also use them.





Once you launch MQTTLens, you can add a new connection using + icon.



Fill any name you want in the connection name and MQTT broker’s IP in the hostname.

In my case, I used Raspberry Pi’s IP which is connected my Mac since Mosuqitto is on Raspberry Pi.

Then you can just create connection and test MQTT by subscribing and publishing.



I just named topic as /xyz for subscribing then also used the same one for publishing and typed “Hello RPi” in the message.

Once you publish the message, you will see the screen as below.



Now, we created two more MQTT clients. The first one is based on the Python example from eclipse paho.

Based on this code, we modified it as below.

mqtt-python-test.py
#Python MQTT test sample
import paho.mqtt.client as mqtt
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
client.subscribe("/xyz")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.2.2", 1883, 60)
client.loop_forever()

Since I'm using /xyz as topic name in this test, I used the same here and my Raspberry Pi's IP with 1883 port.

If you run this code, you will see it is waiting for messages as below.



The second one is written in Javascript and SAPUI5. This client is running on HCP and its functions are very slimier to MQTTLens.

I deployed it on my personal HCP account as below.

UI5 MQTT Client Sample

Before testing, since HCP only allows SSL, you need to configure SSL/TLS client cert to secure MQTT.

In order to achieve this please refer RockingD Labs' blog.

I leveraged 1. Setup a protected workspace and 2. Setup a CA and generate the server certificates.

In order to subscribe /xyz topic, I filled out some fields as below and click connect and subscribe.

For connection, since we use a self-signed certificate, WebSocket opening handshake will be canceled.

The easiest way to avoid this is starting or configuring your browser with ignore self-signed certificate error or accept it automatically option.

In my case, I just started Chrome browser with --ignore-certificate-errors flag.



Now, if you publish any messages from MQTTLens, you will see those messages are available in 3 clients (MQTTLens, Python and Javascript).







That’s it!. Now we’re ready for the integration test. Let me get back to you soon with next posting.

Best,
Seungjoon