|
1
|
import requests
|
|
2
|
import time
|
|
3
|
import threading
|
|
4
|
from flask import Flask, jsonify
|
|
5
|
import sqlite3
|
|
6
|
import os
|
|
7
|
|
|
8
|
|
|
9
|
app = Flask(__name__)
|
|
10
|
|
|
11
|
BASE_URL = "http://172.27.64.91:5000"
|
|
12
|
|
|
13
|
estado = {
|
|
14
|
"conductividad": None,
|
|
15
|
"humedad": None,
|
|
16
|
"deshumidificador": None
|
|
17
|
}
|
|
18
|
|
|
19
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
20
|
|
|
21
|
DB_PATH = os.path.abspath(
|
|
22
|
os.path.join(BASE_DIR, '..', 'backend', 'datos.db')
|
|
23
|
)
|
|
24
|
|
|
25
|
|
|
26
|
def guardar_humedad_conductividad(humedad, temp, sensor_id, conductividad):
|
|
27
|
try:
|
|
28
|
conn = sqlite3.connect(DB_PATH)
|
|
29
|
c = conn.cursor()
|
|
30
|
if humedad is not None and temp is not None and sensor_id is not None:
|
|
31
|
c.execute('INSERT INTO humedad_log (sensor_id, humedad, temperatura) VALUES (?, ?, ?)',
|
|
32
|
(sensor_id, humedad, temp))
|
|
33
|
if conductividad is not None:
|
|
34
|
c.execute('INSERT INTO conductividad_log (conductividad) VALUES (?)',
|
|
35
|
(conductividad,))
|
|
36
|
conn.commit()
|
|
37
|
except Exception as e:
|
|
38
|
print("Error guardando en DB:", e)
|
|
39
|
finally:
|
|
40
|
conn.close()
|
|
41
|
|
|
42
|
|
|
43
|
@app.route('/estado', methods=['GET'])
|
|
44
|
def get_estado():
|
|
45
|
return jsonify(estado)
|
|
46
|
|
|
47
|
|
|
48
|
def leer_sensor():
|
|
49
|
try:
|
|
50
|
r = requests.get(f"{BASE_URL}/sensorC", timeout=3)
|
|
51
|
data = r.json()
|
|
52
|
estado["conductividad"] = data.get("conductividad")
|
|
53
|
except Exception as e:
|
|
54
|
print("Error leyendo sensor:", e)
|
|
55
|
estado["conductividad"] = None
|
|
56
|
|
|
57
|
def leer_humedad():
|
|
58
|
try:
|
|
59
|
r = requests.get(f"{BASE_URL}/esp", timeout=3)
|
|
60
|
data = r.json()
|
|
61
|
estado["humedad"] = data
|
|
62
|
except Exception as e:
|
|
63
|
print("Error leyendo humedad:", e)
|
|
64
|
estado["humedad"] = None
|
|
65
|
|
|
66
|
|
|
67
|
def desh_on():
|
|
68
|
try:
|
|
69
|
requests.post(f"{BASE_URL}/onDesH", timeout=3)
|
|
70
|
estado["deshumidificador"] = "on"
|
|
71
|
except Exception as e:
|
|
72
|
print("Error encendiendo deshumidificador:", e)
|
|
73
|
|
|
74
|
def desh_off():
|
|
75
|
try:
|
|
76
|
requests.post(f"{BASE_URL}/offDesH", timeout=3)
|
|
77
|
estado["deshumidificador"] = "off"
|
|
78
|
except Exception as e:
|
|
79
|
print("Error apagando deshumidificador:", e)
|
|
80
|
|
|
81
|
def loop():
|
|
82
|
while True:
|
|
83
|
leer_sensor()
|
|
84
|
leer_humedad()
|
|
85
|
|
|
86
|
print(estado["humedad"])
|
|
87
|
|
|
88
|
if estado["humedad"] != 0:
|
|
89
|
guardar_humedad_conductividad(
|
|
90
|
estado["humedad"].get("hum"),
|
|
91
|
estado["humedad"].get("temp"),
|
|
92
|
estado["humedad"].get("id"),
|
|
93
|
estado["conductividad"]
|
|
94
|
)
|
|
95
|
|
|
96
|
print("Estado actual:", estado)
|
|
97
|
print(DB_PATH)
|
|
98
|
time.sleep(5)
|
|
99
|
|
|
100
|
if __name__ == "__main__":
|
|
101
|
threading.Thread(target=loop, daemon=True).start()
|
|
102
|
|
|
103
|
app.run(host="0.0.0.0", port=5000)
|