NOTE: This document is in DRAFT version, something can be missing or wrong. If you intend to re-create this scenario, you are smart enough to go through without a real step-by-step guide. Anyway, if you find errors, missing steps or typos as well as if you need some help to implement the scenario, feel free to contact me. I really appreciate your help and your feedbacks.
DISCLAIMER: This repository doesn't want to represent the best way to develop on SAP HCP or on RaspberryPi, but it's an attempt to make things work and, it works. Code can be improved in readability, efficiency and design.
This is the repository for a real and complete end-to-end scenario (Greenhouse) built using the SAP Hana Cloud Platform IoT Services (SAP HCP IoT) and a RaspberryPi as a gateway.
The document and the code snippets will provide information on how to quick re-use it, replicate what I built, improve it and extend it.
The entire Greenhouse scenario is based on two separate applications:
- greenshouse: Sensor data readings, data transmission via RaspberryPi to SAP HCP and an HTML5 UI/Dashboard developed with SAPUI5 on SAP HCP.
- remotecontroller: Receiving the messages sent to the device. It acts as a controller to switch on/off a lamp, open/close the roof consuming messages sent to the devices.
Here a list of components used to build the scenario:
- 1 Raspberry Pi B+ or Raspberry Pi 2
- 1 AM2302 sensor
- 1 Breadboard
- 3 leds (Red, Green and Yellow)
- 3 resistors 330Ω
- 1 T-Cobbler cable
- 1 DC 5V Relay module
- 1 Lamp
- 1 Continuous Rotation Servo
- 1 Additional 5V (2000mA) power supply
- Some jumpers
- Plexiglass box (W: 50cm H: 25cm)
In this section you can find a quick list of prerequisites, software and hardware.
Discussing how to connect leds, lamp, servo motor and so on to the RaspberryPi as well as the SAP HCP IoT architecture, features etc. are outside the scope of this document; There are lots of resources available on line.
The RaspberryPi acts as a gateway. All the code is written in Python 3 (v. 3.2) and uses some python libs. Take a look to the following links to install all the requirements:
Getting started with SAP HCP IoT following the step-by-step guides:
In this section you can find steps to replicate the entire scenario. Make sure all the above prerequisites are in place.
To handle the AM2302 sensor protocol (as well as for of DHT11 and DHT22) will use an utility developed by Adafruit that, among other things, develops and sells many interesting accessories for RaspberryPi.
The sources of Adafruit also contains a library to allow Python code to access the sensor data. Unfortunately, at the moment, the library has not been updated to support Python 3. We will use a class in Python 3, DHTUtils, as a wrapper that implements methods to make the call to the library Adafruit.
First of all, we clone the Adafruit_Python_DHT Github repository and save the code in a Adafruit
folder within the home
directory of pi
user:
cd ~/
mkdir Adafruit && cd $_
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
The, we clone this repository:
cd ~/
git clone https://github.com/indaco/saphcpiot-greenhouse.git
Create a folder to save the code inside it:
cd ~/
mkdir code
Move code for the greenhouse app to the right folder:
cd ~/
mv ~/saphcpiot-greenhouse-master/apps/greenhouse/raspberrypi ~/code/greenhouse
and adapt the configs.py with your information.
Copy and paste code for the remotecontroller app to the right folder
cd ~/
mv ~/saphcpiot-greenhouse-master/apps/remotecontroller/raspberrypi ~/code/remotecontroller
and adapt the configs.py with your information.
Both greenhouse and remotecontroller save logs in their respective "logs" folders. You should not see output on the shell.
Use the following documentations as step-by-step guide to:
-
Create Device Information in Internet of Things Services Cockpit
- after device and device type creations, it's time to create message types to and from RaspberryPi. Create them as shown in the figures
-
[Deploy the Message Management Service (MMS)](Deploy the Message Management Service (MMS)
- Create a new XS App for the Greenhouse
- Copy all the code in
~/saphcpiot-greenhouse/apps/greenhouse/hcp/xs/iotmmsxs
to the HANA XS app previously created - Adapt the following files to reflect information from your account and your HANA Schema:
- Execute the following SQL Script to grant accesses (refer to Grant Access) documentation
call "HCP"."HCP_GRANT_ROLE_TO_USER"('<user_id>trial.iotmmsxs::iotaccess', '<user_id>');
- Open the Java project
~/saphcpiot-greenhouse/apps/remotecontroller/hcp/java/it.sapiotlab.greenhouse.remotecontroller
with Eclipse - Adapt the index.html file at the bottom of the page (
createModel
method) to reflect information from your SAP HCP IoT Cookpit - Compile with Maven and Deploy
Two different scripts are provided to start and stop the applications.
START:
STOP:
Execute them as sudo
, e.g.:
cd /home/pi/code/greenhouse/
sudo ./start_greenhouse.sh
To access the RaspberryPi via SSH or other network protocols without a monitor and moving from network to network, we need to know the IP address. The startup_mailer.py utility do exactly this task. It's based on RPi Email IP On Boot Debian documentation by Cody Giles.
Crontab tasks are used to automatically start and stop the scenario.
Application | Wake-Up | Sleep | Days |
---|---|---|---|
Greenhouse | 8:00 AM | 8:00 PM | Monday - Friday |
RemoteController | 8:05 AM | 7:55 PM | Monday - Friday |
Run crontab -e
and paste the following entries:
# Greenhouse
0 8 * * 1-5 /home/pi/code/greenhouse/start_greenhouse.sh > /tmp/start_greenhouse.log
0 22 * * 1-5 /home/pi/code/greenhouse/stop_greenhouse.sh > /tmp/stop_greenhouse.log
# Remote Controller
5 8 * * 1-5 /home/pi/code/remotecontroller/start_remotecontroller.sh > /tmp/start_remotecontroller.log
55 19 * * 1-5 /home/pi/code/remotecontroller/stop_remotecontroller.sh > /tmp/stop_remotecontroller.log
You can find the cron-file.txt
for both the applications.
- SAP Hana Cloud Platform
- SAP HANA Cloud Platform Developer Center
- SAP HANA Cloud Platform Internet of Things (IoT) Services
- SAP HANA Cloud Platform IoT Starter Kit
- Turning on an led with your RaspberryPi's GPIO pins
- Controlling Relay Boards using RaspberryPi
- Controlling Servo Motors
All the code is released under Creative Commons CC0 1.0 Universal (CC0 1.0) license