Connecting a Raspberry Pi to SAP Cloud Platform IoT Services
In this short tutorial we will showcase the ease of connecting an IoT board from a Smart City component to SAP Cloud Platform IoT Service. We will be using IoTIFY’s Virtual IoT Lab, connecting a Smart trash can powered by a virtualized Raspberry Pi without the need of any physical hardware. Anyone can carry out this tutorial, there is no need to have physical IoT hardware, IoTIFY provides it for you!
So, let’s get to work!
First, we must create a free account at https://iotify.io/ and go to the Virtual Lab section.
We will select the Smart Trash can project, insert a name and click on Launch Project
After a couple of minutes, the project will have successfully loaded and we will click on Open.
Once inside IoTIFY’s virtual lab, we will see a GUI with a smart trash can that has an ultrasonic sensor attached to the lid of the trash can. To the right, we can see the information on how the IoT scenario is configured.
This smart trash can scenario is powered by a virtualized Raspberry Pi with the following connections:
- Pins 5 and 6 of the Raspberry Pi are connected to an HC SR04 ultrasonic Sensor.
- Pin 15 is connected to a switch that indicates if the lid of the smart trash can is open or closed.
- Pin 22 has a led connected to it. In our use case, we will light it up once the trash can is filled up to alert users.
Before we start configuring and programming our Virtualized IoT board, we must configure our IoT Service at SAP Cloud Platform. We will give for granted that you already have an account at SAP Cloud Platform. If you don’t, you can create it for free!
First we will go to our IoT Service Cockpit to create two new message types.
The first message type we will define will be used to indicate that our smart trash can Lid is open. This is necessary as having an open lid will completely change fill level measurements from the ultrasonic sensor attached to the lid. We will define it as follows:
- Timestamp: This is the timestamp of when the message was sent by our smart trash can.
- isOpen: Indicates if our Smart Trash can is open or closed. We will use an Integer.
After we create the message type, we will store the Message ID somewhere, as we will need it in the next steps.
The second Message Type we will define will be used to know the fill level of the trash can. The ultrasonic sensor, like physical ultrasonic sensors, only measures the distance from the lid of the trash can to the trash fill level. Hence we must transform this value to represent percentage fill level later on.
We will define the message Type as follows:
- Timestamp: This is the timestamp of when the message was sent by our smart trash can.
- Distance: Measured distance
After we create this second message type, we will store the Message ID somewhere, as we will need it in the next steps.
The next step is to create a Device Type. We will include both Message Types in this Device type, as shown in the following screenshot.
Finally, we must create a Device too. The Device Type for this Device is what we just defined in the previous step.
We will need to safely store the Device ID, as like the AuthToken prompted in the pop up window, as we will be using these in subsequent steps of this tutorial.
Once our Message Types, Device Type and Device are correctly configured at SAP Cloud Platform, we will proceed to program our Raspberry Pi powering the smart trash can over at IoTIFY.
Within the IoTIFY IDE for Virtual Lab we have access to the board via SSH. This is preconfigured for each project so you will not have to connect yourself.
The first thing we will do in our Raspberry Pi is update the urllib3 library we will need to connect to SAP Cloud Platform. To do this, we will have to click on the first of the three icons on the top left of the IDE, as shown in the following figure. This will open the console for us.
We should be in this view now:
In the console we will type in:
sudo pip install urllib3 –upgrade
In IoTIFY there is way to inspect the files within our Raspberry Pi by double clicking on your file of choice on the left bar of the IDE, as shown in the following figure.
To create a file we can right click on a directory and create a new file, clone a GitHub repository via a standard terminal command or simply use terminal commands to create a new file and then open it with sudo vi.
We will now have to create a new file that we will name config.py
In this file, we will configure the necessary parameters to POST to the IoT Service over at SAP Cloud Platform, where we intend to send our real-time data. It’s a standard configuration file that can be seen at: https://github.com/SAP/iot-starterkit/blob/master/src/examples/python/iot-starterkit-for-pi-and-grove-peripherals/template-config.py
We will configure the config.py file to fit our needs. In our scenario, it should look like the previous image where
- Line 4: Our account ID for SAP Cloud Platform
- Line 7: The host for the account at SAP Cloud Platform. If we are using a trial account, it should look like what is shown in the previous image.
- Line 11: User / Password credentials to access SAP Cloud Platform
- Line 14: Device ID. This was obtained in a previous step.
- Line 15: OAuth Token. This was obtained in a previous step.
- Line 17 y 18: Message ID. In our case we have two message IDs. This was obtained in a previous step
Once the config.py is finished, don’t forget to save it by clicking on File-> Save on the top menu of the IDE.
Nearly Finished!
Now we simply have to include the Python script to control our Raspberry Pi. Don’t worry, we have done that for you J
Check it out the at https://github.com/naraquev/SmartTrash
The script measures the fill level with the ultrasonic sensor and checks to see that the lid is open or closed. It also flashes a Red led once the trash can fill level is over a certain value. We have included comments within the code so you can follow it along.
In the same way we created the config.py file, we will have to create a SmartTrash.py file in the same directory.
We are ready and all set yo run our script. To run it, type in the console:
sudo python SmartTrash.py
Note: Remember you should be in the same directory as the SmartTrash.py and config.py files. Also, consider that if you have renamed SmartTrash can to another name, you should use that instead.
If we click on the WebView of IoTIFY IDE, we can now manually start chainging the sensor readings for the smart trash can. You can check that if the fill level is over 50cm, the red light should start turning on. You can also open and close the lid.
In order to stop the script, just go back to the console and press CTRL+C.
You can now go to your IoT Cockpit over at SAP Cloud Platform and check the received messages in your tables. If we have followed the guide, we should be seing our messages receive one at a time in real time.
Congratulations! You have successfully connected a Raspberry Pi for a Smart Trash can scenario with your SAP Cloud Platform account!
Hi Nicolas ,
Great blog and detailed explanation but i am facing issue regarding folder structure of config.py and smarttarsh.py.
can u share me screenshot of your project structure.
Regards
Shadan
You can put it under raspberry folder. Or navigate to the parent folder by command cd <folder name> in the console on execution.
Hi Nicolas- I have created the new files in folder smarttrash with names config.py & Trashcan.py and placed the code given in git. I am receiving a syntax error
pi@raspberrypi ~/smarttrash $ sudo python Trashcan.py
File "Trashcan.py", line 1 Skip to content
^SyntaxError: invalid syntax
Any help please
Reg
Siva
Hi There,
I have followed the guide thoroughly, however i get stuck at the last part.
When i try to access the IoT dashboard to view the messages i receive the following error: Access to the requested resource is forbidden (HTTP 403).
I checked that my credentials are fine, and i have also assigned the "IoT-MMS-User" role to my trial user ID. Please advise.
Regards,
Yuveer
Double check you have assigned your user ID "pXYZ" and not your account ID "pXYZtrial". Also be aware that role assignment takes effect with a new browser session, so it is recommended to close all browser windows and re-login.
IoT FAQ https://help.sap.com/viewer/7436c3125dd5491f939689f18954b1e9/Cloud/en-US/a012a3788b6e498a8971dd27c97ce6bf.html -> MMS -> Why does the MMS return HTTP status 403?
Thanks.
I am able to get the distance into the cloud database, however i am not receiving the Lid on/off information (in the form of 0 or 1). I get the error below:
[0e0ecfba-62af-45d5-9b32-903317a17e0f]"}
{"mode":"async", "messageType":"4347485aed849c69cf53", "messages":{"timestamp":"1504267137", "isOn": 1 }]}
{"msg":"The message does not conform to the given message type"}
How to i fix this?
The error message says "The message does not conform to the given message type". So, ensure that the message type "4347485aed849c69cf53" you have modelled conforms to what you are sending as payload: "timestamp" and "isOn" fields. Do you have them in that message type as fields? By the way, for "messages" attribute a JSON array is expected, not object. More about MMS Data Services [1]
[1] https://help.sap.com/viewer/7436c3125dd5491f939689f18954b1e9/Cloud/en-US/97854e1b1d544d0aa8efc724ff857186.html
Hi dear author
the URL you put in this article is not accessible.
https://github.com/SAP/iot-starterkit/blob/master/src/examples/python/iot-starterkit-for-pi-and-grove-peripherals/template-config.py
Do you have any updates?
Best regards,