mirror of
https://github.com/adrigongv23/G26---Telemetry-Software.git
synced 2026-08-25 19:43:16 +02:00
Inicio del Django y base de datos
This commit is contained in:
parent
57494f02e5
commit
0e0c8d7d1b
66 changed files with 647 additions and 69 deletions
|
|
@ -2,34 +2,28 @@ import socket
|
|||
import time
|
||||
import math
|
||||
import random
|
||||
from datetime import datetime # <--- IMPORTANTE: Nueva librería
|
||||
|
||||
UDP_IP = "127.0.0.1"
|
||||
# --- CONFIGURACIÓN ---
|
||||
UDP_IP = "127.0.0.1"
|
||||
UDP_PORT = 4210
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
print("TELEMETRÍA SIMULADA - ENVÍO DE DATOS")
|
||||
print(" Formato: ECT | RPM | BATT | SPEED | HH:MM:SS")
|
||||
print(f"--- SIMULADOR DE COCHE G26 ---")
|
||||
print(f"Enviando datos falsos a {UDP_IP}:{UDP_PORT}")
|
||||
|
||||
t = 0
|
||||
while True:
|
||||
# Generación de datos (Igual que antes)
|
||||
ect = 85 + (5 * math.sin(t / 5.0)) + random.uniform(-0.2, 0.2)
|
||||
rpm = 3000 + (1000 * math.sin(t / 2.0))
|
||||
batt = 13.8 + random.uniform(-0.1, 0.1)
|
||||
speed = 90 + (30 * math.sin(t / 3.0))
|
||||
# Generar una temperatura que sube y baja mucho para probar todos los colores
|
||||
# Oscilará entre 40ºC y 120ºC
|
||||
ect = 80 + (40 * math.sin(t / 10.0)) + random.uniform(-0.5, 0.5)
|
||||
|
||||
# --- CAMBIO AQUÍ: OBTENER HORA REAL ---
|
||||
# Obtenemos hora actual y la convertimos a texto
|
||||
hora_real = datetime.now().strftime("%H:%M:%S.%f")[:-3] # Hora con milisegundos
|
||||
|
||||
# Mensaje con la hora legible al final
|
||||
mensaje = f"{ect:.2f}|{int(rpm)}|{batt:.2f}|{int(speed)}|{hora_real}"
|
||||
# Enviar solo el número
|
||||
mensaje = f"{ect:.2f}"
|
||||
|
||||
sock.sendto(mensaje.encode(), (UDP_IP, UDP_PORT))
|
||||
|
||||
print(f"TX: {mensaje}")
|
||||
print(f"Simulando: {mensaje} °C")
|
||||
|
||||
t += 0.1
|
||||
time.sleep(0.002)
|
||||
time.sleep(0.05)
|
||||
|
|
@ -2,91 +2,90 @@ import socket
|
|||
import matplotlib.pyplot as plt
|
||||
import matplotlib.animation as animation
|
||||
from collections import deque
|
||||
from datetime import datetime
|
||||
|
||||
# --- CONFIGURACIÓN ---
|
||||
UDP_IP = "0.0.0.0"
|
||||
UDP_PORT = 4210
|
||||
MAX_PUNTOS = 150
|
||||
UDP_IP = "0.0.0.0" # Escuchamos en Todas las interfaces posibles (WiFi, Ethernet...)
|
||||
UDP_PORT = 4210 # Mismo puerto que usamos para la ESP32
|
||||
MAX_PUNTOS = 200 # Vamos a mostrar en la gráfica los últimos 200 datos
|
||||
|
||||
# Cola de datos
|
||||
data_ect = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
data_rpm = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
data_bat = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
data_spd = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
|
||||
#Ocultamos estos valores ya que por el momento únicamente vamos a trabajar con la temperatura
|
||||
#data_rpm = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
#data_bat = deque([0]*MAX_PUNTOS, maxlen=MAX_PUNTOS)
|
||||
|
||||
# Configuración del socket UDP
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.bind((UDP_IP, UDP_PORT))
|
||||
sock.setblocking(False)
|
||||
|
||||
# --- DISEÑO GRÁFICO ---
|
||||
plt.style.use('dark_background')
|
||||
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True, figsize=(8, 10))
|
||||
fig, ax1 = plt.subplots(figsize=(10, 6)) # Usaremos una única ventana grande
|
||||
|
||||
# Título principal + RELOJ
|
||||
fig.canvas.manager.set_window_title('G26 Telemetry - Monitor de Temperatura')
|
||||
fig.suptitle('GADES TELEMETRY SYSTEM', fontsize=16, fontweight='bold', color='white')
|
||||
# Creamos un texto vacío arriba a la derecha para la hora
|
||||
texto_reloj = fig.text(0.85, 0.97, "--:--:--", fontsize=12, color='cyan', ha='center', fontweight='bold')
|
||||
|
||||
def setup_plot(ax, color, label, y_min, y_max):
|
||||
line, = ax.plot([], [], color=color, lw=2)
|
||||
ax.set_ylabel(label)
|
||||
ax.set_ylim(y_min, y_max)
|
||||
ax.grid(True, alpha=0.3, linestyle='--')
|
||||
props = dict(boxstyle='round', facecolor='black', alpha=0.7, edgecolor=color)
|
||||
text = ax.text(0.03, 0.85, '', transform=ax.transAxes,
|
||||
fontsize=14, color=color, fontweight='bold', bbox=props)
|
||||
return line, text
|
||||
# Reloj en la esquina superior derecha
|
||||
txt_reloj = fig.text(0.85, 0.95, '--:--:--', fontsize=12, color='white', fontweight='bold')
|
||||
|
||||
line_ect, txt_ect = setup_plot(ax1, '#ff3333', 'ECT (°C)', 50, 110)
|
||||
line_rpm, txt_rpm = setup_plot(ax2, '#ffff33', 'RPM', 0, 5000)
|
||||
line_bat, txt_bat = setup_plot(ax3, '#33ffff', 'BATT (V)', 12, 15)
|
||||
line_spd, txt_spd = setup_plot(ax4, '#33ff33', 'SPEED', 0, 140)
|
||||
# Configuración de la línea (empieza en blanco pero cambiará)
|
||||
line_ect, = ax1.plot([], [], color='white', lw=3)
|
||||
ax1.set_ylabel('Temperatura (°C)', fontsize=14)
|
||||
ax1.set_ylim(0, 130) # Rango de temperatura (0 a 130 grados)
|
||||
ax1.grid(True, alpha=0.3, linestyle='--')
|
||||
|
||||
# Caja de texto con el valor actual
|
||||
props = dict(boxstyle='round', facecolor='black', alpha=0.8, edgecolor='white')
|
||||
txt_ect = ax1.text(0.02, 0.90, 'ESPERANDO...', transform=ax1.transAxes,
|
||||
fontsize=16, color='white', fontweight='bold', bbox=props)
|
||||
|
||||
def get_color(temp):
|
||||
if temp > 105:
|
||||
return '#ff3333' # ROJO (Peligro)
|
||||
elif temp >= 95:
|
||||
return '#ffff33' # AMARILLO (Precaución)
|
||||
elif temp >= 65:
|
||||
return '#33ff33' # VERDE (Ok)
|
||||
else:
|
||||
return '#33ffff' # AZUL (Frío)
|
||||
|
||||
def update(frame):
|
||||
# 1. Actualizar el Reloj con la hora del PC
|
||||
ahora = datetime.now().strftime("%H:%M:%S")
|
||||
txt_reloj.set_text(f"HORA: {ahora}")
|
||||
try:
|
||||
while True:
|
||||
data, addr = sock.recvfrom(1024)
|
||||
msg = data.decode('utf-8')
|
||||
partes = msg.split('|')
|
||||
# partes = msg.split('|')
|
||||
|
||||
# Extraer valores numéricos
|
||||
val_ect = float(partes[0])
|
||||
val_rpm = float(partes[1])
|
||||
val_bat = float(partes[2])
|
||||
val_spd = float(partes[3])
|
||||
try:
|
||||
val_ect = float(msg) # Convertimos texto a número
|
||||
|
||||
data_ect.append(val_ect) # Guardamos el dato
|
||||
color_actual = get_color(val_ect) # Para que el valor cambie de color segun la temepratura
|
||||
|
||||
txt_ect.set_text(f"TEMP: {val_ect: .1f} °C")
|
||||
txt_ect.set_color(color_actual)
|
||||
|
||||
# --- EXTRAER LA HORA (Es el elemento 4) ---
|
||||
val_time = partes[4]
|
||||
|
||||
# Guardar datos
|
||||
data_ect.append(val_ect)
|
||||
data_rpm.append(val_rpm)
|
||||
data_bat.append(val_bat)
|
||||
data_spd.append(val_spd)
|
||||
|
||||
# Actualizar textos gráficas
|
||||
txt_ect.set_text(f"TEMP: {val_ect:.1f} °C")
|
||||
txt_rpm.set_text(f"RPM: {int(val_rpm)}")
|
||||
txt_bat.set_text(f"BATT: {val_bat:.2f} V")
|
||||
txt_spd.set_text(f"VEL: {int(val_spd)} Km/h")
|
||||
|
||||
# --- ACTUALIZAR EL RELOJ DE ARRIBA ---
|
||||
texto_reloj.set_text(f"{val_time}")
|
||||
except ValueError:
|
||||
print(f"Error de formato: {msg}")
|
||||
|
||||
except BlockingIOError:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
# Actualizamos la línea gráfica
|
||||
x_range = range(len(data_ect))
|
||||
line_ect.set_data(x_range, data_ect)
|
||||
line_rpm.set_data(x_range, data_rpm)
|
||||
line_bat.set_data(x_range, data_bat)
|
||||
line_spd.set_data(x_range, data_spd)
|
||||
ax1.set_xlim(0, max(len(data_ect), 1))
|
||||
|
||||
ax1.set_xlim(0, len(data_ect))
|
||||
|
||||
return line_ect, line_rpm, line_bat, line_spd, txt_ect, txt_rpm, txt_bat, txt_spd, texto_reloj
|
||||
return line_ect, txt_ect, txt_reloj
|
||||
|
||||
plt.tight_layout(rect=[0, 0, 1, 0.96]) # Dejar hueco arriba para el reloj
|
||||
ani = animation.FuncAnimation(fig, update, interval=20, blit=False, cache_frame_data=False)
|
||||
plt.show()
|
||||
Loading…
Add table
Add a link
Reference in a new issue