Skip to Content

Send the CPU usage data to SAP IoT Application Enablement


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. If you get stuck with steps from this tutorial, then you can skip to the next chapter and use someone else’s device data in your app.


Step 1: Check prerequisites

Your computer, which is going to play a role of the IoT device, should have:
- Python - a programming language:
- requests - HTTP library for Python:
- psutil - the (process and system utilities) is a cross-platform Python’s library:
- connection to Internet

Step 2: Prepare a file with a script to collect and send data

On your computer create a file and paste the following Python code into it.

import requests #
import psutil   #
import time, sys, platform

hostiotmms = ''
apiiotmmsdata = '/'
msgtypeid = 'a0c0c766ab65e08432a2'

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)

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+"}]}"
	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)


except KeyboardInterrupt:
	print("Stopped by the user!")

Save the file.

Step 3: Update the code and run the program

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

Run the script with the command.


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.

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

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:'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


Step 6: Explore IoT APIs in SAP API Hub

To better understand IoT Application Enablement APIs you can go to SAP API Hub at and review and execute different APIs in its sandbox. As this sandbox has the same backend you will find your things again here.

Next Steps

Back to top