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
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.
Software:
- Python and Flask installed.
- Adafruit Blinka library for GPIO control.
Steps
1. Install Flask and Adafruit Blinka
Run the following commands:
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:
4. HTML Template (index.html)
Create an index.html file in a templates folder:
5. Run the Flask App
Save the Python file as app.py and run it:
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 = Truecommand activates the relay. - The
relay_pin.value = Falsecommand deactivates the relay.
- The
Use Cases
- Control home appliances remotely.
- Create IoT projects for automation.
Comments
Post a Comment