Start building your own chatbot now

What’s the goal of this tutorial?

By the end of this tutorial, you’ll be able to speak with your own chatbot to get information about temperature and humidity from an IoT device.

I’ll describe a step-by-step guide about how to configure your SAP Cloud Platform (IoT Service + SAP Hana DB), codify your IoT device and create a new chatbot that integrates with IoT Service in SAP Cloud Platform storing data into an SAP HANA Database.

See below the result expected at the end of this article.

chatbot-iot-service

Data flow

data-flow-sap-cloud-platform-chatbot-hana

Prerequisites

  • SAP Cloud Platform Neo account (Create a free trial account here);
  • SAP Conversational AI (Create a free account here);
    • If you don’t have experience with SAP CAI, I suggest first following this tutorial to understand how to work with it.
  • An IoT device. You can use an existing one or prototype a new one (our case here). But don’t worry if you don’t have a physical IoT device or can’t prototype one – you can simulate one with your computer sending data to the service using Postman.

Steps

  1. Create a new database (SAP HANA MDC)
  2. Activate and configure an IoT Service in your SAP Cloud Platform account
  3. Create the digital twin
  4. Configure your IoT Device
  5. Create a new service (XSJS) in your database
  6. Create a chatbot in SAP Conversational AI

STEP 1: Create a new database (SAP HANA MDC)

In the left-hand menu of your SAP Cloud Platform account, go to SAP HANA / SAP ASE and click on Databases & Schemas:

sap-cloud-platform-database

Click on New, provide the information asked on the next screen, and click on Create:

new-database-creation-sap-hana

 

IMPORTANT: In this step, I activated the option to configure a new user for SHINE, but it isn’t needed, you can create a new user in SAP HANA (under Security) and assign the correct roles if you prefer. I’ve just activated here because it is easier.

Wait for the DB creation. When it’s ready, you’ll see a screen like this one in the Overview menu option:

database-job-creation-sap-hana

Here is a trick! Click on SAP HANA Cockpit link, and on the next page, log on with the SYSTEM user using the password that you’ve just created:

sap-hana-connection-account

This action will assign some needed roles. After that, you can click on the SAP HANA Web-Based Development Workbench link, and you can log on with your SHINE user to change your password for the first time and to check your SCHEMAS in your new HANA database.

Done, your new DB is created!

STEP 2: Activate and configure an IoT Service in your SAP Cloud Platform account

First, you must activate IoT Services in your SAP Cloud Platform account.

In the left-hand menu of your SAP Cloud Platform account, go to Services and search for Internet of Things:

sap-cloud-platform-cockpit-iot

Click on Go to Service.

STEP 3: Create the digital twin

In the IoT Service Cockpit you must set up a new Message Type, a new Device Type and a new Device in this exact order.
These following steps will create a Digital Twin of your physical IoT device.

device-management-iot-service-cockpit

Step 1/3

Click on Message Types and on the Create message type icon at the bottom of the page. Following the example below, fill in the information according to the data sent from your IoT Device, and click on Create.

create-message-type-iot

Note: I’ve created these 3 fields because my IoT prototype send these 3 values.

You’ll need the Message Type ID later on, but you can check it anytime.

message-id

Save this information and go back to the Cockpit.

Step 2/3

Click on Device Types and then click on the Create Device Type icon at the bottom page.

Enter the name of your device type and click on Add Message Type.

create-device-type

Enter a name in the Assignment Name field and choose the Message Type that you’ve just created, and select the From Device option. Click on Create.

create-device-type-2

Step 3/3

Click on Devices and then click on the Create Device icon at the bottom of the page.

Fill in Name with a name for your device, select the Device Type that you’ve just created and click on Create.

create-device-3

IMPORTANT: You must save the token that appears in this step because you won’t be able to see it again afterwards. If you lose this token, you’ll need to generate another one.

oath-access-token

You’ll need the device ID too, but you can check this information at any time.

serviceroom-information-id

Go back to the cockpit and click on Redeploy the Message Management… or Deploy if you’ve never used it before.

internet-of-things-service-cockpit

Enter the account information and click on Deploy twice.

deploy-the-message-management-service

After that, go to the link below.

deployment-database-sap-cloud-platform

In the application dashboard, go to the Data Source Bindings menu.

sap-cloud-platform-data-source-bindings

