-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (38 loc) · 1.3 KB
/
app.py
File metadata and controls
44 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
from flask import Flask, jsonify, render_template, request
import serial
import threading
import json
app = Flask(__name__)
arduino_data = {"distancia": 0, "detecto": 0, "servo": 0, "basura": 0}
# Simulación o conexión real al puerto serie
def leer_serial():
global arduino_data
try:
arduino = serial.Serial('COM5', 9600) # O Linux: '/dev/ttyUSB0'
while True:
linea = arduino.readline().decode('utf-8').strip()
if linea.startswith("{") and linea.endswith("}"):
try:
arduino_data.update(json.loads(linea))
except json.JSONDecodeError:
pass
except serial.SerialException:
print("No se pudo conectar al puerto serial")
@app.route("/datos")
def datos():
return jsonify(arduino_data)
@app.route("/update_basuras", methods=["POST"])
def update_basuras():
global arduino_data
data = request.get_json()
if "cantidad" in data:
arduino_data["basuras"] = data["cantidad"]
return jsonify(success=True, cantidad=arduino_data["basuras"])
@app.route("/")
def dashboard():
return render_template("dashboard.html")
if __name__ == "__main__":
hilo_serial = threading.Thread(target=leer_serial)
hilo_serial.daemon = True
hilo_serial.start()
app.run(debug=False)