20/05/2026, 19:34
(Questo messaggio è stato modificato l'ultima volta il: 20/05/2026, 19:41 da Tanguy.)
Dal manulale "Getting started with raspberry" di Matt Richardson 2012 ho preso questo programma
WEBLAMP.PY
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
pins = {
24 : {'name' : 'coffee maker', 'state' : GPIO.LOW},
25 : {'name' : 'lamp', 'state' : GPIO.LOW}
}
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
@app.route("/")
def main():
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
templateData = {
'pins' : pins
}
return render_template('main.html', **templateData)
@app.route("/<changePin>/<action>")
def action(changePin, action):
changePin = int(changePin)
deviceName = pins[changePin]['name']
if action == "on":
GPIO.output(changePin, GPIO.HIGH)
message = "Turned " + deviceName + " on."
if action == "off":
GPIO.output(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."
if action == "toggle":
GPIO.output(changePin, not GPIO.input(changePin))
message = "Toggled " + deviceName + "."
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
templateData = {
'message' : message,
'pins' : pins
}
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='192.168.1.101', port=80, debug=True)
Che va messo in una directory assieme alla directory "templates" contenente questo programma:
MAIN.HTML
<!DOCTYPE html>
<head>
<title>Current Status</title>
</head>
<body>
<h1>Device Listing and Status</h1>
{% for pin in pins %}
<p>The {{ pins[pin].name }}
{%if pins[pin].state == true %}
is currently on (<a href="/{{pin}}/off">turn off</a>)
{% else %}
is currently off (<a href="/{{pin}}/on">turn on</a>)
{% endif %}
</p>
{% endfor %}
{% if message %}
<h2>{{ message }}</h2>
{% endif %}
</body>
</html>
Consente di pilotare,da un altro PC,alcune uscite,tramite browser,chiamando l'indirizzo IP del raspberry.
Su un raspberry PI 4 (Debian versione 11,release may 2023 64 bit,kernel6.1 Python 3.11.2) funziona,ma su un rsapberry PI5
(Debian 12 bookworm Nov 2024 64 bit kernel 6.6 python 3.9.2)da sempre,al lancio,:
lgpio.error "GPIO not allocated"
Cosa puo' essere ?
Ho notato che eliminando le ultime 3 righe di weblamp.py il programma termina subito,senza dare errore.
WEBLAMP.PY
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode(GPIO.BCM)
pins = {
24 : {'name' : 'coffee maker', 'state' : GPIO.LOW},
25 : {'name' : 'lamp', 'state' : GPIO.LOW}
}
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
@app.route("/")
def main():
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
templateData = {
'pins' : pins
}
return render_template('main.html', **templateData)
@app.route("/<changePin>/<action>")
def action(changePin, action):
changePin = int(changePin)
deviceName = pins[changePin]['name']
if action == "on":
GPIO.output(changePin, GPIO.HIGH)
message = "Turned " + deviceName + " on."
if action == "off":
GPIO.output(changePin, GPIO.LOW)
message = "Turned " + deviceName + " off."
if action == "toggle":
GPIO.output(changePin, not GPIO.input(changePin))
message = "Toggled " + deviceName + "."
for pin in pins:
pins[pin]['state'] = GPIO.input(pin)
templateData = {
'message' : message,
'pins' : pins
}
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='192.168.1.101', port=80, debug=True)
Che va messo in una directory assieme alla directory "templates" contenente questo programma:
MAIN.HTML
<!DOCTYPE html>
<head>
<title>Current Status</title>
</head>
<body>
<h1>Device Listing and Status</h1>
{% for pin in pins %}
<p>The {{ pins[pin].name }}
{%if pins[pin].state == true %}
is currently on (<a href="/{{pin}}/off">turn off</a>)
{% else %}
is currently off (<a href="/{{pin}}/on">turn on</a>)
{% endif %}
</p>
{% endfor %}
{% if message %}
<h2>{{ message }}</h2>
{% endif %}
</body>
</html>
Consente di pilotare,da un altro PC,alcune uscite,tramite browser,chiamando l'indirizzo IP del raspberry.
Su un raspberry PI 4 (Debian versione 11,release may 2023 64 bit,kernel6.1 Python 3.11.2) funziona,ma su un rsapberry PI5
(Debian 12 bookworm Nov 2024 64 bit kernel 6.6 python 3.9.2)da sempre,al lancio,:
lgpio.error "GPIO not allocated"
Cosa puo' essere ?
Ho notato che eliminando le ultime 3 righe di weblamp.py il programma termina subito,senza dare errore.

![[-] [-]](https://forum.raspberryitaly.com/images/square/collapse.png)

Facebook
Twitter
Google +
Youtube
Telegram