Delete <DEFAULT> binding if it exists, and click on New Binding:

java-application-data-source-bindings

Enter the information below and use the user that you created in DB (in my case PENA) and click on SAVE.

new-binding-database

Go back to the application dashboard and go to Security > Roles. Select the role IoT-MMS-User and assign your SAP Cloud Platform user account.

java-application-roles

Go back to Overview and start the application.

java-application-overview

Once the service has started, access the link of the application.

java-application-overview-2

In the Message Management Service Cockpit, you can see the messages stored, push, messages to device and so on.

Now click on Messaging through HTTP in Data Services.

internet-of-things-service-cockpit

In the HTTP API are two important pieces of information: The Data Endpoint that you’ll use in your IoT Device code to send information from the device to the service, and Message. You need to use exactly this JSON format to send the message to the service.

http-api

You can run a test here to check if your data is being saved correctly in the database.

In the Data Endpoint you must change the Device ID information:

https://iotmmsp999999trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/<DEVICE_ID>

In the message JSON, you must change the information below from your message type ID. In our case, we’ll change the fields and values according to our message type:

{“mode”:”sync”,”messageType”:”<MESSAGE_TYPE_ID>”,”messages”:[{“temperature”:”20.50″,”humidity”:”40.00″,”luminosity”:”1024”}]}

When you click on SEND and run a test, the server should reply like this:

reply-from-server

And you can see your message test if you click on Display Stored Messages in Core Services.

display-stored-messages-core-services

iot-service-sap-hana-database

Done. Your IoT Service is now configured and saving data in your SAP HANA database!

STEP 4: Configure your IoT Device

As mentioned before, you must configure some IoT Devices to send data to your IoT service.

In this case, I used an Arduino based board, but you can use any device (e.g. computer, smartphone, Raspberry PI or other board that you would like to prototype).

wemos-d1

Wemos D1 mini with ESP8266 module and embedded WiFi

dht11-sensor

For temperature and humidity, I’m using a DHT11 sensor, connected in this Wemos D1 mini.

ldr-sensor

For luminosity I’m using an LDR sensor.

Note: In my prototype I’m using another sensors, LED’s and OLED display for example, but I will talk about it here, because it hasn’t reference with this demonstration.

My focus here is not to show you how to connect the electronic parts, but to show you the communication between this device and the IoT Service in SAP Cloud Platform. So I won’t talk too much about physical parts, but I’m sure that you can easily find how to prototype a device like this one. With this in mind, let’s see the Arduino code used to send the information from the device to the IoT Service.

You can see the complete Arduino code at this GitHub link. In this post, I’ll talk about the important parts:

https://github.com/PenaNumen/CAI_IOT_SCP/blob/master/Numen_Wemos_HTTP_SCP.txt

First, set your WiFi connection:

Set your connection with SAP Cloud Platform and Device information:

Set the Endpoint URL:

Set the payload message:

POST method:

And now you can see the data in your database table:

application-data-table

Ready! Your IoT Device is sending real data to your IoT Service. Congratulations!

STEP 5: Create a new service (XSJS) in your database

We need to create a new XSJS service in order to extract data from the database and send the information to your chatbot. Let’s try with 2 questions, one asking about temperature and one about humidity. If the user doesn’t specify the temperature or the humidity, the chatbot will answer to both.

To create an XSJS service, follow these steps below:

  1. Access your SAP Cloud Platform account and go to your database tenant in Databases & Schemas.
  2. Access the Web-Based Development Workbench. You can use Eclipse to develop if you prefer, here we show how to do it in Web, but the process is the same.
  3. Log in to your database with your SHINE user, or the user that you’ve created before.

Click on Editor:

sap-hana-workbench-menu

In the Editor, I created a new package called IoT. Under the IoT package, I created another package called DATA to organize all services related to data access.

create-package-iot-device

Under the IoT package, we need to create two files to expose package and data:

  • .xsapp
  • .xsaccess

The .xsapp file is an empty file. You’ll find the code of the .xsaccess file below (available on GitHub)

{
  "exposed": true,
  "authentication": {
   "method": "Form"
  },
  "cache_control": "must-revalidate",
  "cors": {
   "enabled": true,
   "allowMethods": [
   "GET",
   "POST",
   "HEAD",
   "OPTIONS"
   ],
   "allowOrigin": ["*"],
   "maxAge": "3600"
  },
  "enable_etags": false,
  "force_ssl": false,
  "prevent_xsrf": false
}

