How to build an IoT app on top of SAP Cloud Platform IoT Application Enablement
Introducing
In this blog i want to continue the little walktrough which we´ve started last month about SAP Cloud Platform IoT service in combination with SAP Cloud Platform IoT Application Enablement:
Scenario Architecture
In the picture below you can see we´ve still connect our device to SAP Cloud Platform IoT services (1) and we´ve successfully build up the virtual(digital) twin of our device (2).
We want now start to develop a little app in the “neo” environment wit the SAP Web IDE (3) by the usage of the SAP IoT AE API´s:
Update/Change
As you can see in this blog by Lukas Brinkmann we´re now able to send commands from SAP CP IoT AE to IoT service an finally to our device.
Please check the “Release Notes” here for a little change of the “attribute type”:
A final example of an adjusted configuration can be found in my GitHub repository here
Futhermore you can find a python based prgramm (runs on your local pc or an Raspberry PI) which use publish/subscribe with MQTT here:
https://github.com/fableh/iot_python_scripts
If u execute the script and fire a command back to the device:
…. we can now also se these commands in the console if we succesfully subsribed to the mqtt topic:
For more examples please visit also the SAP IoT Starte Kit, especially for the “Cloud Foundry” ennvironment for a lot of more examples:
https://github.com/SAP/iot-starterkit/tree/master/cf
IoT App development with SAP Web IDE Full Stack
Prerequisite: Before we can satrt the the development we need to check/create some destinations in the “neo” environment, These destinations are required to access the “iot data” from the “cloud foundry” environment. The setup of these destination is part of the onboarding process of your SAP CP IoT AE tenant.
A detailed documentation about the config can be found here.
After we have checked the destinations we can now execute the “SAP Web IDE Full Stack” in our SAP Cloud Platform Neo Environment.
For the usage of the SAP CP IoT Application Enablement features/templates you need to enable this as described here.
Now we can create a new template based “IoT Application” by using the wizard:
Afterwards we define teh basic information for our app:
Project Name | CPU_usage |
Namespace | syc.iot.notebooks |
Titel | MyApp |
Reference to the OData destination, which allows us to fetch the “Thing Data”:
Afterwards we can now search our property set(s):
In upcomming next screens we can now first decide which views we want to have in our app (we choose here all):
And finally we have now the chance to define for each view which properties we want to display:
After we run the application for a first test we got the following result:
But if we now want to click on our “Thing” to locate it in the map, something is missing.
Based on this it´s required to enhance our thing with some additional data.
Enhance the “Thing Data”
To specify the “location” of our thing we have two options one via the “Thing Modele Application” or via the “SAP CP IoT AE API´s”:
UI:
The API way (my choice);
Fetch the CSRF token
First of all we need to Fetch the CSRF Token and setting this globally in our Postman Environment:
URL: https://{{tenant_id}}.iot-sap.cfapps.eu10.hana.ondemand.com/appiot-mds/Things
Maintain your “Postman Environment” for SAP CP IoT AE:
Create the Location
URL: https://{{tenant_id}}.iot-sap.cfapps.eu10.hana.ondemand.com/location/Locations
{
"basicData": {
"tenant" : "<TENANT ID>"
},
"locationData": {
"streetName": "Heinrich-von-Stephan-Straße",
"houseNumber": "1-5",
"cityName": "Göttingen",
"postalCode": "37073",
"country": "DE",
"countryDescription": "Germany",
"longitude": 9.92829,
"latitude": 51.53708
}
}
Response (Location ID):
https://location.cfapps.eu10.hana.ondemand.com/Locations('3C8C03C00CDE451092E293EDDF22489F')
Create the “Business Partner” for the Thing
URL: https://{{tenant_id}}.iot-sap.cfapps.eu10.hana.ondemand.com/tenant/Persons
{
"person":
{
"basicData": {
"tenant": "<TENANT ID>",
"etag": "1"
},
"personName": {
"formOfAddress": "0002",
"formOfAddressDescription": "Mr.",
"familyName": "Lehmann",
"givenName": "Fabian"
},
"communicationData": {
"emailAddress": "fabina.lehmann@sycor.de",
"mobilePhoneNumber": "+49111111111",
"landlinePhoneNumber": "+49222222222",
"streetName": "Heinrich-von-Stephan-Straße 1-5 ",
"cityName": "Göttingen",
"postalCode": "37073 ",
"country": "DE",
"countryDescription": "Germany",
"region": "03",
"regionDescription": "Lower Saxony",
},
"objectGroup": "<object group>"
},
"employingOrganization":
{
"businessPartnerID": "<organization id>"
},
"userActive":"true"
}
Response (Person ID):
https://tenant-administration.cfapps.eu10.hana.ondemand.com/Persons('9E1B3941B70E47338276B3E4C9F1220D')
Update the Thing with the location and person id
URL: https://{{tenant_id}}.iot-sap.cfapps.eu10.hana.ondemand.com/appiot-mds/Things('442414166D8D4F79B809054EBE3B45C4')
{
"_id": "442414166D8D4F79B809054EBE3B45C4",
"_externalId": "FabiansZBook",
"_alternateId": "ZBook",
"_name": "FabiansZBook",
"_description": {
"en": "FabiansZBook"
},
"_thingType": [
"iotae.sycor.syc.sap.blogs.notebooks:ZBook"
],
"_location": "3C8C03C00CDE451092E293EDDF22489F",
"_customer": "9E1B3941B70E47338276B3E4C9F1220D",
"_objectGroup": "D34AF712DFD34A77A92E9A26652D227E"
}
Check the result in the SAP Web IDE “preview”
If we now enter the application again, we can see that our device is now located as defined and has assigned our “Person” as “Contact Info”:
We can now enter the different screens to see more details:
Summary
As u can see with SAP IoT Application Enablement, it´s really easy to develop an IoT App to visulaize your “Thing Data”. Futhermore we´ve seen how we can enhance our Thing with additional data e.g. from core applications like S/4 HANA.
In the next blog we want to cover the follwoing
- Deploy the app to “SAP Cloud Platform Cloud Foundry Environment”.
- Developing an second application were we visualize routes on the map.
- Creating events via the SAP CP IoT Application Enablement API.
Some links
SAP Cloud Platform Application Enablement
What´s new in SAP Application Enablement
SAP CP IoT AE – Reuse Controls and Templates
IoT Application Projects in SAP Web IDE
Used python script to send and retrieve data
cheers,
Fabian
Hi Fabian,
Thank you for writing these wonderful blogs. Waiting for other blogs in this series.
I do not know if this question is answered already, but want to know what is the best way to combine SAP IoT Application Enablement Thing data with SAP S/4HANA data?
can we do this in SAP IoT Application Enablement Thing Modeler? or in SAP Web IDE?
Could you please share some insights?
Thank you and Best Regards,
Venu
Hi Venu,
from my perspective the perfect integration between S/4 HANA and SAP CP IoT AE works with SAP CP Integration (aka HCI).
I´am using this e.g. also for the integration with SAP hybris C4S or other SCP related services lile workflow services or business rules service,
U can chek these little blog series which covers this as well:
https://blogs.sap.com/2017/08/01/connect-a-device-to-the-sap-cloud-platform-internet-of-things-cloud-foundry-environment-part-1/
As u can see in the API Hub S/4 HANA Cloud provides a wide range of API´s as well: https://api.sap.com/shell/discover/contentpackage/SAPS4HANACloud?section=ARTIFACTS
cheers,
fabian
Thank you Fabian
Hi Fabian,
Many thanks for the detailed blogs and documentation on the IoT 4.0 platform.
We have been trying to connect to the IoT 4.0 platform to send data via MQTT. We are attempting the Internet of Things gateway cloud platform for the connection. We have changed the hostname in the “Gateway_3_IoT Gateway MQTT_configuration.xml” file to our tenant hostname and deployed it. The <hostname> section currently looks like this “Companyname.eu10.cp.iot.sap” throughout the XML file. We have downloaded the .key and .cert file and are using it in the python code(Thanks for the code btw :o).
We want to send some static data and view it in the device under data visualization. The Paho-MQTT library is providing callbacks on connection, subscribing as well for publishing but no callback for on_message is coming through. The data is not being published onto the subscribed topic as well on the 4.0 platform. A sample output is provided for your consideration. Please let us know if there’s something that we are missing or not doing right?
Many Thanks,
Avinash Manoli
Hi Avinash Manoli,
first of all can you tell me how you want to connect the SAP Cloud Platform IoT service.
Did you use the GW "edge" component, or the direct connection to iot service.
Why do u have changed sth. in this file “Gateway_3_IoT Gateway MQTT_configuration.xml”
Normaly in my scenario i connect directly via MQTTS (cert based auth) to SAP CP iot service, no need to change any config file.
br,
fabian
Hi Fabian,
Many Thanks for your reply.
We are attempting the direct connection to the IoT service and NOT the Gateway Edge Method. I have reverted the changes made to the XML File and the hostname component now reads as <localhost>.
The situation remains the same though. The on_connect call back returns an “rc” value of 0 indicating successful connection, The on_subscribe callback returns a “granted_qos” of 0 but the message does not get published.
We have been able to successfully send a payload to one of the devices using the “Eclipse Paho windows client” and we can view the data in the IoT platform under data visualization. When we attempt to send the same payload to the same device over the python code, we are unable to do so. This in spite of having a successful connection established. I have tried the code in Python 2 as well as Python 3. What do you think is going on here ?
The Output and the Entire code I’m using is given below. Appreciate any of your thoughts on the matter.
[EDIT]: The on_log function gives the following output. Acknowledgements are being received for connection as well as for subscription
Many Thanks,
Avinash Manoli
Hi Avinash,
so i´ve tried this with the script which i mentioned in the blog 5 minutes ago and it works perfect to send a message from SAP CP IoT service perfect to your device/notebook:
SensorType Definition:
Started script:
Requested API call to the device:
HTTP POST: https://<tenant>/iot/core/api/v1/devices/<device id>/commands
hopefully this helps.
br,
fabian
Hi Fabian,
Many Thanks for your reply. I have now been able to fix the issue. Previously I was sending the payload in the same manner as i was sending it through the PAHO Java client. I believe that was the issue. Once I changed it and sent it in the format that you send the payload, the data is now populating under the device.
Many Thanks,
Avinash Manoli
Hi Avinash,
you´re welcome.
br,
fabian
Hi Fabian,
Any idea if it's possible to fetch commands on the device with REST api? If possible, we would have to poll for commands I guess?
Kr,
Wouter
Hello Wouter, Gateway REST exposes an endpoint for getting the device commands. See more at https://help.sap.com/viewer/3d1c4ea7d7344d6fb5b0ca602d3ee9b7/Cloud/en-US/eebbcd06a9be4fa4ac1702db3a52380c.html
Regards, Anton
Hi Wouter,
does the answer from Anton resolved your issue?
thx,
fabian
Yes! Perfect 🙂 Thanks!