• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoHow to Read/Control GPIO (turn on/off LED) on web ...
Previous Next

How to Read/Control GPIO (turn on/off LED) on web page using Python

Posted by: admin , January 13, 2015

In this tutorial, we will create a web server on pcDuino using Python. We can then control/read GPIOs on pcDuino through the webpage. This is a prefect implementation of web lamp or any web powered devices.

1. Install Requests (Requests is a HTTP client library of Python):

  1. $ sudo apt–get install python–requests

2. Install python-pip (pip is the tool used to install and manage python software packages):

  1. $sudo apt–get install python–imaging python–imaging–tk python–pip python–dev git

Note: Some time the above commands will fail, we need:

  1. $sudo apt–get update

3. Install Flask (Flask is a light weight web application framework written by Python)

  1. $sudo pip install flask

4. Download python-pcduino from github, and put it under /home/ubuntu. Enter into directory Sample, and copy directory blink_led to a directory named hellp-gpio, and rename the file blink_led.py there to hello-gpio.py.

The code of hello-gpio.py is as following:

  1. from flask import Flask, render_template
  2. import datetime
  3. import gpio
  4.  
  5. app = Flask(__name__)
  6.  
  7. channel = { 0:‘gpio0’, 1:‘gpio1’, 2:‘gpio2’, 3:‘gpio3’, 4:‘gpio4’,
  8. 5:‘gpio5’, 6:‘gpio6’, 7:‘gpio7’, 8:‘gpio8’, 9:‘gpio9’,
  9. 10:‘gpio10’, 11:‘gpio11’, 12:‘gpio12’, 13:‘gpio13’
  10. }
  11.  
  12. @app.route(“/”)
  13. def hello():
  14.  
  15. now = datetime.datetime.now()
  16. timeString = now.strftime(“%Y/%m/%d %H:%M:%S”)
  17. templateData = {
  18. ‘title’:‘HELLO!’,
  19. ‘time’:timeString
  20. }
  21. return render_template(‘main.html’,**templateData)
  22.  
  23. @app.route(“/readpin/”)
  24. def readPin(pin):
  25.  
  26. gpio.pinMode(channel[int(pin)],gpio.INPUT)
  27. value = ” “
  28.  
  29. if (gpio.digitalRead(channel[int(pin)]) == gpio.HIGH) :
  30. value = “Read GPIO” + pin + ” is high !”
  31. else :
  32. value = “Read GPIO” + pin +” is low !”
  33. templateData = {
  34. ‘title’ : ‘Status of GPIO’ + pin ,
  35. ‘value’ : value
  36. }
  37. return render_template(‘pin.html’,**templateData)
  38.  
  39. if __name__ == “__main__” :
  40. app.run (host=‘0.0.0.0’,port=80,debug=True)

5. Create a directory named templates under directory hello-gpio, and create two files there “main.html” and “pin.html’:

python_web_gpio_1

python_web_gpio_2

The content of file ‘main.html”:

01
02
03
04
05
06
07
08
09
10
11
12
<!DOCTYPE html>
        <head>
                <title>{{ title }} </title>
        </head>
        <body>
                <center>
                <h1>Welcome to pcDuino !</hl>
                <h2>The date and time on the server is :{{ time }}</h2>
                </center>
        </body>
</html>

The content of ‘pin.html’ is as following:

01
02
03
04
05
06
07
08
09
10
11
12
13
<!DOCTYPE html>
        <head>
                <title>{{ title }} </title>
        </head>
        <body>
                <center>
                <h1>Pin Status </hl>
                <h2>{{ value }}</h2>
                <hr>
                <a href="http://www.pcduino.org">pcDuino.org</a>
                </center>
        </body>
</html>

6. Type “$sudo python ./hello-gpio.py” to run the code:

 

python_web_gpio_3

 

7. Check IP address of pcDuino using “$ifconfig”:

python_web_gpio_4

 

8. Open a web browser on a PC that shares the same network, and enter the IP address of pcDuino found in step 7:

python_web_gpio_5

 

Enter 192.168.35/readpin/0 (this is to read GPIO 0), we will see :

 

python_web_gpio_6

 

 

python_web_gpio_7

 

192.168.35/readpin/7 will read GPIO 7.

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors