IoT Project – Raspberry Pi, Docker, Sensors & Data Analytics

Posted on Posted in IoT, Raspberry Pi
29zfzy6i   Follow the author on Twitter
apple-touch-icon-152x152  GitHub repository for the code used in the project.

 

We live in a world where technology connects people across time zones, borders, continents and enabled seamless transfer of knowledge/information. The next push of technology is now bringing the machines around us into the fold of the connected eco-system.

The Internet of Things is the future of this world wherein almost every appliance, device or should i say anything that exists would have the potential to send and receive data & more often than not also perform some based on that data.

A snapshot of one of the prediction for the connected devices by 2020 is given below.

 

IoT

Keeping this in mind this blog post would describe in detail an end to end implementation of a basic use case for ‘Internet of Things’. I will start with the Hardware setup and also include the software programs & data analytics tools needed to analyse the data.

 

 

Hardware Setup

In order to complete the project I have used the following hardware. All these things can easily be sourced for amazon or other e-commerce website.

  1. Raspberry Pi 3 model B
  2. DS18B20 Temp Sensor (keyes model with 4k7 ohm resistor on board)
  3. Hc-Sr501 PIR Motion Sensor
  4. JumperWires
  5. BreadBoard

Software Setup

I will be using the below softwares for the data analytics part of this project and all softwares have open source version.

  1. Docker
  2. Grafana
  3. Influxdb
  4. Python

 

Raspberry Pi GPIO Pins

Find below the snapshot of the Raspberry Pi GPIO Pins. The GPIO pins enable the sensors to send data to the Pi & this data is then further moved to a database for us to work on that data.

screen-shot-2016-11-27-at-11-22-18-am

The pins marked in red colour are the power pins which are used for providing the power for running the external sensors, kindly note that there are two output voltages that can be delivered by the Pi  ( 5V & 3.3 V )

 

The Black pins are ground pins.

& the yellow pins are Data IO pins. GPIO outputs are easy; they are on or off, HIGH or LOW, 3v3 or 0v. The Inputs are a bit complex and out of the scope of this article.

For advanced users this diagram from pinout.xyz website gives a picture of the advanced capabilities of the GPIO pins.

raspberry-pi-pinout

 

Step 1 :- Setup the Raspberry Pi

 

I am assuming that you should be able to do this easily, it is easy & there are many tutorials on the web that describe how to get this done. Raspberry Pi is just like any other computer so you install a operating system and then connect it to the internet using WiFi. Once the device has been setup after that all other commands can be given by remote SSH connection.

This official link from Raspberry Pi can also be used for figuring out how to set a raspberry pi.

Setup a Raspberry Pi. 

 

Step 2 :- Connect the sensors to the Raspberry Pi

As mentioned earlier we would be using two sensor for this project. Find below a brief description of the sensors.

  • DS18B20 Temp Sensor
    • This is a temperature sensor with a built in 4k7 Ohm resistor, this sensor gives the current ambient temperature.
    • This sensor has three pins.  — GND, VCC (+5V Power) & Data. The VCC pin is for giving power to the sensor & the Data pin is for moving sensor data to the computer (raspberry pi).  screen-shot-2016-12-08-at-1-29-41-pm 
  • HC-SR501 PIR Motion Sensor
    • This is a motion temperature sensor detects if there is any motion in its range & send a signal.pir-sensor-0002

 

We will now connect the sensors to the Raspberry Pi using a Breadboard and Jumper cables in the manner mentioned below.

  • DS18B20 Temp Sensor
    • Connect Ground Pin of the sensor to physical Pin 6 of the GPIO board.
    • Connect Data Pin to physical Pin 7 of the GPIO board.
    • Connect Vcc to physical Pin 1 (3.3V) of the GPIO board.

 

  • HC-SR501 PIR Motion Sensor
    • Connect Ground pin of the PIR sensor to the Ground Pin of the temperature sensor
    • Connect Vcc pin to physical Pin 2 (5V) of the GPIO board
    • Connect Output pin to physical Pin 11 of the GPIO board

 

The final assembly would look like this.

img_6958

 

 

 

Step 3 :- Setup the softwares for storing and visualising the data

