Go to the Home Page

Web Control Buzzer


.py
# -*- coding: utf-8 -*- from bottle import route,run,template,request import pigpio import time import threading BZ=18 BUZZERON=False gpio=pigpio.pi() gpio.set_mode(BZ,pigpio.OUTPUT) scale=[523,587,659,698,783,880,987,1046] def play_buzzer(): global BUZZERON while BUZZERON: for i in scale: if not BUZZERON: # 如果 BUZZERON 被设置为 False,退出循环 break gpio.hardware_PWM(BZ, i, 100) time.sleep(0.4) gpio.hardware_PWM(BZ, 0, 0) time.sleep(0.1) @route("/") def toppage(): return template("toppage") @route("/buzzer",method="get") def buzzer(): global BUZZERON buzzer_value=request.query.get("buzzer") if buzzer_value=="0": BUZZERON=False if buzzer_value=="1": BUZZERON=True threading.Thread(target=play_buzzer, daemon=True).start() return template("toppage") try: run(host="localhost",port=8080,debug=True) except KeyboardInterrupt: pass finally: gpio.hardware_PWM(BZ,0,0) gpio.stop()

.html
<!DOCTYPE html> <html lang="ja"> <head><meta charset="utf-8"><title>Web LED</title></head> <body> <h1>Top Page <form action="/buzzer"> <input name="buzzer" type="hidden" value="1"> <input type="submit" value="buzzer ON"> </form> <form action="/buzzer"> <input name="buzzer" type="hidden" value="0"> <input type="submit" value="buzzer OFF"> </form> </body> </html>