Wednesday, October 2, 2013

Home Automation with RaspberryPi Python - Django

Home Automation with RaspberryPi Python - Django

So after attending the 2 meet-ups for IOT-Pune. I got a Raspberry Pi and decided to play around with it a little bit. The following post will walk you through the steps needed to turn on/off a light bulb.

Things needed
1. Raspberry Pi
2. Relay switch
3. light bulb with wire to plug into main power supply

Lets us start!!!

  • sudo apt-get install python-dev python-setuptools
  • sudo pip install django
  • startproject mysite
  • cd mysite/
  • startapp ledblink
  • cd mysite
  • open file named and add the ledblink app in the INSTALLED_APPS so that it looks like this
    # Uncomment the next line to enable the admin:
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
  • add this to the templates dir make sure you  add import os at the top of the page
     os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
  • cd ledblink
  • open the file and change it to this
from django.shortcuts import render
from django.http import HttpResponse
import RPi.GPIO as GPIO
import time


def blinker(request):
    if 'on' in request.POST:
    elif 'off' in request.POST:
    return render(request,'control_page.html')
  • Let us add the template control pages so go ahead and create a templates directory in your ledblink app and add the following html code and name it control_page.html
<!DOCTYPE html>


<form action="" method="post">{% csrf_token %}
<input type="submit" name="on" value="on" />
<input type="submit" name="off" value="off" />

  • now let us create a url that links to this view. Goto the file in mysite directory cd ../mysite
from django.conf.urls import patterns, include, url
from ledblink.views import blinker
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(,
  • Goto main folder of the project where the file is located and run the following command
sudo python runserver

open up a web browser and type in the address bar and you should have the following result

now lets get to the part where we connect every thing to the raspberry pi.

  • Sorry I couldn't get a better picture even though Nishant suggested to take the book out. :P But yoy connect the 5V out of the Pi that is pin 2 to the positive on the relay . The ground pin 6 to the regative of the relay and finaly GPIO18 to the corresponding pin on the relay board.

  • Plug the bulb in the main power supply. Go ahead and try turning on the bulb from the web Browser


  1. To install pip
    "sudo apt-get install python-pip"

  2. Heyy,
    You are a good IoT evangelist in Pune, Would you like to try our free IoT cloud storage platform. Do go and try its free

  3. This comment has been removed by a blog administrator.

  4. Hi I am getting an error

    File "", line 8, in
    from import execute_from_command_line
    ImportError: No module named

    how to get rid of this

    1. install "" by using this command
      sudo pip install

  5. That would mean you don't have Django installed. Do a sudo pip install django --upgrade

  6. everythings work fine, but there is no data in request.POST so the relay no react. Any idea why?

  7. hi
    I can not import Rpi.GPIO in the
    where strat to run server error comes :
    ImportError : No module name Rpi.GPIO

  8. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Door sensors Chennai

  9. did you try read data sensor with django?thanks

  10. It is a great website.. The Design looks very good.. Keep working like that!.

  11. How do we run the development server on startup on the pi...I am trying to make it headless..

  12. Apeejay Business Center Provides shared office space in pune and shared office space pune or temporary office space on Rent suitable to your Business in pune at competitive Rates.

  13. Superb Post, Full of Technical Knowledge. Reading this post enhance my knowledge about IOT. You can also checkout article on Introduction to Internet of Things . You can also surf projects on Raspberry Microcontroller Projects

  14. Thanks for sharing this!
    Even turning on a light is difficult with Pi

    Melbourne Web Developer

  15. Nice blog provides many informative and helpful articles. Thanks for sharing the information. Looking for more updates in future.
    Smart WI-FI In-Line Switch


  16. I like your blog, I read this blog please update more content on python, further check it once at python online training

  17. Nice blog provides many informative and helpful articles. Thanks for sharing the information. Looking for more updates in future. Power Cord

  18. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
    Website Development company in Delhi
    Website Designing Company in Delhi

  19. Can I simply say what a relief to uncover somebody who really knows what they are discussing on the internet.
    You actually understand how to bring an issue to light and make it important.
    More people have to read this and understand this side of your story.
    It’s surprising you’re not more popular given that you surely possess the gift. If anyone is looking for weight loss medicines at cheaper price you can immediately call on +1-661-2217635 and get the best prescription by the experts.
    Buy Reductil 15MG
    Buy Meridia 15MG
    Buy adipex 37.5mg
    Buy Ambien 10mg
    Buy Klonopin 1mg
    Buy Valium 5mg
    Pain Relief Medicines Online
    Reduce Fat now with pills
    Erectile dysfunction solution is here
    Best anxiety medicine