Wednesday, October 2, 2013

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

django-admin.py startproject mysite

cd mysite/

django-admin.py startapp ledblink

cd mysite

open file named setting.py and add the ledblink app in the INSTALLED_APPS so that it looks like this

INSTALLED_APPS = (

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.sites',

'django.contrib.messages',

'django.contrib.staticfiles',

'ledblink',

# Uncomment the next line to enable the admin:

'django.contrib.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

Let us add the template control pages so go ahead and create a templates directory in your ledblink app and add the following html codeand name it control_page.html

<!DOCTYPE html>

<html>

<head>

</head>

<body>

<form action="" method="post">{% csrf_token %}

<input type="submit" name="on" value="on" />

<input type="submit" name="off" value="off" />

</form>

</body>

</html>

now let us create a url that links to this view. Goto the urls.py 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('',

url(r'^ledblink/$',blinker),

# Examples:

# url(r'^$', 'mysite.views.home', name='home'),

# url(r'^mysite/', include('mysite.foo.urls')),

# 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(admin.site.urls)),

)

Goto main folder of the project where the manage.py file is located and run the following command

sudo python manage.py runserver 192.168.0.106:8000

open up a web browser and type http://192.168.0.106:8000/ledblink/ 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