How to Get Started With IoT Using Raspberry Pi and PuTTY – Part 1

By Vaibhav Mathur and Aradhana Kumar

raspberry pi iot


This Raspberry Pi IoT project is the best way to get started on working with the 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. So, we made a step-by-step guide for Pi beginners so they can learn various methods that can be used to make their application of choice.

In short, we use the Raspberry Pi to record temperature on a 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. Here is what you will learn from this project:

  1. Setting up a Raspberry Pi
  2. Using a temperature sensor (DS18B20)
  3. Using a PIR sensor
  4. Using a temperature and PIR sensor together
  5. Setting up and working with a web server to store data and run other applications
  6. Using Python, PHP, and MySQL
  7. Using Google Cloud Messaging (GCM) service to send temperature notification to an Android app when the situation arises and to display stored data

We have divided the whole Raspberry Pi IoT project into two parts. The first part 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.

Required Materials

  1. Raspberry Pi
  2. SD Card (minimum recommended – 4GB, would be a micro SD card)
  3. DS18B20 Temperature sensor (preferred Keyes model)
  4. PIR Motion Sensor
  5. Jumper wires, breadboard
  6. Working Laptop with internet connection
  7. Router and ethernet cables

If you aren’t familiar with Linux and terminal, check out this Basic Linux Commands for Beginners tutorial.

Note: We’ll be remotely accessing the 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 software:

  1. Win32 Disk Imager (Windows users)
  2. PuTTY (Windows users)
  3. Advanced IP Scanner
  4. WAMP/LAMP/MAMP server (This project was done using a Windows laptop on a WAMP server)

How Does it Work?

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:

Raspberry pi
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 iot

Raspberry Pi can only handle digital signals. The 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 the 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



raspberry pi iotThe 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 the sensing distance is 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)


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. Otherwise, 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:
    raspberry pi iot
  • 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 into the respective slot (at the back of your Pi) and we are ready to boot!

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 the terminal is sufficient to get most things done. So we eliminate the need for extra hardware by logging into the 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 it 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:raspberry pi iot

For Windows Users

  • Use PuTTY to access Pi’s Terminal. Run PuTTY and simply enter the IP Address determined in the above step.
    raspberry pi iot

Subsequently, you will receive a login prompt. Use login id as pi and password as raspberrySide Note – For first-time login, you’ll receive a warning for a 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.       

raspberry pi iot

For Linux and Mac Users

  • Open your terminal window and enter the following command: ssh -l pi (Put the IP address of your Pi after ssh). Enter the password as raspberry. You can now access the 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.

Initial Configuration:

  • 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!

Installing a WAMP/LAMP/MAMP Server on Your Laptop

You can install WAMP/LAMP/MAMP Servers following this tutorial or several others you can find on google.

We were working on a Windows Laptop, therefore we have the 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 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:
    • C:\Bitnami\wampstack-5.6.22-0\apache2\conf\httpd.conf
    • C:\Bitnami\wampstack-5.6.22-0\apache2\conf\original\httpd.conf
    • C:\Bitnami\wampstack-5.6.22-0\apache2\conf\bitnami\bitnami.conf
  • Search for the line: ‘Options Indexes FollowSymLinks’ and add ‘ExecCGIraspberry pi iot

Note: Your DocumentRoot directory, and 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.raspberry pi iot
    • 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.

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 –
    • temp-at-interrupt : this will store all the recorded temperatures as well as the date and time of the trigger. ( 3 Fields – Date, Time, Temperature)raspberry pi iot
    • regids:  this will store all the RegIDs of the registered users. (1 Field – RegID)raspberry pi iot

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!

Interfacing a Temperature Sensor for the Raspberry Pi IoT Project

Connect the temperature sensor to your Raspberry Pi as such:

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

For a better understanding of pins and numbering conventions, refer to this image:

Raspberry Pi GPIO pin out

You will have to add one-wire support 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.

Now run the commands as shown to get the current temperature detected by the sensor:raspberry pi iot

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.

Interfacing the PIR Sensor with the Raspberry Pi IoT System

Connect the PIR sensor to your Raspberry Pi as such:

  • Connect the 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

raspberry pi iot

