element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pi 3 HTML Control Interface Part 3
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 17 Oct 2016 6:22 PM Date Created
  • Views 905 views
  • Likes 3 likes
  • Comments 2 comments
Related
Recommended
  • raspberry pi3
  • rpi_3
  • raspi3

Raspberry Pi 3 HTML Control Interface Part 3

Former Member
Former Member
17 Oct 2016

This part focusses on controlling the gpio pins of the Pi using a web based user interface. Ive kept everything as simple as possible and also kept the page layout simple too.

 

Step 1

 

The webserver running on the Pi3 needs permission to access the gpio pins, this can be done easily by adding the webservers user account (www-data) to the gpio group

 

sudo adduser www-data gpio

 

now the pins can be controlled without the need of root permissions which makes things so much easier

 

Step 2

 

We need to create some Python scripts to run the Bash commands that controls the gpio pins, its likely that in a final application all of these commands will be put into a single Python file but to keep things segmented at this stage, a seperate file for each command was used. When Python files are added to the cgi-bin on the webserver make sure to use chown to change the owner of the file to the www-data user and chmod to make the files executable.

 

gpiodirectionin.py

#! /usr/bin/python
#

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo in > /sys/class/gpio/gpio"+pinnumber+"/direction")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

gpiodirectionout.py

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo out > /sys/class/gpio/gpio"+pinnumber+"/direction")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

gpioexport.py

#! /usr/bin/python
#

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo "+ pinnumber  +" > /sys/class/gpio/export")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

gpiounexport.py

 

#! /usr/bin/python
#

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo "+ pinnumber  +" > /sys/class/gpio/unexport")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

gpiovaluehigh.py

#! /usr/bin/python
#

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo 1 > /sys/class/gpio/gpio"+pinnumber+"/value")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

gpiovaluelow.py

#! /usr/bin/python
#

import os
import sys

os.getenv("QUERY_STRING")  
pinnumber = sys.stdin.read()

p = os.popen("echo 0 > /sys/class/gpio/gpio"+pinnumber+"/value")
p.close()

print "Content-Type: text/html\n\n"
print '<html><head><meta content="text/html; charset=UTF-8" />'
print "</body></html>"

 

Step 3

 

Rather than use the index.html file from the previous 2 blog posts, I made a new webpage to keep it easy to read. It contains a text field for a user to enter the gpio pin number to control and buttons to operate the pin exporting, direction control and set the high or low. The buttons call javascript functions which interact with the Python scripts above.

 

gpio.html

<html>
<body>
<input type="text" id="pinnumber" style="font-size:2.0em;" maxlength="2" size="1" value="1">
<input type="button" onClick="exportPin()" value="Export">
<input type="button" onClick="unexportPin()" value="Unexport">
<input type="button" onClick="setDirectionOut()" value="Set Output">
<input type="button" onClick="setPinHigh()" value="Set High">
<input type="button" onClick="setPinLow()" value="Set Low">
<script>
function exportPin(){
    var pinnumber = document.getElementById("pinnumber").value;  
    var req = new XMLHttpRequest();
    req.open("POST","/cgi-bin/gpioexport.py",true);  
    req.send(pinnumber);  
}
function unexportPin(){
        var pinnumber = document.getElementById("pinnumber").value;
        var req = new XMLHttpRequest();
        req.open("POST","/cgi-bin/gpiounexport.py",true);
        req.send(pinnumber);
}

function setDirectionOut(){
        var pinnumber = document.getElementById("pinnumber").value;
        var req = new XMLHttpRequest();
        req.open("POST","/cgi-bin/gpiodirectionout.py",true);
        req.send(pinnumber);
}

function setPinHigh(){
        var pinnumber = document.getElementById("pinnumber").value;
        var req = new XMLHttpRequest();
        req.open("POST","/cgi-bin/gpiovaluehigh.py",true);
        req.send(pinnumber);
}
function setPinLow(){
        var pinnumber = document.getElementById("pinnumber").value;
        var req = new XMLHttpRequest();
        req.open("POST","/cgi-bin/gpiovaluelow.py",true);
        req.send(pinnumber);
}



</script>


</body>
</html>

 

There's a video demonstrating the use of this system below, any questions please ask them in the comments and keep a look out for a follow up blog post showing how we can use the pins as input pins and display their values in the webpage!

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 8 years ago in reply to DAB +2
    Yes, that's a good idea; my voice is an absolute nightmare to record! A video I did yesterday might as well have been a silent movie, I had to provide a voice track for it to be edited with! A couple of…
  • Former Member
    Former Member over 8 years ago in reply to DAB

    Yes, that's a good idea; my voice is an absolute nightmare to record! A video I did yesterday might as well have been a silent movie, I had to provide a voice track for it to be edited with!

     

    A couple of weeks ago I was speaking on a radio show and they had to turn everybody elses microphones down and gain the master volume right up to make my voice level with everybody elses!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 8 years ago

    Hi Lucie,

     

    I would suggest you get a clip on microphone.

     

    Your voice just does not carry to the camera on these videos.

     

    Thanks

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube