Flask
Python code
Connect the LED to GPIO 21 using 330 ohm resistor, breadboard and jumpers as described in Interfacing lesson.
Open Thonny editor, use the following code and save it to flask folder as GPIO-Flask.py:
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#define LED GPIO
ledRed = 21
#initialize GPIO status variable
ledRedSts = 0
# Define led pin as output
GPIO.setup(ledRed, GPIO.OUT)
# turn led OFF
GPIO.output(ledRed, GPIO.LOW)
@app.route("/")
def index():
# Read GPIO Status
ledRedSts = GPIO.input(ledRed)
templateData = {
'ledRed' : ledRedSts,
}
return render_template('index.html', **templateData)
@app.route("/<devicename>/<action>")
def action(deviceName, action):
if deviceName == 'ledRed':
actuator = ledRed
if action == "on":
GPIO.output(actuator, GPIO.HIGH)
if action == "off":
GPIO.output(actuator, GPIO.LOW)
ledRedSts = GPIO.input(ledRed)
templateData = { 'ledRed' : ledRedSts,}
return render_template('index.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
You can find the GPIO-Flask.py code here: https://pastebin.com/QnnrvVMC
Open terminal window in flask directory and use the following command to test your web server:
sudo python GPIO-Flask.py
You will
get a response from server that server is active.
Open the other terminal window and use ifconfig command to see your IP
address:

As you can see this Raspberry Pi is connected to local WiFi network with IP 192.168.5.102 (highlited).
Use the other device such as smartphone or desktop computer from same local network and enter the IP address given from ifconfig in your web browser URL bar and it will open a simple web page which allows you to control the LED state.

Relay modules can be used with Raspberry Pi instead of LED to control house devices and collect sensor readings. The Raspberry Pi sends signals to the relay module to turn on/off the connected devices, such as lights or appliances. The relay module acts as a switch that physically connects and disconnects the power supply to the controlled device. Sensors, such as temperature or humidity sensors, can be connected to the Raspberry Pi to collect data and monitor the environment. The Raspberry Pi processes the sensor readings and can be programmed to take actions based on the collected data. This setup allows for automation and remote control of house devices and collection of sensor readings through the Raspberry Pi.
The code in GPIO-Flask.py is a simple Flask application to control the state of an LED connected to a Raspberry Pi using GPIO (General Purpose Input/Output) pins.
- Importing
the required libraries: The code starts with importing the necessary libraries,
which are Flask to create the web application, render_template to
render the HTML templates, and request to handle the incoming HTTP
requests.
- Initializing
the Flask application: A new Flask application is created by instantiating the
Flask class with __name__ as argument.
- GPIO
setup: The code sets the GPIO pin numbering mode to GPIO.BCM, which
stands for Broadcom SOC channel, and disables warnings.
- LED
GPIO definition: A constant named ledRed is defined with the value 21,
which is the GPIO number connected to the LED. An LED status variable ledRedSts
is also initialized with the value 0. The LED pin is then set as an output
using the GPIO.setup method. The LED is turned off using the GPIO.output
method by passing the ledRed constant and GPIO.LOW as arguments.
- The
index route: A index function is defined as the default route of the
Flask application, which will handle the root URL requests. The function reads
the status of the LED and stores it in the ledRedSts variable. The LED
status is then passed to an HTML template named index.html as a template
variable named ledRed. The function returns the rendered template.
- The
action route: A action function is defined to handle requests to the URL
with the format /<deviceName>/<action>. The function takes
two arguments, deviceName and action, which are extracted from
the URL. If the device name is ledRed, the actuator variable is
set to ledRed. The function then checks the action argument and
sets the corresponding GPIO output accordingly to turn the LED on or off. The
LED status is then updated and passed to the index.html template,
similar to the index route.
- The main function: The code checks if the script is being executed as the main module and runs the Flask application with host set to 0.0.0.0, port set to 80, and the debug mode enabled.
This application can be run on a Raspberry Pi with the LED connected to the GPIO 21 pin and will create a simple web interface to control the LED.