Raspberry Pi IoT: The Best Project to get started with IoT ! Pt 1
From our STAR Makers: Vaibhav Mathur and Aradhana Kumar
This Raspberry pi IoT project is the best way to get started on working with IoT (Internet of Things) and to build your knowledge to expand it to other applications.The first problem was figuring out a simple enough sample project to learn how to make sensors communicate with each other and work together. So, we made a step-by-step guide for pi beginners so that they can learn various methods that can be used for making their application of choice.
In short, here we are using the Raspberry Pi to record temperature on trigger and then push notifications from the server to your android device. You can also find all the project files for the android app below this tutorial. To list out, these are what you will learn from this raspberry pi iot project:
- Setting up raspberry pi
- Using temperature sensor(DS18B20)
- Using PIR sensor
- Using temperature and PIR sensor together
- Setting up and working with a web server to store data, and running other applications
- Uses of Python, PHP and MySQL
- Using Google Cloud Messaging (GCM) service to send temperature notification to an android app when situation arises and to display stored data
Moreover, we have divided the whole Raspberry Pi IoT project into two parts. The first part here consists of setting up the Raspberry Pi and interfacing it with the sensors. The second part covers building the android application and connecting it to the server. It can be viewed here.
What are the stuff required to build this project?
- Raspberry Pi
- SD Card (minimum recommended – 4GB, would be a micro SD card)
- DS18B20 Temperature sensor(preferred Keyes model)
- PIR Motion Sensor
- Jumper wires, bread board
- Working Laptop with internet connection
- Router and ethernet cables
Note: We’ll be remotely accessing Raspberry Pi’s terminal through SSH, so you won’t need a dedicated monitor, mouse or keyboard in any part of this tutorial.
You would also want to download the following softwares:
- Win32 Disk Imager (Windows users)
- PuTTY (Windows users)
- Advanced IP Scanner
- WAMP/LAMP/MAMP server (This project was done using a Windows laptop on a WAMP server)
How does it work?
In this Raspberry Pi IoT project, the raspberry pi is interfaced with a temperature sensor and PIR sensor, and reads data from it through its GPIO pins. After this, the data is relayed to a web server and subsequently pushed to your android device through the Google Cloud Messaging (GSM) service. A brief outline of the process:
Interface temperature sensor (DS18B20) and PIR sensor
Store data from sensors in a web server
Google Cloud Messaging (GCM) service to send temperature notification to an android app when situation arises and to display stored data
You are working with: DS18B20
Raspberry Pi can only handle digital signals. DS18B20 is a digital temperature sensor. In the above Keyes model of the sensor that we used, the left-most pin is Ground, the center pin is Vcc and the right-most pin is Output/Data pin. The sensor that we used came with a 4.7K ohm resistor connected between Vcc and data pin. You might have to connect an external one in case yours does not have it. It requires an input voltage of 3.3V
PIR sensor detects motion using Infra-red rays. It is important to know that this motion sensor has a detection delay of about 3 seconds (adjustable) during which the pin state stays high and sensing distance adjustable from 3-7 meters. It also comes with a lock time (in our case 3 seconds) which is the minimum time interval between two accepted signals. It requires an input voltage of 5V. Pin specifications are given on the sensor itself (picture on the right)
STEP 1: Setting up the Pi for the Raspberry Pi IoT project
If you already have a Raspberry Pi setup, move over to the next step to start with your raspberry pi IoT project. Else, head over to https://www.raspberrypi.org/downloads/raspbian/ to download the Raspbian OS for your Pi. There are many other distributions you can use, but Raspbian remains the most common and convenient for beginners. Extract the .img file from the downloaded zip folder and write it to your SD card.
For Windows users :
- Insert the SD card reader with the SD card in it. Figure out the Drive assigned to it.
- Run Win32 Disk Imager (you may have to run this as administrator), select the extracted image file and the drive letter. Be very careful to select the correct drive, you do not want to unintentionally destroy other data. It should look something like this:
- Click ‘Write’. Wait for the process to complete and eject the SD card.
Side note – While on this step, we got stuck because our SD card became read-only due to a tiny lock on the SD card reader that we had ignored. Watch out!
For Linux users:
- Run df -h on the command line. This will list out the devices currently mounted on your device.
- Insert the SD Card reader with the SD Card in it and run df -h again to figure out the device name. It’ll be something like /dev/mmcblk0.
- Unmount the SD Card with umount /dev/mmcblk0 replacing /dev/mmcblk0 with your own device name. You might want to format the SD Card with FAT32 (use sudo mkdosfs -F 32 -v /dev/mmcblk0) just to be sure.
- Now, write the .img file to your SD Card with sudo dd bs=1M if=”/home/linux/2016-05-27-raspbian-jessie-lite.img” of=/dev/mmcblk0. Edit to add the path to your extracted file (after if=) and your device name (after of=). Wait for the process to complete and eject the SD card.
For Mac users:
- Connect the SD card reader with the SD card inside.
- From the Apple menu, choose “About This Mac”, then click on “More info…”; if you are using Mac OS X 10.8.x Mountain Lion or newer, then click on “System Report”.
- Click on “USB” (or “Card Reader” if using a built-in SD card reader) then search for your SD card in the upper-right section of the window. Click on it, then search for the BSD name in the lower-right section; it will look something like diskn where n is a number (for example, disk4). Make sure you take a note of this number.
- Unmount the partition so that you will be allowed to overwrite the disk. To do this, open Disk Utility and unmount it; do not eject it, or you will have to reconnect it. Note that on Mac OS X 10.8.x Mountain Lion, “Verify Disk” (before unmounting) will display the BSD name as /dev/disk1s1 or similar, allowing you to skip the previous two steps.
- From the terminal, run the following command:
sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn
Remember to replace n with the number that you noted before!
- If this command fails, try using disk instead of rdisk:
sudo dd bs=1m if=path_of_your_image.img of=/dev/diskn
After writing the image file to your SD Card, insert it in the respective slot (at the back of your pi) and we are ready to boot!
STEP 2: Headlessly Booting your Raspberry Pi IoT system
Getting a dedicated monitor, mouse and keyboard to use your Raspberry Pi might become an unnecessary hassle. Access to just the terminal is sufficient to get most things done. So we eliminate the need for extra hardware by logging into Pi using your personal laptop through SSH. Latest versions of Raspbian come with SSH enabled by default therefore you can run Pi remotely even while setting up for the first time.
- Get a router with DHCP enabled. This is necessary because we want our Pi to have a unique IP address to be able to connect to it. Connect both your Laptop and your Pi to the router via ethernet cables. Your Laptop and Pi now share a Local Area Network and can identify each other using their unique IP address.
- To use SSH, we need the IP address of your Pi. Run the Advanced IP Scanner. This will list out the IP addresses of all devices on your network and their manufacturer. You’ll see something like this:
For Windows users
- Use PuTTY to access Pi’s Terminal. Run PuTTY and simply enter the IP Address determined in the above step.
Subsequently, you will receive a login prompt. Use login id as pi and password as raspberry. Side Note – For first time login, you’ll receive a warning for security alert. Click on ‘Yes’ and proceed
You have now opened a terminal session on your Raspberry Pi which can be accessed through your Windows Laptop.
For Linux and Mac users
- Open your terminal window and enter the following command: ssh 192.168.0.101 -l pi (Put the IP address of your Pi after ssh). Enter the password as raspberry. You can now access Pi’s terminal on the terminal window of your host computer.
Please note that the cursor won’t move forward while entering the password due to default settings.
- Enter the command sudo raspi-config in the terminal window and get started with the initial configuration
- Go to >Raspi-Config>expand_rootfs>OK to configure the Pi to use the entire space on the SD card (otherwise only as much space will be used up as the OS requires)
- Apart from the above, you can go through the other available options and configure according to your needs.
Woohoo! Your Raspberry Pi is now ready for you to go crazy!
STEP 3: Install a WAMP/LAMP/MAMP server on your laptop
You can install them by following this tutorial or several others you can find on google.
We were working on a Windows Laptop, therefore we have Bitnami WAMP Stack loaded. There are other development environments available too. Some of the further steps are specific to Bitnami WAMP Stack, you can customize them according to your servers.
While installing Bitnami WAMP Stack, you will be prompted to create a MySQL ‘root’ account by entering a password. Make one and remember it.
Note: Since this project is purely done for learning purposes, our server-side codes are both in Python and PHP. You will have to configure your Apache server to run .py files, though. You can do this by editing the httpd.conf file in the apache install directory.
- In Bitnami Wampstack, you’ll have to make the same edit in 3 files in the following locations:
Note: your DocumentRoot directory, all your CGI scripts should be saved in this directory.
- Search for the line: ‘#Add Handler cgi-script .cgi’. Uncomment it and add a ‘.py’ in the end.
- Restart your Apache Server. to apply the configuration changes.
- Now, save a test python file in your DocumentRoot Directory and run it on your browser to check if things are working correctly. You can check the error log (C:\Bitnami\wampstack-5.6.22-0\apache2\logs\error.log) for hindrances and correct them as required.
STEP 4: Creating a database and table to store temperature readings and GCM RegIDs
- Login to PHPMyAdmin (enter: localhost/phpmyadmin/ in the address bar of your browser) using the password you entered during installation of Bitnami WAMP Stack with username root and create a new database called ‘templog’.
- Now, create two tables in that database –
You now have all the essentials in place. Let’s get started with the device and sensor set-up and get down to the codes!
STEP 5: Interfacing a temperature sensor for the Raspberry Pi IoT project
Connect the temperature sensor with raspberry pi as such:
- 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.
For better understanding of pins and numbering conventions, refer to this image:
You will have to add one-wire support to be able to work with the sensor. Edit the /boot/config file by running the command sudo nano /boot/config.txt and add dtoverlay=w1-gpio to the end of the file.
28-021502c3a0ff is the device name assigned to the temperature sensor in our case, edit the command cd 28-021502c3a0ff according to your device name.
t=27062 means that the current temperature is 27.062 degree Celsius. ‘YES’ in line 1 signifies that a valid temperature has been recorded in the file
STEP 6: Interfacing PIR sensor with Raspberry Pi IoT system
Connect the PIR sensor to Raspberry Pi as such:
- 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
Make a file called trigger.py by running the command sudo nano trigger.py on the command line of Pi’s terminal and save the following code:
#!/usr/bin/env python import os import RPi.GPIO as gpio import time import datetime import glob import MySQLdb from time import strftime import urllib def probe_temp(tempfile): #Returns tamperature value at the time of interrupt try: file = open(tempfile, 'r') lines = file.readlines() file.close() except: return None status = lines[-4:-1] if status == 'YES': tempstr = lines[-6:-1] tempval = float(tempstr)/1000 return tempval else: print "Error in reading file, please try again" def main(): PIR_Input = 11 #PIR sensor connected to pin 11 gpio.setmode(gpio.BOARD) #differential numbering of board pins, need to specify which convention being used gpio.setup(PIR_Input, gpio.IN)#specifying physical pin 11 as input os.system('sudo modprobe w1-gpio') #loads the kernel os.system('sudo modprobe w1-therm') status = lines[-4:-1] if status == 'YES': tempstr = lines[-6:-1] tempval = float(tempstr)/1000 return tempval else: print "Error in reading file, please try again" def main(): PIR_Input = 11 #PIR sensor connected to pin 11 gpio.setmode(gpio.BOARD) #differential numbering of board pins, need to specify which convention being used gpio.setup(PIR_Input, gpio.IN)#specifying physical pin 11 as input os.system('sudo modprobe w1-gpio') #loads the kernel os.system('sudo modprobe w1-therm') devicename = glob.glob ('/sys/bus/w1/devices/28*') # useful only if only 1 temperature sensor is connected if devicename=='': print "Check DS18B20 connection, no device recognized" else: w1tempfile = devicename + '/w1_slave' print "Process Initialized.\nWaiting for Interrupt..." try: while True: current_state = gpio.input(PIR_Input) #Checks for input if current_state: print "Current state of input at pin 11 is", current_state temperature = probe_temp(w1tempfile) url = 'http://192.168.0.100/write-to-database.py?temp='+str(temperature) result = urllib.urlopen(url) time.sleep(2) print "\nRecorded Temperature is ", (temperature) time.sleep(2) print "\nWriting to Database..." time.sleep(2) print "\nProcess Finished.\n" print "\nProcess Initialized Again.\nWaiting for Interrupt..." except KeyboardInterrupt: pass if __name__=="__main__": main()
Note: Remember to change the IP Address in url to the local IP address of your server.
What this code does is:
The code checks the current state of the input pin in a while loop. When the PIR sensor detects motion, the current state becomes 1 and the if condition becomes true.’w1tempfile’ is the file name which stores the temperature in the last characters of its second line. Note that the device name for a DS18B20 will always be of the type 28-xxxx, in case of multiple temperature sensors, each will have its own unique name. The function probe_temp returns the value of recorded temperature in degrees Celsius. Next, a request is made to server and the temperature value is sent through GET method. We’ll get to the server side script to handle the request in a minute. The net time sleep is of 6 seconds, meaning the if loop takes a total of 6 seconds to execute. This has been done because with the detection delay and lock time, the minimum wait between registering two different signals by the PIR sensor came to 6s in our case.
Let’s sum up all that we’ve done till now –
- Set up raspberry pi for the first time
- Use SSH to remotely access the terminal window on another device
- Set up a webserver on an external computer to handle requests, store data in MySQL database
- Connect DS18B20 to the raspberry pi and record the current temperature
- Connect PIR sensor and DS18B20 to the raspberry pi and use them together such that only when the former detects something, the temperature is recorded.
STEP 7: Server side scripting for the Raspberry Pi IoT system
We now handle the server side scripts to do the following:
- Get the temperature values sent by raspberry pi
- Record the data in a database along with the date and time relevant to the data
- Display the data in a tabular form
- Send push notifications to registered users when the temperature exceeds a certain threshold.
Download MySQLdb module for python and make sure it is of the same bitness as your python.
In your DocumentRoot directory noted before, create a new file called ‘write-to-database.py’ and save the following code:
Note the shebang used in the first line of the code. Edit it as-
#!<Path to the python environment in your system>
#!C:\Users\Aradhana Kumar\AppData\Local\Enthought\Canopy\User\Scripts\python.exe # Server side code to read data sent by pi through GET method. import cgi, cgitb import os import time import datetime import glob import MySQLdb from time import strftime import urllib # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from client temp = form.getvalue('temp') print "Content-type:text/html\r\n\r\n" print "" print "" print "Data Storage" print "" print "" temperature = float(temp) # Alert user if recorded temperature is above a threshold (30 degrees in this case) if temperature >= 30: push = '1' message = 'Alert! Current temperature recorded is ' + str(temperature) url="http://localhost/sending-push-notifications.php?push="+push+"&message="+message response = urllib.urlopen(url) #sends url request to another file responsible for sending GCM alerts data = response.read() print data else: print temp #Code to write the recorded temperature in the MYSQL database 'templog' and table 'temp-at-interrupt' db = MySQLdb.connect(host="localhost", user="root", passwd="raspberry", db="templog") cur = db.cursor() while True: dateWrite = time.strftime("%Y-%m-%d") timeWrite = time.strftime("%H:%M:%S") sql = ("""INSERT INTO `temp-at-interrupt` (`Date`, `Time`, `Temperature`) VALUES (%s,%s,%s);""", (dateWrite,timeWrite,temperature)) try: cur.execute(*sql) db.commit() print "\nProcess finished" except: db.rollback() print "\nProcess Failed to Complete" cur.close() db.close() break
What this code does:
When the raspberry pi sends a request to the server, this file identifies and stores the sent temperature value in a variable. In case the value is above our set threshold, it sends a url request to another file on the same server, which sends a GCM push notification to all registered users. The code is responsible for entering the temperature value as well as the date and time of the moment it was registered, into the table in our database.
Now, to display the table in a tabular form, now download this file: ‘temp-readings.php’ from here and save it.
On entering ‘http://localhost/temp-readings.php’ in the address bar of your browser, you should see a table like this for your raspberry pi IoT project:
For finishing up the raspberry pi IoT project, the next steps to build the android application (or download the project files for it) and to connect it to the server, refer to Part 2 of this tutorial: https://diyhacking.com/best-raspberry-pi-iot-android/
Submitted by Vaibhav Mathur and Aradhana Kumar
Aradhana Kumar: currently studying in IIT, Kharagpur. She has completed her second year in chemical engineering and has hands on experience with the raspberry pi along with other sensors, she has coded in python, php,MySQL, and their likes.
Vaibhav Mathur: currently studying under the University of Delhi, pursuing bachelor in technology in computer science in his fourth year. Has working knowledge with languages like C,C++ as well as Java. He has hands on experience with raspberry pi and other sensors, he also has developed many Android applications with different multi nationals as well as under a professor.