Raspberry Pi Device Control: Python Flask Web App for On/Off Automation with Adafruit


To create a Flask web app that controls a device (on/off) using a Raspberry Pi and Adafruit libraries, follow the steps below:


Prerequisites

  1. Hardware:

    • Raspberry Pi with Raspbian OS installed.
    • Relay module.
    • Device to control (e.g., LED or an AC/DC device via a relay).
    • Jumper wires.
  2. Software:

    • Python and Flask installed.
    • Adafruit Blinka library for GPIO control.

Steps

1. Install Flask and Adafruit Blinka

Run the following commands:

sudo apt update
sudo apt install python3-pip
pip3 install flask adafruit-blinka

2. Wire the Relay to the Raspberry Pi

  • Connect the VCC and GND of the relay to the 5V and GND pins of the Raspberry Pi.
  • Connect the IN pin of the relay to a GPIO pin (e.g., GPIO17).

3. Python Flask Code

Create a Flask application to toggle the relay:

from flask import Flask, render_template, request
import board
import digitalio

app = Flask(__name__)

# Initialize GPIO pin
relay_pin = digitalio.DigitalInOut(board.D17)  # Replace D17 with your GPIO pin
relay_pin.direction = digitalio.Direction.OUTPUT

# Ensure relay is off initially
relay_pin.value = False


@app.route('/')
def index():
    # Show the web interface
    return render_template('index.html')


@app.route('/control', methods=['POST'])
def control():
    action = request.form['action']  # Get action from the form (on/off)
    if action == 'on':
        relay_pin.value = True
        return "Relay is ON"
    elif action == 'off':
        relay_pin.value = False
        return "Relay is OFF"
    else:
        return "Invalid action"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

4. HTML Template (index.html)

Create an index.html file in a templates folder:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Relay Control</title>
</head>
<body>
    <h1>Control Relay</h1>
    <form action="/control" method="post">
        <button name="action" value="on">Turn ON</button>
        <button name="action" value="off">Turn OFF</button>
    </form>
</body>
</html>

5. Run the Flask App

Save the Python file as app.py and run it:

python3 app.py

Access the Web App

  • Open a browser on the same network as the Raspberry Pi.
  • Navigate to http://<raspberry-pi-ip>:5000 (replace <raspberry-pi-ip> with your Raspberry Pi's IP address).
  • Use the buttons to turn the relay ON or OFF.

Explanation

  • Flask Web App: Provides a simple web interface to control the relay.
  • GPIO Control:
    • The relay_pin.value = True command activates the relay.
    • The relay_pin.value = False command deactivates the relay.

Use Cases

  • Control home appliances remotely.
  • Create IoT projects for automation.

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example