Make a file called by running the command sudo nano on the command line of the 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
    file = open(tempfile, 'r')
    lines = file.readlines()
    return None
  status = lines[0][-4:-1]

  if status == 'YES':
    tempstr = lines[1][-6:-1]
    tempval = float(tempstr)/1000
    return tempval
    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[0][-4:-1]

  if status == 'YES':
    tempstr = lines[1][-6:-1]
    tempval = float(tempstr)/1000
    return tempval
    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"
    w1tempfile = devicename[0] + '/w1_slave'

  print "Process Initialized.\nWaiting for Interrupt..."
    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 = ''+str(temperature)
        result = urllib.urlopen(url)
        print "\nRecorded Temperature is ", (temperature)
        print "\nWriting to Database..."
        print "\nProcess Finished.\n"
        print "\nProcess Initialized Again.\nWaiting for Interrupt..."
  except KeyboardInterrupt:

if __name__=="__main__":

Note: Remember to change the IP Address in the URL to the local IP address of your server.

What This Code Does:

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 the 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 the 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 the server and the temperature value is sent through the GET method. We’ll get to the server side script to handle the request in a minute. The net time sleep is 6 seconds, meaning the if loop takes a total of 6 seconds to execute. This was 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 a 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 and store data in MySQL database.
  • Connect a DS18B20 to the Raspberry Pi and record the current temperature.
  • Connect PIR sensor and DS18B20 to the Raspberry Pi and use them together so only when the former detects something, the temperature is recorded.

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 the 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 ‘’ 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)
  response = urllib.urlopen(url) #sends url request to another file responsible for sending GCM alerts
  data =
  print data
  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))
    print "\nProcess finished"
    print "\nProcess Failed to Complete"


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, 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:
raspberry pi iot

To finish up this 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:

Recommended Posts
Showing 16 comments
  • Dotara

    I don’t want setup web-server in laptop. Can I setup it in Raspberry?

  • Rashid

    i have a question when i am going to connect temperature sensor i am facing this problem
    sudo modprobe w1-gpio
    sudomodeprobe w1-therm
    cd /sys/bus/w1/devices/
    why my sensor does not show here

  • Chuck

    Recently got my RPi-3 and I now know how to spell Raspberry. I’ve learned a lot from your site. Languages and applications I had no knowledge of before. I’m using Apache, Python, PHP, SQL, C Language and Linux.
    Today, I web-enable my Temp/humidity web page.
    Thanks for the inspiration.

  • Ahammad Karim

    Excellent Tutorial.
    I am little bit Confused with this. “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”
    How i will get device name of the temperature sensor?

    Waiting for your response

  • Nele

    Hello, is it possible to do same connection but to a webhosting database. Which ports to use and how to route data?

  • Henrik

    Great introduction to getting started with IoT using the Raspberry Pi.

    Using a Raspberry Pi in IoT projects is not only useful for home automation but can also be applied in your car.


  • Guillermo Morel

    Hey I’m trying to make this project but have a few questions:

    1) Where exactly do I save the “temp-readings.php” file? In my documentRoot directory?

    2) Once everything’s done how do I start the system? Do I run the “” script?

  • Prasanna Arthekar

    Hey hi… This is one of the best tutorial i ever have seen. Actually I am not getting device folder other than w1_bus_master1

    • Gary

      I’m working through this tutorial myself and I had the same problem. I found that my Vcc and Data cables were incorrectly attached. There are two similar sensors but they seem to be connected differently. Try that out and see if it gets you a result.

  • Prashant Dwivedi

    Hey, its a great guide.I completed 80% of it almost successfully but got stuck in the end when i am opening the temp readings page it is showing only the table without data in it? (I am pretty sure I did the hardware connections right!) Plz help me as i am doing this project for last semester majors. Thanks in advance.

  • jeeva

    If you add some videos with this guide it will become more useful

  • vasavi

    hello could you mind helping us in solving problem regarding putty?putty fatal error was occuring every time when we are running can you say what might be the problem?we are struck at that step.we would be thankful if you could help.

  • okta

    Excuse me, can you explain the usefulness of the PIR sensor in this project?

  • Pavan

    Hello, can you please tell me where exactly should I save the ‘temp-readings.php’ file?

  • kratika Gupta

    hey was trying to make this project. can you guys please show the connection of temperature sensor , PIR sensor more clearly. please. I am a beginner and so confused with the wiring.

    • Vaibhav Mathur

      Hey! Sure. I hope you have covered the above mentioned steps till step 4. The raspberry pi board could be quite intimidating at times, I believe you have jumper wires and a bread board.

      Step1:- place your heat sensor on the bread board( in a single column , i.e which has more number of ports and all three pins should be on the same line/column)

      2:- Now, take a 3 male to female jumper wires and corresponding to each pin of the sensor insert the wires( in the next column but in the same row)

      3. Now, with the female end of the jumper wires, connect to the raspberry pi ( read step5 for the details now)

      If you still find problems we will add photos too!

Leave a Comment

69 − 61 =

Start typing and press Enter to search