Controlling GPIO Outputs Using a Web Interface with Raspberry Pi

You want to control GPIO outputs using a web interface to your Raspberry Pi. Today I show you how to Controlling GPIO Outputs Using a Web Interface with Raspberry Pi. Fast you have to need to know how to Use the bottle Python web server library to create an HTML web interface.


≡ Writing a Simple Web Server in Python:


To get it how the program works, we, to begin with, have to be seen at how a web interface works. All web interfacing depends on a server someplace responding to requests from a web browser. When the server receives a request, it looks at the information that comes with the request and formulates a few HyperText Markup Language (HTML) in response.

Utilize the bottle Python library to run an unadulterated Python web server that will respond to HTTP requests. To install bottle, utilize the following command:

sudo apt-get introduce python-bottle

≡ Simple Web Server in Python Code:


from bottle import route, run, template
from datetime import datetime

@route('/')
def index(name='time'):
    dt = datetime.now()
    time = "{:%Y-%m-%d %H:%M:%S}".format(dt)
    return template(' <head> <h1> Welcome to MechatronicsLab </h1> </head> <br>    '
                    '<b> <h3> Raspberry Pi Thinks Today is : </h3>  <br> date/time : {{t}}</b>', t=time)

run(host='localhost', port=8080)

When you run this code you can see like this

[caption id="attachment_709" align="alignnone" width="300"]Browsing to a Python bottle web server Browsing to a Python bottle web server[/caption]

 


≡ Components Required Web Interface Raspberry Pi :


1.Raspberry pi

2. Micro SD Card

3.Ethernet cable

4.Breadboard

5. Connecting wire

7. Resistors

8.Push Button Switch

9.  5mm  LED 

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook


≡ Circuit diagram Web Interface Raspberry Pi:


The breadboard layout for this is shown in

Controlling GPIO Outputs Using a Web_bb Controlling GPIO Outputs Using a Web_bb


≡ Code Web Interface Raspberry Pi:


Open an editor (nano or IDLE) and paste in the following code

from bottle import route, run
import RPi.GPIO as GPIO
GPIO.setwarnings(False)


GPIO.setmode(GPIO.BCM)
led_pins = [18, 23, 24]
led_states = [0, 0, 0]
switch_pin = 25

GPIO.setup(led_pins[0], GPIO.OUT)
GPIO.setup(led_pins[1], GPIO.OUT)
GPIO.setup(led_pins[2], GPIO.OUT)
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def switch_status():
    state = GPIO.input(switch_pin)
    if state:
        return 'Up'
    else:
        return 'Down'

def html_for_led(led):
    l = str(led)
    result = " <input type='button' onClick='changed(" + l + ")' value='LED " + l + "'/>"
    return result

def update_leds():
    for i, value in enumerate(led_states):
        GPIO.output(led_pins[i], value)

@route('/')
@route('/<led>')
def index(led):
    if led >= '0' and led <= '9':
        led_num = int(led)
        led_states[led_num] = not led_states[led_num]
        update_leds()
    response = "<script>"
    response += "function changed(led)"
    response += "{"
    response += "  window.location.href='/' + led"
    response += "}"
    response += "</script>"
    
    response += '<h1>MechatronicsLAB GPIO Control Raspberry Pi</h1>'
    response += '<h2>Button=' + switch_status() + '</h2>'
    response += '<h2>LEDs</h2>'
    response += html_for_led(0) 
    response += html_for_led(1) 
    response += html_for_led(2) 
    return response

try:
    run(host='0.0.0.0', port=8080)
finally:
    print('\nCleaning up')
    GPIO.cleanup()

If it starts correctly, you should see a message like this:

Bottle server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8080/
Hit Ctrl-C to quit.

Open a browser window from any machine on your networkindeed the Raspberry Pi itself, and explore to the IP address of the Raspberry Pi. The web interface appeared in ought to show up.

[caption id="attachment_710" align="alignnone" width="300"]A web interface to GPIO A web interface to GPIO[/caption]

On the off chance that you tap on one of the three Led buttons at the bottom of the screen, you ought to discover that the suitable Led toggles on and off. Also, in case you hold down the button as you reload the web page, you should see that the content next to Button says Down instead of Up.

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS

No comments

Theme images by Dizzo. Powered by Blogger.