RaspberryPi on SAP HCP – IoT blog series part 3: Sending sensor data from Raspberry Pi to your cloud-app
In the second blog post of this blog post series around Raspberry Pi and the SAP HANA Cloud Platform we’ve taken care of the receiving part in the cloud. If you’ve followed the instructions in the blog post you have now an app running on your free developer account which can receive sensor data and persist it and provide some output in JSON format for the user interface.
In this blog post we’ll now look into the code for the Raspberry Pi and will connect an external temperature sensor to our Raspberry Pi. That code will be sending the temperature data from the CPU inside the Raspberry Pi as well as the temperature from the external temperature sensor.
Table of Contents
- Connecting the external sensor
- The Java app for the Raspberry Pi
- Assign DB id’s of sensors to the ones on your Raspberry Pi
- Compile the Temperature.java file
- Send your sensor data to your app on SAP HANA Cloud Platform
- Next Steps
But first thing first. Let’s first tackle some hardware challenges.
Connecting the external sensor
This is the part where you need to have solding skills (or know somebody who has). Follow the instructions on this blog under the section “
Step Two: Connecting the Temperature Sensor” and connect your sensor to your Raspberry Pi.
Please be aware that this is hardware-stuff which actually can react in a physical way like getting hot, explode, burn you, etc.. So you either know what you are doing, find someone who can do it, buy it somewhere ready-made or just leave it. You do this at your own risk (which you’ve probably know before anyways, but just mentioning it here, so you don’t forget while reading 🙂
The Java app for the Raspberry Pi
You can find the code for your Raspberry Pi in the file I’ve attached to this blog post.
After logging-in to your Raspberry Pi change the current directory to the folder you’ve created for your scripts. If you did it, like I’ve done it, you’ll switch to your myscripts folder.
The next thing we do is to create the app on the Rasperry Pi. To create the corresponding Java file we create a file called Temperature.java with the text editor nano.
Your window should look more or less like this:
Now copy the content of the file you’ve extracted before on your machine into your clipboard. Switch back to the console with the opened nano editor and paste the clipboard into the window.
Now press the command and X key and the editor will ask you to either press Y to save the file or N for not doing it. Press Y and hit the return key to save the file.
Before you can move forward you need to adapt the code so it works with your account. So let’s open the Temperature.java file again in nano
First thing you need to do is adapt the USEPROXY variable in the Temperature.java file. Set it to false if you are working from home, as most probable you don’t use a proxy server at home. If you do have a proxy server for accessing the internet, set it to true.
Adapt URL of Java app on your SAP HANA Cloud Platform account
Get Hardware ID of the external temperature sensor (optional)
In case you want to use the values of the external temperature sensor, you need to find out it’s ID before. To do that you should ensure you’ve added the modules for the sensors as described in the first blog post and also connect the sensor to the Raspberry Pi as described above.
Now move to the folder /sys/bus/w1/devices/ and search for a subfolder there called 28-xxxxxxxxxxx. That folder name is the id of your sensor.
If you don’t get any results or an error message saying “No such file or directory” you’ve either not connected the external sensor, or you’ve connected it wrongly to your Raspberry Pi.
Once you have the id of your sensor you need to assign it to the variable called SENSOR_HARDWARE_ID in your Temperature.java file.
Assign DB id’s of sensors to the ones on your Raspberry Pi
In the past blog post you’ve already created at least one sensor in your database. What you need to do now is to get the database ids for 2 sensors. So if you haven’t done, yet, create at least two sensors in your app like described in the past blog post.
If you call your app you should see something like this
Take the number after the “id” field and assign them in your code to the variables SENSOR_DB_ID_CPU_TEMP and SENSOR_DB_ID_TEMP_SENSOR.
Compile the Temperature.java file
After doing all the changes you need to save the file in nano. Now you need to compile the file. To do that enter this command in your myscript folder:
Should you get an error message you might have messed-up the code. Try again 🙂
If there are no errormessages a new file was created called Temperature.class. Check it by listing up the files in your myscript folder:
Send your sensor data to your app on SAP HANA Cloud Platform
The last thing missing now is to actually run the app. To do it simply we call the Java app like this
java Temperature 10
The 10 at the end tells the app that it should iterate through the app 10 times.
If everything worked fine you should now be able to see the sent sensor data in your Java app on your account.
Now that we have worked on the hardware side of this, we’ll work on the user interface to provide a nice looking dashboard for Rui’s Fish Import/Export Inc..
That’s what we’ll do in the next blog post.