Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, jsonify
- import serial
- app = Flask(__name__)
- bluetooth_port = 'COM3'
- baud_rate = 9600
- try:
- ser = serial.Serial(bluetooth_port, baud_rate)
- except serial.SerialException as e:
- ser = None
- print(f"Error: Could not open port {bluetooth_port}. {e}")
- @app.route('/')
- def index():
- return render_template('index.html')
- @app.route('/led/on')
- def turn_led_on():
- if ser:
- ser.write(b'1')
- return jsonify(status='LED On')
- else:
- return jsonify(status='Error: Could not open serial port'), 500
- @app.route('/led/off')
- def turn_led_off():
- if ser:
- ser.write(b'0')
- return jsonify(status='LED Off')
- else:
- return jsonify(status='Error: Could not open serial port'), 500
- if __name__ == '__main__':
- app.run(debug=True)
Add Comment
Please, Sign In to add comment