Rajesh K Jeyapaul, IBM Cloud Solution Architect
Krishna Kumar SLV , IBM Intern @IBM Innovation labs, Bangalore
In this Blog we would like to bring out our experience in working with Raspberry Pi device and motion sensor. How in simple 2 steps someone can connect the device and start visualizing the data from device.
We would like show the following things in this blog:
-> Connect the device to IoTF (Blumix IOT service )
-> Collect and Analyze the data using Bluemix
-> Also, mention about the protocols and step wise data flow.
Device Used : RaspberryPi B+ for demonstration purposes
To understand how to connect the device and how the IoTF works, it is necessary to know about an Organization, End-Application and Devices as these are the major components in the IoTF. I urge you to read about these at,
Step 1: Create IOT service in Bluemix and Register the Device
->Create a IOT service at IBM Bluemix
->The IOT service will redirect you to IoTF, Register the device in your organization
->Choose your recepie and follow the instructions to download the required packages.
->Configure the device after registration with your unique credentials(This ensures security of your device)
->You have an option of adding users to your organization
->Generate API keys which helps the Applications to communicate with the IoTF to extract the data of a particular device in the authorised organization.
Step 2: Install the Receipe (Agent to communicate between Device and Bluemix IOTF)
For RaspberryPi, after installing the package,
you can start the service using,
sudo service iot start
Congratulations !! once the iot service starts running in the device, it starts publishing the data. This can be verified through Bluemix IOTF console.
( IoTF organisation dashboard under your device )
COLLECT / ANALYZE DATA USING BLUEMIX:
To create a application side to this,
choose IoT boilerplate under Bluemix Boilerplates and start the Application,
Go to the Application link,
Then, In your Node Red run time, configure IoT device to the device mac address and provide the authorisation details(API key and API token)
You can see the real time data coming in the console.
CHANGES REQUIRED IN DEVICE TO ADD ADDITIONAL SENSOR DATA
To make changes to the data being sent, you need to modify the Device side package installed on Pi.
1) git clone https://github.com/ibm-messaging/iot-raspberrypi.git
2) cd iot-raspberrypi/samples/c/
Understand the code and make changes accordingly
3) Rebuild it,
If 'debuild' and ssl lib are not found on your Raspberry Pi, install them and re-build the .deb file,
sudo apt-get install dpkg-dev build-essential devscripts
sudo apt-get install libssl-dev
4) install the latest .deb,
sudo dpkg -i iot_1.0-1_armhf.deb
restart the service using,
5) sudo service iot restart
Note: To understand the code, its advisable to read throught the MQTT protocol and its functions at,
We can also send and recieve data to and from cloud using Python,
It is mandatory for you to go through the ibmiotf(Python Library used for communication with IoTF) documentaion,
EXPERIMENT WITH ACCELEROMETER
Now, Lets do a small task.
Lets connect a accelerometer to the Pi and send the data to IoTF using Python and recieve data in the same Application.
Raspberry Pi - ADXL345 Accelerometer & Python!!
Connecting it up:
Raspberry Pi -> ADXL345:
GND -> GND
3V -> VCC
SDA -> SDA
SCL -> SCL
Here, SDA is the line thorugh which data is transmitted and this transmission is synchronized with the clock at SCL.
Configure your Pi:
The ADXL345 supports both I2C and SPI connections, I used I2C, which requires some configuration on the Pi:
Basically, I2C and SPI are light weight protocols through which the Master device like Pi communicates with the Slaves like the sesor(acclerometer)
It is advisable to know the working of these 2 protocols at,
Add the I2C modules to the Pi's configuration:
sudo vim /etc/modules
add the following lines:
Remove I2C from the blacklist:
sudo vim /etc/modprobe.d/raspi-blacklist.conf
comment out using '#':
Reboot to make the changes:
sudo shutdown -r now
You will need to install some software:
sudo apt-get install python-smbus i2c-tools git-core
You can check that your ADXL345 is found on the I2C bus, by running:
sudo i2cdetect -y 1
You should see a device at address 53
Download the ADXL345 pimoroni python library from github:
git clone https://github.com/pimoroni/adxl345-python
Now you can write your code to read the accelerometer data and publish it into cloud.
If you had already gone through the IoTF documentation,
You just need to connect to your organisation, take the data and publish it into cloud!!
Find the code for doing this at,
In this manner, you can send any data to cloud, analyse the data using numerous number of services provided by IBM bluemix like Watson services.
And then this might lead to a special and amazing application!!