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
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.
CAUTION:
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.
Just unzip it on your machine and open a console to connect to your Raspberry Pi like I’ve described in my first blog post of this series.
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.
cd /home/pi/myscripts
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.
nano Temperature.java
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
nano Temperature.java
Check proxy
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
Now look into the variable called SERVLET_URL_cloud and change it to the URL of your application that you’ve deployed in the second blog post of this series.
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.
ls /sys/bus/w1/devices/
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:
javac Temperature.java
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:
ls -l
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.
Next Steps
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.
Have fun.
Best,
Rui
Received the sensors. Solding and connecting them is for the weekend.
Curious to see what will happen
It's up and running.
I had fun putting it together.
Looking forward to the next parts.
Congratulations! More blog posts coming soon.
Best,
Rui
Perfect Step-by-Step tutorial.
Here is my result:
Note: You don't have to solde the components:
Cheers,
Fouad
Cool to to see this up and running on your side.
Very adventurous setup of your temperature sensor 🙂
Best,
Rui
Hi Rui,
I'm also following the scenario and it works well. Hopefully we will see the next blogs soon.
I use the eclipse plugin LaunchPI to run and debug the PI application directly from Eclipse.
This works very easily without to code on the pi.
Best
Robert
Hi,
It's a great project!!
I adapted the Raspberry piece a bit by using Python and send the URL request to add data via that way. This makes a maybe a bit easier.
Best,
Michael
Nice. That's exactly what I intended with this blog post series: people start modifying the code and adapt it to their needs and their know-how.
Hope you're having fun with this series.
Best,
Rui
Hi Michael, We also try to send an URL request to add data from Python. Can you tell us, what kind of security/Authentication you used? We successfully test with postman, but having trouble with sending the request from the Raspberry Pi.
Thank you,
Hendrik
Hi Hendrik,
I didn't use anything special in terms of security or authentication. I simply used the urllib2. from python and built the ULR string as per the tutorial. I started the script via sudo and used & to make it run in the background with sudo authorization. You can find the code including some streamer logging here: https://github.com/mliebski/Sensors/blob/master/temp_logging.py
I found that even easier than using JAVA.
Cheers,
Michael
Hi Rui,
I challenged myself. I altered the programs in order to be more flexible.
The program running on the Raspberry Pi now first reads the cpuinfo file, located in the /proc directory and retrieves the serial number of the Pi. This is used as hardware ID.
It then checks on HCP if the sensor is known. If not, it asks for a device name and description, stores this at HCP and requests the ID on HCP.
Same for the external sensor(s). I managed to connect 5 of them to the Pi.
It reads the directory /sys/bus/w1/devices and searches for directories starting with “28-“, the id’s of the temperature sensors and uses the name as hardware ID.
After that, the program runs as usual, sending the readings of the connected sensors to the cloud.
As I am not a java-developer, it took me a long time to figure it all out. It is probably not the best or most elegant solution, but it works 🙂 .
In order to retrieve the json information coming back from the cloud, I used the json-simple libraries (couldn’t get the gson-lib working). These can be found at https://code.google.com/p/json-simple/
For those interested in my solution, the eclipes-projects can be found here:
https://onedrive.live.com/?cid=55CEDAAF12202C3F&id=55CEDAAF12202C3F%21116
- Pi_onHCP is the cloud project
- pi_sensors the application for the Pi
Now on to the dashboard....
Kind regards,
Peter Lans
Hi Peter, nicely done. I've actually done similar things, too, like the automatic detection of sensors and the Pi serial number. You'll also see a few more things in next blog posts from me.
Meanwhile have fun playing with HCP and the Raspberry Pi.
Best,
Rui
Hi there!
I got stuck with sending data from Raspberry PI to HCP. The error msg the PI is giving is "401 <html><head><title>Error report</title></head></body><h1>HTTP Status 401 -..."
Config.py and insert.py files are in place and I could not find any issue from there. Any ideas what might be the cause?
Hi,
the 401 error codes indicates that the HCP site asks for authentication to open the called page. Did you set it up that way? If yes, the URL string needs to contain the authorization details.
Regards,
Michael
Hi,
thanks! I got the issue solved, authorization token was the problem.
Olli
Hi Olli,
Can you tell me how did you pass the authorization token?
I am facing the same problem.
Hi,
Thank you for the tutorial, it works like a charm. I was wondering, is there a way to export the data from the app to an excel/txt so I can analyse it using SAP Predictive Analytics?
Hi,
Thank you Rui.
A very detailed and interesting step by step explanation.
Unfortunately, I cannot find the Java App for R Pi attached,
Can you please share that.
Hi Rui and community,
I am looking for the file Temperature.java  ... attached to this blog post ... Where can I find that file?
Thanks!
Hey Rui and Community,
I am also looking for the Temperature.java file.
Can anyone please share it?
Would be awesome, i want to finish this great tutorial! 🙂
Regards,
Nadine
Hello, is it possible to post the code for Temperature.java? I really need this for my school project. Thanks in advance!
See my comment below. Let me know in case it doesn't work. If it does, I expect a picture from your school project here in this blog post 🙂
Best,
Rui
After a long time of trying, the code didn't work. The solution is that I wrote a python script which reads the data from the sensor and calls the webservice with the data.
Hi all, here's the content of the Temperature.java file that I found on my laptop. Hope it's the right version. Still amazed to see that many take this blog post series to do some experiments with SAP Cloud Platform.
Best,
Rui