Skip to Content

Send the CPU usage data to SAP IoT Application Enablement

Previous

Send the CPU usage data to SAP IoT Application Enablement

By Vitaliy Rudnytskiy

Send the CPU usage data to SAP IoT Application Enablement via SAP Cloud Platform IoT service for Neo environment

You will learn

You will learn how to use Python script to send sensor data to SAP Cloud Platform.

Details


Step 1: Check prerequisites

Your computer, which is going to play a role of the IoT device, should have:
- Python - a programming language: https://www.python.org/downloads/
- requests - HTTP library for Python: https://github.com/requests/requests/#installation
- psutil - the (process and system utilities) is a cross-platform Python’s library: https://github.com/giampaolo/psutil#quick-links
- connection to Internet

Please log in to access this content.
Step 2: Run python script to collect and send data

On your computer create a file ` ` and insert the following code into it.

import requests # http://docs.python-requests.org/en/master/
import psutil   # https://pypi.python.org/pypi/psutil
import time, sys, platform

hostiotmms = 'iotmmsa2667617c.hana.ondemand.com'
apiiotmmsdata = '/com.sap.iotservices.mms/v1/api/http/data/'
msgtypeid = '3a852d7951dd7ecbe217'

deviceid = '591188FC5CEF413E9D5AE3F641429BB5'
authtoken = '7461f8d7385_fake_179d36fcfd8'

url = "https://"+hostiotmms+apiiotmmsdata+deviceid

def readsensors():
	global d_pctCPU
	d_pctCPU = psutil.cpu_percent(percpu=False, interval = 1)
	return

def postiotneo ():
	global d_pctCPU

	s_pctCPU = str(d_pctCPU)
	d_tstamp = int(round(time.time()))

	s_tstamp = str(d_tstamp)

	print("\nValues to post: ", d_pctCPU, d_tstamp)

	payload = "{\"mode\":\"sync\",\"messageType\":\""+msgtypeid+"\",\"messages\":[{\"cpu_usage\":"+s_pctCPU+",\"tstamp\":"+s_tstamp+"}]}"
	headers = {
			'content-type': "application/json",
			'authorization': "Bearer "+authtoken,
			'cache-control': "no-cache"
			}

	print("Payload to post: ", payload)

	response = requests.request("POST", url, data=payload, headers=headers)

	print(response.status_code, response.text)

	return

try:
	while(True):
		readsensors()
		postiotneo()
		time.sleep(2)
except KeyboardInterrupt:
	print("Stopped by the user!")

Save the file.

Please log in to access this content.
Step 3: Update and run the code

Update deviceid and authtoken in the code with IDs you copied in the previous tutorial when created the thing for your computer.

Run the script with the command.

python computer_iotdevice_cpu.py

If all mentioned prerequisites have been satisfied, then you should see outcome like:
running python code

The return code 200 means that the message was received and processed by the IoT service in SAP Cloud Platform.

The return code 202 means that the message was received and queued for processing by the IoT service in SAP Cloud Platform.

Codes 4xx and 5xx mean errors.

Please log in to access this content.
Step 4: Check posted values in the Thing Modeler

Go back to the Thing Modeler application from the previous tutorial. Make sure you have the proper package computeriotdevice selected and find your computer on the list of Things.

Go to Measured Values and expand resource_sensors to the cpu_usage. You should see the value (like 22.2 on the screen shot). It is updated only once a minute, so you may need to wait a bit for it to appear and then to change.

Posted value

You can find corresponding value in the output of the Python’s script as well.
value in Python

Please log in to access this content.
Step 5: Check posted values in the API

The power of SAP IoT Application Enablement is in its rich set of APIs available to build powerful customer applications on top of data from devices.

E.g. to check last 5 values posted open following URL in the browser, like Chrome: https://sap-iotaeexplore.iot-sap.cfapps.eu10.hana.ondemand.com/appiot-mds/Things('591188FC5CEF413E9D5AE3F641429BB5')/sap.iotaeexplore.computeriotdevice:generic_computer/resource_sensors?timerange=1H&$top=5

You need to modify the thing id from 591188FC5CEF413E9D5AE3F641429BB5 to your own. Properly formatted URL will return results like this to authorized user.
Values in API

Please log in to access this content.

Optional

Step 6: Explore IoT APIs in SAP API Hub

To better understand IoT Application Enablement APIs you can go to SAP API Hub at https://api.sap.com/#/shell/discover/contentpackage/IOTAE and review and execute different APIs in its sandbox.

Please log in to access this content.

Next Steps

Updated 07/08/2017

Time to Complete

10 Min

Beginner

Prerequisites

Next
Back to top