0
0
Raspberry Piprogramming~10 mins

Controlling GPIO through web interface in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controlling GPIO through web interface
User opens web page
Web server receives request
Parse user input (e.g., button press)
Send command to GPIO pin
GPIO pin state changes (ON/OFF)
Web server sends updated page/status
User sees updated GPIO state on web page
User interacts with a web page, which sends commands to the Raspberry Pi's GPIO pins via a web server, changing pin states and updating the page.
Execution Sample
Raspberry Pi
from flask import Flask, render_template, request
import RPi.GPIO as GPIO

app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if 'on' in request.form:
            GPIO.output(18, GPIO.HIGH)
        elif 'off' in request.form:
            GPIO.output(18, GPIO.LOW)
    state = GPIO.input(18)
    return render_template('index.html', state=state)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
A simple Flask web server that controls GPIO pin 18 based on user button presses on a web page.
Execution Table
StepActionInputGPIO Pin 18 StateWeb Response
1Server startsNoneLOW (default)Web page loads with LED OFF
2User presses ON buttonPOST with 'on'HIGHPage reloads showing LED ON
3User presses OFF buttonPOST with 'off'LOWPage reloads showing LED OFF
4User refreshes pageGET requestLOWPage shows LED OFF
5User presses ON button againPOST with 'on'HIGHPage reloads showing LED ON
💡 Server runs continuously; stops on manual shutdown.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
GPIO Pin 18 StateLOWHIGHLOWLOWHIGH
Request MethodNonePOSTPOSTGETPOST
Request FormNone{'on'}{'off'}None{'on'}
Key Moments - 3 Insights
Why does the GPIO pin state change only after a POST request and not on GET?
Because the code changes GPIO output only when the request method is POST (see execution_table rows 2 and 3). GET requests just read and display the current state without changing it (row 4).
How does the web server know which button was pressed?
The server checks the form data in the POST request for 'on' or 'off' keys (execution_table rows 2 and 3). This tells it whether to set GPIO HIGH or LOW.
What happens if the user refreshes the page after turning the LED on?
A GET request is sent, which does not change GPIO state but shows the current state (HIGH if LED is on). This is shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the GPIO Pin 18 state after step 3?
ALOW
BHIGH
CUndefined
DFlashing
💡 Hint
Check the 'GPIO Pin 18 State' column in execution_table row 3.
At which step does the server receive a GET request?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Request Method' variable in variable_tracker after Step 4.
If the user presses the ON button twice in a row, how would the GPIO state change in the table?
AIt stays LOW both times.
BIt toggles between HIGH and LOW.
CIt stays HIGH after the first press and remains HIGH after the second.
DIt causes an error.
💡 Hint
GPIO output is set explicitly on POST; pressing ON twice keeps it HIGH (see execution_table steps 2 and 5).
Concept Snapshot
Controlling GPIO through web interface:
- Use a web server (e.g., Flask) to handle HTTP requests.
- On POST, read form data to decide GPIO pin state.
- Use RPi.GPIO to set pin HIGH or LOW.
- On GET, show current GPIO state.
- Update web page to reflect pin status.
Full Transcript
This example shows how a Raspberry Pi can control a GPIO pin using a web interface. The user opens a web page served by Flask. When the user presses ON or OFF buttons, the browser sends a POST request with form data. The server reads this data and sets GPIO pin 18 HIGH or LOW accordingly using the RPi.GPIO library. The server then reloads the page showing the current LED state. GET requests only display the current state without changing it. Variables like GPIO pin state and request method change step-by-step as the user interacts. This method allows easy remote control of hardware through a simple web page.