Sources : https://bottlepy.org/docs/dev/tutorial.html

https://deptinfo-ensip.univ-poitiers.fr/ENS/doku/doku.php/stu:python_reseau:bottlepy

 

sudo pip3 install bottle

from bottle import route, run

@route('/')
def hello():
    return "Hello World!"

run(host='localhost', port=80, debug=True)

 

 

from bottle import route, run
import RPi.GPIO as GPIO

host = '192.168.1.105'

pinButton = 17
pinLed = 18
GPIO.setmode(GPIO.BCM)
# pin 17 as input with pull-up resistor
GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# pin 18 as output
GPIO.setup(pinLed, GPIO.OUT)

LedState = False

def read_button():
    if GPIO.input(pinButton):
        return 'Released'
    else:
        return 'Pressed'

def update_led():
    GPIO.output(pinLed, LedState)
    
def toggleLed():
    global LedState
    LedState = not LedState
    
@route('/')
@route('/<arg>')
def index(arg=""):
    if arg == "toggle":
        toggleLed()
        
    update_led()
    
    response = "<html>\n"
    response += "<body>\n"
    response += "<script>\n"
    response += "function changed()\n"
    response += "{window.location.href='/toggle'}\n"
    response += "</script>\n"
    
    response += "Button: " + read_button() +"\n"
    response += "<br/><br/>\n"
    response += "<input type='button' onClick='changed()' value=' LED '/>\n"
    response += "</body></html>\n"
    return response
    
run(host=host, port=80)



from bottle import route, run

import RPi.GPIO as GPIO # import RPi.GPIO module


GPIO.setmode(GPIO.BCM)


GPIO.setup(16, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)


def led(pin):
if GPIO.input(pin):
GPIO.output(pin,0)
else :
GPIO.output(pin,1)


@route('/')
@route('/<arg>')
def index(arg=""):
test=""
if arg != "":
led( int(arg) )
test=arg

#update_led()

response = "<html>\n"
response += "<body>\n"
response += "<script>\n"
response += "function changed(pin)\n"
response += "{var req= '/'+pin;\n window.location.href=req;}\n"
response += "</script>\n"

response += "Led: " + test #read_button() +"\n"
response += "<br/>\n"
response += "<input type='button' onClick='changed(16)' value=' LED 16 '/>\n"
response += "&nbsp"+str(GPIO.input(16))
response += "<br/>\n"
response += "<input type='button' onClick='changed(19)' value=' LED 19 '/>\n"
response += "</body></html>\n"
return response


run(host='localhost', port=80, debug=True)

 

 

from bottle import route, run

import RPi.GPIO as GPIO # import RPi.GPIO module


GPIO.setmode(GPIO.BCM)


GPIO.setup(5, GPIO.OUT) #Lampe 1
GPIO.setup(6, GPIO.OUT) #Lampe 2
GPIO.setup(13, GPIO.OUT) #Lampe 3
GPIO.setup(16, GPIO.OUT) #Lampe 4
GPIO.setup(26, GPIO.OUT) #Gachette


def led(pin):
if GPIO.input(pin):
GPIO.output(pin,0)
else :
GPIO.output(pin,1)


@route('/')
@route('/<arg>')
def index(arg=""):
test=""
if arg != "":
led( int(arg) )
test=arg

#update_led()

response = "<html>\n"
response += "<body>\n"
response += "<script>\n"
response += "function changed(pin)\n"
response += "{var req= '/'+pin;\n window.location.href=req;}\n"
response += "</script>\n"

response += "Led: " + test #read_button() +"\n"
response += "<br/>\n"
response += "<input type='button' onClick='changed(5)' value=' LED 5 '/>\n"
response += "&nbsp"+str(GPIO.input(5))
response += "<br/><br/>\n"
response += "<input type='button' onClick='changed(6)' value=' LED 6 '/>\n"
response += "&nbsp"+str(GPIO.input(6))
response += "<br/><br/>\n"
response += "<input type='button' onClick='changed(13)' value=' LED 13 '/>\n"
response += "&nbsp"+str(GPIO.input(13))
response += "<br/><br/>\n"
response += "<input type='button' onClick='changed(16)' value=' LED 16 '/>\n"
response += "&nbsp"+str(GPIO.input(16))
response += "<br/><br/>\n"
response += "<input type='button' onClick='changed(26)' value=' LED 26 '/>\n"
response += "&nbsp"+str(GPIO.input(26))
response += "</body></html>\n"
return response

run(host='0.0.0.0', port=80, debug=True)

Source : https://github.com/bwssytems/ha-bridge 

run(host='0.0.0.0', port=80, debug=True)

En poursuivant votre navigation sur mon site, vous acceptez l’utilisation des Cookies et autres traceurs  pour réaliser des statistiques de visites et enregistrer sur votre machine vos activités pédagogiques. En savoir plus.