the lock up my pi 3 b+
Code:
import RPi.GPIO as GPIOimport timeimport datetimeimport sqlite3from flask import Flask, render_template, request, redirect, url_for# Initialize Flask appapp_flask = Flask(__name__)DATABASE = 'pill_dispenser.db'# Initialize the servo driver pinsservo_pins = [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46]# Initialize the servo driverdef initialize_servos(): GPIO.setmode(GPIO.BCM) GPIO.setup(servo_pins, GPIO.OUT) for pin in servo_pins: servo = GPIO.PWM(pin, 50) servo.start(0)# Function to move a servo to a specific angledef move_servo(servo, angle): duty = angle / 18 + 2 GPIO.output(servo, True) servo.ChangeDutyCycle(duty) time.sleep(1) GPIO.output(servo, False) servo.ChangeDutyCycle(0)# Initialize the databasedef initialize_database(): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS pills (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, hour INTEGER, am_pm TEXT, servo_channel INTEGER)''') conn.commit() conn.close()# Add a new pill to the scheduledef add_pill_to_schedule(name, hour, am_pm, servo_channel): if len(get_all_pills()) >= 30: raise ValueError("Maximum limit of 30 pills reached.") else: conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("INSERT INTO pills (name, hour, am_pm, servo_channel) VALUES (?, ?, ?, ?)", (name, hour, am_pm, servo_channel)) conn.commit() conn.close()# Get all pills from the scheduledef get_all_pills(): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills") pills = c.fetchall() conn.close() return pills# Get pills scheduled at the current timedef get_pills_at_schedule(current_time): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills WHERE hour=? AND am_pm=?", current_time) pills = c.fetchall() conn.close() return pills# Delete a pill from the scheduledef delete_pill_from_schedule(pill_id): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("DELETE FROM pills WHERE id=?", (pill_id,)) conn.commit() conn.close()# Get a pill by its IDdef get_pill_by_id(pill_id): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills WHERE id=?", (pill_id,)) pill = c.fetchone() conn.close() return pill# Update a pill in the scheduledef update_pill_in_schedule(pill_id, name, hour, am_pm, servo_channel): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("UPDATE pills SET name=?, hour=?, am_pm=?, servo_channel=? WHERE id=?", (name, hour, am_pm, servo_channel, pill_id)) conn.commit() conn.close()# Flask routes@app_flask.route('/')def index(): return render_template('index.html')@app_flask.route('/dispense')def dispense_pills(): try: initialize_servos() # Initialize servos # Code to activate the pill dispensing mechanism pills = get_all_pills() for pill in pills: servo_channel = pill[4] # Assuming servo_channel is the 5th column move_servo(servo_pins[servo_channel], 90) # Move servo to 90 degrees return "Pills dispensed" except Exception as e: print("Error in dispense_pills route:", e) return "An error occurred while dispensing pills"@app_flask.route('/schedule')def view_schedule(): try: pills = get_all_pills() return render_template('schedule.html', pills=pills) except Exception as e: print("Error in view_schedule route:", e) return "An error occurred while viewing schedule"@app_flask.route('/add_pill', methods=['GET', 'POST'])def add_pill(): if request.method == 'POST': try: name = request.form['name'] hour = int(request.form['hour']) am_pm = request.form['am_pm'] servo_channel = int(request.form['servo_channel']) # Assuming servo_channel is an integer add_pill_to_schedule(name, hour, am_pm, servo_channel) return redirect(url_for('view_schedule')) except ValueError as e: return render_template('error.html', error=str(e)) except Exception as e: print("Error in add_pill route:", e) return "An error occurred while adding pill" else: return render_template('add_pill.html')# Other routes remain the same...if __name__ == '__main__': initialize_database() # Initialize database app_flask.run(host='0.0.0.0', port=5000)
Statistics: Posted by bob5731 — Wed Apr 03, 2024 7:37 pm