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.subscribe("kobuki/data")
mqtt_client.on_message = on_message 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('/') @app.route('/')
def index(): def index():
return render_template('index.html') return render_template('index.html')
@@ -33,6 +41,12 @@ def control():
def move(): def move():
data = request.get_json() data = request.get_json()
direction = data.get("direction") 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 # Verstuur de richting via MQTT
if direction: if direction:
@@ -47,23 +61,12 @@ def phpmyadmin_passthrough(path):
@app.route("/database") @app.route("/database")
def database(): def database():
try: cursor = cnx.cursor()
cnx = mysql.connector.connect( cursor.execute("SELECT * FROM kobuki_data")
host="127.0.0.1", rows = cursor.fetchall()
port=3306, cursor.close()
user="admin", cnx.close()
password="kobuki", return str(rows)
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
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5000) app.run(debug=True, port=5000)