It is one of many examples of different devices supplying environmental sensor data (temperature, humidity) to SAP Cloud Platform (SCP). Here Raspberry Pi 3B device is used with connected Sense Hat. Sensor data will be send synchronously using HTTPS REST protocol.
Prepare your Raspberry Pi deviceIn this project I am using Raspberry Pi 3B with Sense Hat connected to it. Sense Hat library is usually preinstalled on this model.
The code is executed with Python 3 and is using Requests library available from http://docs.python-requests.org/en/master/
Make sure you have free developer account in SAP Cloud PlatformIf you do not, please follow this tutorial first: https://www.sap.com/developer/tutorials/iot-part6-hcp-services.html
Configure virtual device for Raspberry Pi in SCPFollow the tutorial https://www.sap.com/developer/tutorials/iot-part7-add-device.html and then create a separate virtual device for your raspberry Pi. Instead of "myDevice" you can call it "raspberrypi".
Note down technical ID of this raspberrypi virtual device and its assigned authorization token. You will need them in the next step.
Modify the codeModify attached Python code (at the bottom of this page). You will need to add your Message Type ID, Device ID, etc. The lines in particular you will need to modify are the following:
You should modify:
- The
accountOwner
line to add your own SAP Cloud Platform Account User ID which starts with either anS
,P
,I
,D
orC
.
- The
msgtypeid
is the ID of the message type you created, and not the name of the message type.
- The
deviceid
is the ID of the device you created, not to be confused with thetoken
- The
authtoken
line contains the token of the device (not the device type!). You received it in the pop-up window after creating your device. But if missed it, then regenerate this token from Device screen once again.
Save the file as scpiot_neo_rpisensehat_http.py
Run the code and check the resultsOnce you have saved your file run the following command:
python3 scpiot_neo_rpisensehat_http.py
If everything is correct, then you should see output similar to this one. The loop reads and posts values every f and the green arrow flying through HAT's LEDs with every successful post to the SAP Cloud Platform.
You can preview received data in the cloud in Message Management Service Cockpit. There is the OData API link to now use this data in your applications!
Press Ctrl+C to stop the execution of the program.
Have a good weather!
Comments