diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5740036 Binary files /dev/null and b/.DS_Store differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..50a16f3 --- /dev/null +++ b/main.py @@ -0,0 +1,258 @@ +# MicroPython ESP32 - Guardián Fénix +# Sensores: BME680 + ADXL345 +# Servidor web para mostrar datos y registros + +import machine, time, ujson, network, socket, math +from machine import Pin, I2C +import bme680 # Asegúrate de tener la librería BME680 para MicroPython +import adxl345 + +# ------------------- +# CONFIGURACIÓN SENSORES +# ------------------- +i2c = I2C(scl=Pin(5), sda=Pin(4)) + +# BME680 +bme = bme680.BME680(i2c) + +# ADXL345 +acc = adxl345.ADXL345(i2c) + +# ------------------- +# CONFIGURACIÓN WIFI (HOTSPOT) +# ------------------- +ssid = "GuardianFenix" +password = "12345678" + +ap = network.WLAN(network.AP_IF) +ap.active(True) +ap.config(essid=ssid, password=password) + +print("Hotspot activo:", ssid) + +# ------------------- +# VARIABLES +# ------------------- +registros = [] # Lista de registros +aceptar_vib = False +alerta_activa = False + +# ------------------- +# FUNCIONES DE SENSORES +# ------------------- +def leer_bme680(): + bme.measure(gas=False) + temp = bme.temperature() + hum = bme.humidity() + pres = bme.pressure() + return {"temp": round(temp,1), "hum": round(hum,1), "pres": round(pres,1)} + +def leer_adxl345(): + x, y, z = acc.read_g() # m/s² + vib = abs((x*2 + y*2 + z*2) * 2) + estado = "Estable" + global alerta_activa + if vib > 6: + estado = "Inestable" + alerta_activa = True + else: + alerta_activa = False + return {"vib": round(vib,2), "estado": estado} + +# ------------------- +# REGISTRO AUTOMÁTICO +# ------------------- +def registrar(): + datos = leer_bme680() + vib = leer_adxl345() + datos["vib"] = vib["vib"] + datos["estado"] = vib["estado"] + datos["hora"] = time.localtime()[3:6] # HH:MM:SS + registros.append(datos) + # Mantener solo últimos 100 registros + if len(registros) > 100: + registros.pop(0) + return datos + +# ------------------- +# SERVIDOR WEB +# ------------------- +def web_page(): + return """ + + + + + Guardián Fénix + + + +

🛡️ Guardián Fénix 🔥

+ +
+

🌿 Sensor Ambiental (BME680)

+
+
Temp
-- °C
+
Hum
-- %
+
Pres
-- hPa
+
Gas
-- Ω
+
+
+ +
+

📳 Vibración (ADXL345)

+
+
Shake
--
+
ax=--g
+
ay=--g
+
az=--g
+
+
+ +
+

📝 Registros

+ + + + + + + + + + + +
#HoraTempHumPresGasShake
+
+ + + +""" + +# ------------------- +# SERVIDOR +# ------------------- +addr = socket.getaddrinfo('192.168.4.1',80)[0][-1] +s = socket.socket() +s.bind(addr) +s.listen(5) +print('Servidor escuchando en', addr) + +def handle_client(cl): + try: + req = cl.recv(1024) + req = str(req) + if 'GET /data' in req: + res = ujson.dumps(registros) + cl.send('HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n') + cl.send(res) + elif 'GET /leer' in req: + d = registrar() + cl.send('HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n') + cl.send(ujson.dumps(d)) + elif 'GET /guardar' in req: + d = registrar() + cl.send('HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n') + cl.send(ujson.dumps(d)) + elif 'GET /borrar' in req: + if registros: + registros.pop() + cl.send('HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n') + cl.send(ujson.dumps(registros)) + elif 'GET /toggle' in req: + global aceptar_vib + aceptar_vib = not aceptar_vib + cl.send('HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n') + cl.send(ujson.dumps({"aceptar_vib":aceptar_vib})) + else: + cl.send('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') + cl.send(web_page()) + except Exception as e: + print("Error:", e) + cl.close() + +# Loop principal +while True: + registrar() + cl, addr = s.accept() + handle_client(cl) \ No newline at end of file diff --git a/uC/.DS_Store b/uC/.DS_Store new file mode 100644 index 0000000..383f106 Binary files /dev/null and b/uC/.DS_Store differ diff --git a/uC/main_uC/.DS_Store b/uC/main_uC/.DS_Store new file mode 100644 index 0000000..249f467 Binary files /dev/null and b/uC/main_uC/.DS_Store differ