We would be using docker on our host machine to run the softwares for completing this project. Docker is a software containerization platform that enables us to package a software with all its dependencies. This container can then be shipped using their online hosted registry and would run on any machine that has the docker engine installed on it.

 

  • Installing Docker Engine 
    • First step would be to install docker on the host machine.
    • The installation process depends on the Operating System that we are using.
    • This process is explained in detail for all the OS’s on docker’s official website, please find the link below
    • Docker is an amazing tool but any further discussion on this topic would be beyond the scope of this article.

 

  • Run the Influx DB & Grafana containers using the docker engine. 
    • We can use run the InfluxDB and Grafana containers by using the commands mentioned below.
    • You can find these commands on my GitHub page.
      • Docker run –name influxdb -p 8083:8083 -p 8086:8086 -e INFLUXDB_USER=testsensor -e INFLUXDB_PASS=testsensor -e INFLUXDB_NAME=IoTDashboard -d jaywanii/influxdb:latest

      • Docker run –name grafana -p 3000:3000 –link influxdb:influxdb -e INFLUXDB_USER=testsensor -e INFLUXDB_PASS=testsensor -e INFLUXDB_NAME=IoTDashboard -d jaywanii/grafana:latest

  • Use Docker-Compose 
    • Docker Compose allows us to define your multi-container application with all of its dependencies in a single file, then spin our application up in a single command.
    • Docker-Compose can be installed in different ways as explained on the docker-compose official page below
    • Download the docker-compose.yml file from my GitHub page and create a new folder to put this file in.
    • Go to the newly created folder on the terminal and run this command
      • docker-compose up -d

Once these commands are run we will have two docker containers running. One for Grafana and the other on for InfluxDB.

screen-shot-2016-12-08-at-5-02-25-pm

 

Now we can check if the two softwares are working correctly.

  • Go to a browser and open localhost:3000 , the default Grafana interface should open. Please note that the default User is “admin” & the password is “admin”

screen-shot-2016-12-08-at-5-04-32-pm

 

 

 

Step 4 :- Run python code on Pi to display temp on the Stdout. 

Now we need to login to the Raspberry Pi using SSH and run the following steps.

 

  • Add one-wire support to be able to work with the sensor.
    • sudo nano /boot/config.txt

    • add dtoverlay=w1-gpio to the end of the file.

 

  • Install Python Client for InfluxDB & run the modprobe commands to enable the sensor to read the temp.
    • pip install influxdb

      sudo modprobe w1-gpio

      sudo modprobe w1-therm

       

  • Clone the IoT-RespberryPi-TempSensor on your Raspberry Pi & go to that folder.
    • git clone https://github.com/Jay-Wani/IoT-RespberryPi-TempSensor.git

  • Run the python program temperature.py and you should see the temperature from the sensor being displayed.
    • python temperature.py

       

The temperature should start displaying on the Standard Output (i.e. the terminal)

screen-shot-2016-12-08-at-10-55-47-pm

 

Step 5 :- Ship the data to influxdb using the python client.

  • Check whether InfluxDB is working
    • go to the browser and type “localhost:8083”   if the page opens, then its working fine.

 

  • Run the python program temperature-influx.py and you should see the temperature from the sensor being displayed on the terminal and at the same time the data is also being shipped to influxdb in the right format. This python file is also available in my GitHub repository.
    • python temperature-influx.py

screen-shot-2016-12-08-at-11-54-08-pm

 

Step 6 :- Analyse / Visualise the data in Grafana

  • Go to the Grafana Dashboard  — “localhost:3000”
  • Add a Datasource from InfluxDB & test the data source. You should see a “test successful” message .

screen-shot-2016-12-09-at-12-13-04-am

 

  • Now we can start adding rows to the Dashboard and visualise the data based on your requirements.
  • The Data is real time and the charts can be set to refresh on any given interval based on the business requirements.
  • I have added different charts for visualising the data, see a snapshot of the same below.
  • You can use my dashboard by importing the Temp_DB.json file, also available in my GitHub repo.

screen-shot-2016-12-09-at-12-07-46-am

This blogpost describer the use case for IoT in the real world, using economical and easily available hardware & softwares. Feel free to reach out to me in case of any queries.

A big shout out to Alex Ellis, who provided guidance for getting this completed.

 

Leave a Reply

Your email address will not be published. Required fields are marked *