|
1
|
|
|
2
|
import kivy
|
|
3
|
from kivy.app import App
|
|
4
|
from kivy.uix.boxlayout import BoxLayout
|
|
5
|
from kivy.uix.label import Label
|
|
6
|
from kivy.uix.button import Button
|
|
7
|
from kivy.clock import Clock
|
|
8
|
from kivy.utils import get_color_from_hex
|
|
9
|
import requests
|
|
10
|
import json
|
|
11
|
import time
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
RASPBERRY_PI_IP = '192.168.71.159 '
|
|
16
|
API_LECTURA = f'http://{RASPBERRY_PI_IP}:5000/api/lectura'
|
|
17
|
API_CONTROL = f'http://{RASPBERRY_PI_IP}:5000/api/compuerta'
|
|
18
|
|
|
19
|
|
|
20
|
class GasMonitor(BoxLayout):
|
|
21
|
def __init__(self, **kwargs):
|
|
22
|
super(GasMonitor, self).__init__(orientation='vertical', padding=10, spacing=10)
|
|
23
|
|
|
24
|
|
|
25
|
self.status_label = Label(text="Iniciando Conexión...", size_hint_y=None, height=50)
|
|
26
|
self.add_widget(self.status_label)
|
|
27
|
|
|
28
|
|
|
29
|
self.mediciones_layout = BoxLayout(orientation='vertical', spacing=5)
|
|
30
|
self.labels = {}
|
|
31
|
sensores = ['CO', 'CH4', 'AIRE']
|
|
32
|
|
|
33
|
for sensor in sensores:
|
|
34
|
self.labels[sensor] = Label(text=f'{sensor}: --- PPM', font_size='24sp')
|
|
35
|
self.mediciones_layout.add_widget(self.labels[sensor])
|
|
36
|
|
|
37
|
self.add_widget(self.mediciones_layout)
|
|
38
|
|
|
39
|
|
|
40
|
self.add_widget(Label(text="Control de Compuerta (Buzzer)", size_hint_y=None, height=30))
|
|
41
|
|
|
42
|
self.control_layout = BoxLayout(spacing=10, size_hint_y=None, height=80)
|
|
43
|
|
|
44
|
btn_abrir = Button(text="ABRIR (ON)", background_color=get_color_from_hex('#006600'))
|
|
45
|
btn_abrir.bind(on_press=lambda instance: self.enviar_control('abrir'))
|
|
46
|
self.control_layout.add_widget(btn_abrir)
|
|
47
|
|
|
48
|
btn_cerrar = Button(text="CERRAR (OFF)", background_color=get_color_from_hex('#CC0000'))
|
|
49
|
btn_cerrar.bind(on_press=lambda instance: self.enviar_control('cerrar'))
|
|
50
|
self.control_layout.add_widget(btn_cerrar)
|
|
51
|
|
|
52
|
self.add_widget(self.control_layout)
|
|
53
|
|
|
54
|
|
|
55
|
Clock.schedule_interval(self.obtener_datos, 2)
|
|
56
|
|
|
57
|
def obtener_datos(self, dt):
|
|
58
|
"""Realiza la solicitud GET a la API de la Raspberry Pi."""
|
|
59
|
try:
|
|
60
|
response = requests.get(API_LECTURA, timeout=3)
|
|
61
|
data = response.json()
|
|
62
|
|
|
63
|
if data.get("status") == "OK":
|
|
64
|
self.status_label.text = f"CONECTADO: {data['timestamp']}"
|
|
65
|
self.status_label.color = get_color_from_hex('#00CC00')
|
|
66
|
|
|
67
|
self.labels['CO'].text = f"CO: {data['CO']:.2f} PPM"
|
|
68
|
self.labels['CH4'].text = f"CH4: {data['CH4']:.2f} PPM"
|
|
69
|
self.labels['AIRE'].text = f"AIRE: {data['AIRE']:.2f} PPM"
|
|
70
|
else:
|
|
71
|
self.status_label.text = f"ERROR API: {data.get('message', 'Desconocido')}"
|
|
72
|
self.status_label.color = get_color_from_hex('#FF9900')
|
|
73
|
|
|
74
|
except requests.exceptions.ConnectionError:
|
|
75
|
self.status_label.text = "ERROR: No se puede conectar a la PI"
|
|
76
|
self.status_label.color = get_color_from_hex('#CC0000')
|
|
77
|
except requests.exceptions.Timeout:
|
|
78
|
self.status_label.text = "ERROR: Tiempo de espera agotado"
|
|
79
|
self.status_label.color = get_color_from_hex('#FF6600')
|
|
80
|
except Exception as e:
|
|
81
|
self.status_label.text = f"ERROR: {e}"
|
|
82
|
self.status_label.color = get_color_from_hex('#CC0000')
|
|
83
|
|
|
84
|
def enviar_control(self, accion):
|
|
85
|
"""Envía una solicitud POST para abrir o cerrar la compuerta (buzzer)."""
|
|
86
|
url = f'{API_CONTROL}/{accion}'
|
|
87
|
try:
|
|
88
|
response = requests.post(url, timeout=3)
|
|
89
|
if response.status_code == 200:
|
|
90
|
self.status_label.text = f"Comando enviado: {accion.upper()}"
|
|
91
|
else:
|
|
92
|
self.status_label.text = f"Fallo de control: {response.status_code}"
|
|
93
|
except Exception as e:
|
|
94
|
self.status_label.text = f"Fallo al enviar comando: {e}"
|
|
95
|
|
|
96
|
class GasApp(App):
|
|
97
|
def build(self):
|
|
98
|
self.title = 'KEL AviGasDetector'
|
|
99
|
return GasMonitor()
|
|
100
|
|
|
101
|
if __name__ == '__main__':
|
|
102
|
GasApp().run()
|