xsaccess-editor-sap-hana

Now, access and authentication are ready. Under the package data, we’ll create a new file get_sensor_data.xsjs:

get-sensor-data

Here’s the get_sensor_data.xsjs content – available on GitHub. Select statement to access just the last one sensor data in our table in SAP HANA.

Save data in variables:

variables

Request parameters are sensortype, 1 for Temperature and 2 for Humidity. If the parameters are empty, the chatbot will reply with both info.

sensor-type

The response must be a JSON exactly like the one below. You can find an example in the CAI documentation.

json-chatbot

Your service is now created and you can access the last one sensor data. Test it!

get-sensor-data

Save these links below. You’ll them for your chatbot in the next step of the tutorial.

For both:

For temperature:

For humidity:

Done! You’re now ready to build and train your chatbot.

STEP 6: Create a chatbot in SAP Conversational AI

As mentioned before, if you’ve never built a chatbot before, I recommend following this tutorial in the CAI blog.

Connect to your CAI account and click on the + New Bot button to start.

new-chatbot-sap-conversational-ai

  1. Select Greetings as a predefined skill
  2. Choose a name and a description for your chatbot
  3. Choose a default language
  4. Set the data to non-personal and end users as non-vulnerable

In the Bot Visibility section, you can choose between Public or Private. For this specific case, I’ll choose Public only to make the bot available for other use cases like this, and so that anyone following this step-by-step can check the build. But feel free to choose the best option for you.

When you’re done, click on Create a bot.

Now, your Bot is created and the intents “@goodbye” and “@greetings” are already being used.

Click the + Create button to create a new intent. We’ll use this new intent to get the sensor data.

create-an-intent-sap-conversational-ai

We’ll create 3 intents: One to ask about temperature, one to ask about humidity, and one to ask about climate. This last one will respond with both temperature and humidity. Let’s start with Ask temperature:

create-your-intent

Click the Create Intent button. Click your new intent to select it and input your expressions.

In the Add new expression field, start to write new sentences that make sense for this intent.

CAI may suggest other expressions to enrich your intent. Analyze these suggestions and choose the ones you think make sense.

enrich-intent-sap-conversational-ai

Click on ADD to add these new expressions. The more expressions you enter, the more precise your intent will be. Do the same for the other two intents, humidity and climate.

Examples of expressions used for humidity:

  • tell me moisture
  • give me humidity
  • level of humidity
  • i want to know the humidity

Examples of expressions used for climate:

  • how is the place in the room
  • how is the air conditioning in the room
  • give me room climate information

Now go to the Build tab and create a new skill. Click on the + Create Skill button.

Enter a name for your skill. (We’ll use this skill to ask about temperature, humidity, and climate.)

create-new-skill-sap-conversational-ai

Click on your skill ask-climate and go to the tab Triggers.

Here, choose your 3 intents as triggers for this skill, as below:

intent-sap-conversational-ai

Go to the tab Actions and click on the button ADD NEW MESSAGE GROUP.

Click on ADD CONDITION to trigger messages.

In the if field, enter your intent, in this case @ask-temperature, and click on CALL WEBHOOK.

Select the POST option and fill the URL with your service URL (XSJS in SAP HANA).

For temperature we need to use the URL with “sensortype” parameter with value 1: https://iotdbp999999trial.hanatrial.ondemand.com/IoT/data/get_sensor_data.xsjs?sensortype=1

ask-temperature

Click on SAVE.

Repeat this same process for @ask-humidity and @ask-clima with corresponding URL.

You’re now done! Congratulations! Now you can test your chatbot and consult your sensor data.

Conclusion

This tutorial really shows the capabilities of the SAP Conversational AI bot building platform. You can connect your own chatbot with both SAP and non-SAP tools, including physical devices that we’re using in our everyday life.

With the chatbot we’ve created, you can now easily ask information about temperature and humidity inside a room.
You can see and fork this bot by following this link and the files used in this one.

Of course don’t hesitate to check our tutorials on our blog if you want to try other integrations.
Happy bot building 🤖

Want to build your own conversational bot? Get started with SAP Conversational AI !

Follow us on

Leave a Reply to Lars Vlyminckx
Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.