HANA Cloud Platform and the Internet of Things (IoT) with 1Wire, MQTT and Raspberry Pi – Part 1
In this blog post I want to share my experience with the HCP IoT Service. It’s gonna be a bit of handson with wires and sensors and a bit of coding and configuration. I started this to monitor my gas and solar heating system and to find out what IoT offers for such a lightweight use case.
First things first: IoT is one of the recent buzzwords in the IT lingo and in general it means – make your sensors and devices accessible/readable by IP based services.
And the first challenge is – how to programaticaly read a sensor value.
There is no general answer to this questions – if you are lucky your device communicates via a common bus protocol and you get libraries to communicate with the bus. If this is the case you have already achieved a big milestone. In part 1 we will talk about this.
The second step would be to pass this value on to either a more generic protocol or process it directly. (Mqtt)
And in the third part we will use HCP IoT to actually pass this data on to HCP IoT.
In this example I use temperature sensors communication via the 1Wire Bus and transform these temperature readings to the Mqtt protocol. Mqtt has evolved from the SCADA protocol and was standardized in 2013 by OASIS to be a standard protocol for the Internet of Things.
The sensors here are connected directly to a Raspberry Pi (Raspbian Jessie) and the total costs of the setup (if you have a Raspi handy) is about 15€. (2€ for each sensor and a bit for the wires and a resistor)
I don’t want to get too much into the wiring as others explain it better but just so much:
The 1Wire temperature sensor DS18B20 has three pins: Ground, Data and Power.
Ground will be connected to any GRND GPIO of the raspi. The data pin defaults to GPIO4 (you can use other GPIO when specified in /boot/config.txt) and for the power we use 3,3V.
Between the data pin and the 3,3V we need a “pull up” resistor of 4,7kΩ(between 1,1kΩ and 4,7kΩ will do) https://www.raspberrypi.org/learning/physical-computing-guide/pull_up_down/
A quick example you will find in section two here: http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/
Or if you speak german I found thes pages very useful: https://wiki.fhem.de/wiki/Raspberry_Pi_und_1-Wire or http://www.netzmafia.de/skripten/hardware/RasPi/Projekt-Onewire/index.html
It is to note that (with kernels >3.18.3) you only need to add one entry in /boot/config.txt
# activating 1-wire with pullup dtoverlay=w1-gpio-pullup
But if you need to use a different GPIO than the default GPIO4 add the line below and change the GPIO to your pin:
After editing config.txt you need to reboot and should be able to access the PIN afterwards.
In /sys/bus/w1/devices/ you should now find some entries:
root@pi2:/sys/bus/w1/devices# ls 28-0000086a0f09 28-0000086a2210 28-0000086a2cc9 28-0000086a32f5 28-0000086a8b2d 28-0000088722b2 w1_bus_master1
Where w1_bus_master1 is a default directory and the other directories hold sensor data. Here I have 6 temp sensors and I can get the values via a simple cat command:
root@pi2:/sys/bus/w1/devices# cat 28-0000086a0f09/w1_slave 40 01 4b 46 7f ff 10 10 1d : crc=1d YES 40 01 4b 46 7f ff 10 10 1d t=20000
The temperature here is 20°C.
Here you will find a small python script that makes it more comfortable to read the value(s) via shell
root@pi2:/opt/scripts# ./1Wire.py Connected with result code 0 28-0000086a2210: 36.562 °C 28-0000088722b2: 34.875 °C 28-0000086a0f09: 20.0 °C 28-0000086a2cc9: 40.875 °C 28-0000086a32f5: 18.812 °C 28-0000086a8b2d: 20.125 °C
So much for Part 1. We connected a 1Wire sensor to the Raspi and configured the OS to load the driver. We are now able to read the temperature values.
In Part 2 we will have a look how to pass this data to a MQTT broker. (coming soon)