Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
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:

  1. Timestamp: This is the timestamp of when the message was sent by our smart trash can.

  2. 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:

  1. Timestamp: This is the timestamp of when the message was sent by our smart trash can.

  2. 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-grov...

 



 

We will configure the config.py file to fit our needs. In our scenario, it should look like the previous image where

  1. Line 4: Our account ID for SAP Cloud Platform

  2. 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.

  3. Line 11: User / Password credentials to access SAP Cloud Platform

  4. Line 14: Device ID. This was obtained in a previous step.

  5. Line 15: OAuth Token. This was obtained in a previous step.

  6. 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!


 

 
8 Comments