0
0
Raspberry Piprogramming~30 mins

Controlling GPIO through web interface in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Controlling GPIO through web interface
📖 Scenario: You have a Raspberry Pi connected to an LED light on GPIO pin 17. You want to control this LED from a simple web page by turning it ON or OFF.
🎯 Goal: Build a small Python web server that shows a web page with two buttons: Turn ON and Turn OFF. When you click these buttons, the LED connected to GPIO pin 17 will turn on or off accordingly.
📋 What You'll Learn
Use the gpiozero library to control the GPIO pin.
Use the Flask web framework to create the web server.
Create a web page with two buttons labeled Turn ON and Turn OFF.
When a button is clicked, the server should turn the LED on or off.
Display the current LED status on the web page.
💡 Why This Matters
🌍 Real World
This project shows how to control physical devices like lights or motors remotely using a web browser, which is useful for home automation or IoT projects.
💼 Career
Understanding how to connect hardware control with web interfaces is valuable for roles in embedded systems, IoT development, and automation engineering.
Progress0 / 4 steps
1
Setup GPIO LED control
Import LED from gpiozero and create an LED object called led connected to GPIO pin 17.
Raspberry Pi
Need a hint?

Use from gpiozero import LED and then led = LED(17).

2
Setup Flask web server
Import Flask and create a Flask app called app.
Raspberry Pi
Need a hint?

Use from flask import Flask and then app = Flask(__name__).

3
Create routes to control LED
Create two routes in app: /on to turn the LED on using led.on() and /off to turn the LED off using led.off(). Both routes should return a simple string confirming the action.
Raspberry Pi
Need a hint?

Use @app.route('/on') and @app.route('/off') decorators and call led.on() or led.off() inside the functions.

4
Create main page with buttons and run server
Create a route / that returns an HTML page with two buttons: one linking to /on labeled Turn ON and one linking to /off labeled Turn OFF. Then add the code to run the Flask app with app.run(host='0.0.0.0'). Finally, print the message "Server started".
Raspberry Pi
Need a hint?

Use @app.route('/') to create the main page. Return HTML with two buttons linking to /on and /off. Use app.run(host='0.0.0.0') to start the server and print "Server started".