Thursday, July 10, 2014

Raspberry Pi Flask python web page to control the LED

I was searching for how to control the LED from python web page program. I reached in Flask microframework. http://flask.pocoo.org/


Flask is lightweight web application framework written in python and its very much suited for Raspberry Pi board.  Controlling the LED connectioned to GPIO pin 7 is easy ,through the python program written importing Flask.


Steps to control LED connected to Raspberry Pi through WebPage

Step1: install Flask in Raspberry Pi.Follow below steps

apt -get install python-pip
pip install Flask



Step2. Connect the LED to the GPIO pin 7 through 470ohm resister

Step3: Writing Programs in Python Flask

First you need to create a python program which actually rendor the html templates stored in the 'templates' directory.

1.Create a folder called 'pythonsites' in your home directory  and you can write gpio.py in that folder as below. Refer the Flask site for example programs 


route() decorator is used to bind a function to a URL. refer http://flask.pocoo.org/docs/quickstart/#routing
3 functions we have written which are  light_on, light_off  & light_blink

We are going to keep 3 links in the html template ,those are Turn On, Turn Off, LED blink

2.Once you have written this program ,should create a folder called 'templates' and keep our html template.

folder path -
pi@raspberrypi ~/naveen/pythonsites/templates $

html page shows in the browser create as light.html



Web page using Flask:

Run gio.py program using python

sudo python gio.py

connect with port to raspberry as follows ,depends on your raspberrypi IP address.
 http://192.168.1.10:5000/











Monday, June 30, 2014

Raspberry Pi LED blink program with Python

Today the experiment with Raspberry Pi was to explore the GPIO. But actually I didn't have anything(even connecting wires) , because its long time since I do something with breadboard. Even I was not sure how much I remember on breadboard schema.

Raspberry Pi GPIO




But within 3 hours of effort ( buying all these connecting wires & resisters & jumpers ), happily now the LED is blinking with Raspberry Pi and Python.

 How to  Blink LED with Raspberry Pi 7th GPIN using Python Program

1. login to Raspberry ssh  ssh pi@192.168.1.10  ( hope you have set a static ip address ) other wise you need to use HDMI cable connect to monitor

2.Write Python program to control GPIN 7

Program to On the 7th PIN :

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.output(7,True)


Program to Blink  LED connected to the 7th Pin

if you don't have Rpi GPIO module , it is required to get and install using apt-get install command. Those libraries should be imported before we use specific opcodes. As you indentation is important in python especially for loop logic.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
while(1) :
        GPIO.output(7,True)
        time.sleep(1)
        GPIO.output(7,False)
        time.sleep(1)


Setting Up Raspberry Pi board and Circuit to LED.
  
before starting it recommended to test the LED with connecting the 3.3 V GPIO pin(1) and  GPIO Ground pin(6) through 270ohm to see its up or not . Once its tested , you can move the 3.3V connecting jumper wire to Pin7 in Raspberry so that the python program can control the Pin 7 and make the value 0 and 1 to blink the LED.