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
TEMPLATE_DIRS = (
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 views.py file and change it to this
from django.shortcuts import render
from django.http import HttpResponse
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(18,GPIO.OUT)
def blinker(request):
if 'on' in request.POST:
GPIO.output(18,1)
elif 'off' in request.POST:
GPIO.output(18,0)
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>
<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
Nice!!
ReplyDeletePerfectly elaborated.
To install pip
ReplyDelete"sudo apt-get install python-pip"
Heyy,
ReplyDeleteYou are a good IoT evangelist in Pune, Would you like to try our free IoT cloud storage platform. Do go and try http://www.sense-iot.com its free
This comment has been removed by a blog administrator.
ReplyDeleteHi I am getting an error
ReplyDeleteFile "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
how to get rid of this
install "django.core.management" by using this command
Deletesudo pip install django.core.management
That would mean you don't have Django installed. Do a sudo pip install django --upgrade
ReplyDeleteeverythings work fine, but there is no data in request.POST so the relay no react. Any idea why?
ReplyDeletehi
ReplyDeleteI can not import Rpi.GPIO in the views.py?
where strat to run server error comes :
ImportError : No module name Rpi.GPIO
by the way im using Django 1.10
Deletedid you try read data sensor with django?thanks
ReplyDeleteIt is a great website.. The Design looks very good.. Keep working like that!.
ReplyDeletejio.local.html
How do we run the development server on startup on the pi...I am trying to make it headless..
ReplyDeleteSuperb 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
ReplyDeleteThanks for sharing this!
ReplyDeleteEven turning on a light is difficult with Pi
Melbourne Web Developer
Nice blog provides many informative and helpful articles. Thanks for sharing the information. Looking for more updates in future. Power Cord
ReplyDelete
ReplyDeletepouch manufacturers
wall putty bag manufacturers
Thanks for sharing home automation.Thanks for sharing useful information about the home automation
ReplyDeletesolution.
Home Automation company in Delhi | Home Automation solution company in
delhi | Wireless home automation company in
delhi | Home automation
Exclusive info about smart home automation. Thanks for sharing with s.
ReplyDeleteIP Camera
Great Post!! Thanks to shared your information,Its very useful for me keep updating. Web Designing Companies in Bangalore | Website Designing Company in Bangalore | Web Design Companies in Bangalore | Web Designers in Bangalore
ReplyDeleteWell described post. Thanks for sharing this useful information.
ReplyDeletetrumpf eco nozzle
Great post. Very useful and informative post You have shared. Thank you for sharing. We are offering tablet home automation for tech enabled homes.
ReplyDeleteI wanted to thank you for this excellent read!JioFiLocalHtml
ReplyDeleteWhole house automation systems run $500 to $1,600, with components (switches, cameras, etc.) between $80 to as high as $200. Pricing depends on a variety of factors, most notably the type of system
ReplyDeletesmart home devices
The 100 year old houses in my town still have original electrical switches that work. It would be absurd if I need to change my light switches every 2,5,10 years because they are smart home devices.
ReplyDeleteHey, the content is worth to read and blog is good to follow. keep up sharing useful insights with us putlocker
ReplyDeleteVery nice post with excellent writing skills thanks for sharing.
ReplyDeleteData Analytics Course Online 360DigiTMG
Nice post. Thank you for sharing this valuable content.
ReplyDeleteunindent does not match any outer indentation level
web development company
ReplyDeleteIndian Train Simulator Mod
.Nice Information, your blog is sharing unique information....
ReplyDeleteThanks for sharing!!!
patio doors and windows services
security patio doors French in hyd
security for sliding patio doors in hyderabad
security for sliding patio doors in mumbai
home security for patio doors in hyderabad
home security for patio doors in mumbai
I gained many unknown information.
ReplyDeletedigitechtricks
Howdy! I simply would like to give a huge thumbs up for the good data you have here on this post. I will probably be coming again to your weblog for more soon. https://python.engineering/python-opencv-cv2-puttext-method/
ReplyDeleteperde modelleri
ReplyDeletesms onay
Turkcell mobil ödeme bozdurma
Nft Nasıl Alinir
Ankara evden eve nakliyat
trafik sigortası
DEDEKTOR
web sitesi kurma
aşk kitapları