Remote GPIO Management on Raspberry Pi via Web Interface

Webpage-Controlled GPIO Activation for Home Automation Projects

1. Introduction

This Raspberry Pi project showcases its role as an IoT device. It enables the manipulation of LED states (ON/OFF) via a webpage accessible from any device connected to the same network as the Raspberry Pi. The project's principles serve as a foundation for diverse home automation endeavors.

2. Demonstration

3. Procedure

Follow these steps:

1. Set up the circuit connections according to the circuit schematics.

2. On the Raspberry Pi, create a new directory named ‘Web Server' (or any name you prefer). Inside this directory, create another folder called ‘Templates'. Ensure that the folder name is ‘Templates' as it is necessary for the webpage to function correctly during execution.

3. Save the Python file within the ‘Web Server' folder.

4. Save the HTML file inside the ‘Templates' folder.

5. Within the Python program, specify the IP address assigned to your Raspberry Pi. To find this address, open the command prompt and type ‘ifconfig', then press enter. A list of connections will appear, showing the IP address under ‘lan' (for LAN connection) or ‘wlan' (for WiFi connection).

6. Start the Python program on the Raspberry Pi. Then, enter the IP address in your web browser to access the web page.

Schematics

Code

from flask import Flask
from flask import render_template
import RPi.GPIO as rpi
import time

app= Flask(__name__)

led1,led2,led3= 3,5,7

rpi.setwarnings(False)
rpi.setmode(rpi.BOARD)
rpi.setup(led1, rpi.OUT)
rpi.setup(led2, rpi.OUT)
rpi.setup(led3, rpi.OUT)
rpi.output(led1, 0)
rpi.output(led2, 0)
rpi.output(led3, 0)
print("Done")

@app.route('/')
def index():
    return render_template('webpage.html')

@app.route('/A')
def led1on():
    rpi.output(led1,1)
    return render_template('webpage.html')

@app.route('/a')
def led1off():
    rpi.output(led1,0)
    return render_template('webpage.html')

@app.route('/B')
def led2on():
    rpi.output(led2,1)
    return render_template('webpage.html')

@app.route('/b')
def led2off():
    rpi.output(led2,0)
    return render_template('webpage.html')

@app.route('/C')
def led3on():
    rpi.output(led3,1)
    return render_template('webpage.html')

@app.route('/c')
def led3off():
    rpi.output(led3,0)
    return render_template('webpage.html')

if __name__=="__main__":
    print("Start")
    app.run(debug=True, host='192.168.1.57')

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top