web_led.py

Raspberry Pi Model B Revision 2 (2012) — Board Sheet · File : web_led.py · Language : Python · Use case : Commander une sortie GPIO depuis une page web locale (démon, contrôle simple)

A minimal Flask server exposes /on and /off to drive an LED on GPIO17 from any LAN browser. Without built-in Wi-Fi, connect over Ethernet/USB dongle: révision 2 : alimentation micro-USB/OTG soignée, connecteur 26 broches (pas de 40 broches). It listens on 0.0.0.0:8000; never expose the board to the Internet.

Hardware

A Raspberry Pi Model B revision 2 (BCM2835, 26-pin GPIO header, Ethernet/USB ports moved to the board edge). LED + 330-ohm resistor, local network (Ethernet, Wi-Fi or USB dongle per model).

Wiring

LED on GPIO17 (pin 11) with resistor to GND. Nothing else but networking.

How it works

Each URL is a Flask route: the view calls led.on/off then re-renders with led.value. The single-thread server is fine for a demo.

Running it

Copy the script to the board (scp or USB stick), then run it with python3 (or bash). On Raspbian, install gpiozero with sudo apt install python3-gpiozero. The full 26-pin pinout is in the official Raspberry Pi schematic.

Variants

Radio buttons for several GPIOs; systemd unit as a service; add flask-login before any network exposure. Revision 2: a clean 5 V supply is required; brown-outs distort sensor readings.

web_led.py

# web_led.py — page web locale pour allumer/eteindre une LED (Flask).
# Deps: sudo apt install python3-flask python3-gpiozero
from flask import Flask
from gpiozero import LED

app = Flask(__name__)
led = LED(17)

@app.route("/")
def index():
    etat = "allumee" if led.value else "eteinte"
    return (f"<h1>LED {etat}</h1>"
            '<p><a href="/on">Allumer</a> | <a href="/off">Eteindre</a></p>')

@app.route("/on")
def on():
    led.on()
    return index()

@app.route("/off")
def off():
    led.off()
    return index()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

← Back to the board