add direction to db when pressed button

This commit is contained in:
ishak jmilou.ishak
2024-12-17 14:08:42 +01:00
parent 651dcbc6a5
commit 10a7a2b98c

View File

@@ -18,6 +18,14 @@ mqtt_client.loop_start()
mqtt_client.subscribe("kobuki/data")
mqtt_client.on_message = on_message
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="admin",
password="kobuki",
database="kobuki"
)
@app.route('/')
def index():
return render_template('index.html')
@@ -33,6 +41,12 @@ def control():
def move():
data = request.get_json()
direction = data.get("direction")
cursor = cnx.cursor()
cursor.execute("INSERT INTO kobuki_data (command) VALUES (%s)", (direction,))
cnx.commit()
cursor.close()
cnx.close()
# Verstuur de richting via MQTT
if direction:
@@ -47,23 +61,12 @@ def phpmyadmin_passthrough(path):
@app.route("/database")
def database():
try:
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="admin",
password="kobuki",
database="kobuki"
)
cursor = cnx.cursor()
cursor.execute("SELECT * FROM kobuki_data")
rows = cursor.fetchall()
cursor.close()
cnx.close()
return str(rows)
except mysql.connector.Error as err:
print(f"Database error: {err}")
return f"Database error: {err}", 500
cursor = cnx.cursor()
cursor.execute("SELECT * FROM kobuki_data")
rows = cursor.fetchall()
cursor.close()
cnx.close()
return str(rows)
if __name__ == '__main__':
app.run(debug=True, port=5000)