@@ -0,0 +1,2 | |||
|
1 | IPP(km) DutyCicle(%) Wu Wd Nd Nu Eu Ed Sd Su | |
|
2 | 1500 0.1 250 0 0 0 0 0 200 200 No newline at end of file |
@@ -0,0 +1,113 | |||
|
1 | import paho.mqtt.client as mqtt | |
|
2 | from datetime import datetime | |
|
3 | import numpy as np | |
|
4 | import json | |
|
5 | import time | |
|
6 | subscribe_topic="tesis/test" | |
|
7 | publish_topic="tesis/potencia" | |
|
8 | broker_address = "localhost"#"192.168.43.149" | |
|
9 | analogRawMatriz=[[],[],[],[],[],[],[],[]] | |
|
10 | numMaxdatos=1000 | |
|
11 | contador = 0 | |
|
12 | data=[] | |
|
13 | interval_start_time = time.time() | |
|
14 | interval_duration=15 #duracion del intervalo en segundos para el envio de datos | |
|
15 | def potenciaAncho10us(AnalogRaw): | |
|
16 | potencia=[0,0,0,0,0,0,0,0] | |
|
17 | m=1.5476 | |
|
18 | b=-91.898 | |
|
19 | for i in range(8): | |
|
20 | potencia[i]=m*AnalogRaw[i]+b | |
|
21 | return potencia | |
|
22 | ||
|
23 | def potenciaAncho20us(AnalogRaw): | |
|
24 | potencia=[0,0,0,0,0,0,0,0] | |
|
25 | m=1.5476 | |
|
26 | b=-91.898 | |
|
27 | for i in range(8): | |
|
28 | potencia[i]=m*AnalogRaw[i]+b | |
|
29 | return potencia | |
|
30 | ||
|
31 | def leer_datos_desde_txt(archivo): | |
|
32 | try: | |
|
33 | with open(archivo, 'r') as file: | |
|
34 | lineas = file.readlines() | |
|
35 | # Obtiene la segunda línea (índice 1) | |
|
36 | segunda_linea = lineas[1] | |
|
37 | datos = segunda_linea.strip().split() | |
|
38 | datos_numericos = [float(dato) for dato in datos] | |
|
39 | return datos_numericos | |
|
40 | except FileNotFoundError: | |
|
41 | print(f"El archivo '{archivo}' no fue encontrado.") | |
|
42 | except Exception as e: | |
|
43 | print(f"Error al leer el archivo: {e}") | |
|
44 | ||
|
45 | def procesamiento_data(analogRawMatriz): | |
|
46 | if analogRawMatriz[0]: | |
|
47 | average_raw=[0,0,0,0,0,0,0,0,0] | |
|
48 | for i in range(8): | |
|
49 | average_raw[i] = sum(analogRawMatriz[i]) / len(analogRawMatriz[i]) | |
|
50 | if(ancho<15): | |
|
51 | potencia=potenciaAncho10us(average_raw) | |
|
52 | if ancho>15 and ancho<25: | |
|
53 | potencia=potenciaAncho20us(average_raw) | |
|
54 | processed_data = {"Ancho_us":IPP,"average_potencia": potencia, "timestamp": datetime.now()} | |
|
55 | #client.publish(publish_topic, json.dumps(processed_data)) | |
|
56 | print("---------------------------------------") | |
|
57 | print(average_raw) | |
|
58 | print(processed_data) | |
|
59 | ||
|
60 | def on_connect(client, userdata, flags, rc): | |
|
61 | if rc == 0: | |
|
62 | print("Conexión exitosa con el broker") | |
|
63 | client.subscribe(subscribe_topic) | |
|
64 | else: | |
|
65 | print("Error de conexión. Código de retorno =", rc) | |
|
66 | # Callback cuando se recibe un mensaje en el tópico suscrito | |
|
67 | def on_message(client, userdata, msg): | |
|
68 | global analogRawMatriz | |
|
69 | global contador | |
|
70 | mensaje = msg.payload.decode() | |
|
71 | lista=json.loads(mensaje) | |
|
72 | if contador<numMaxdatos: | |
|
73 | for i in range(8): | |
|
74 | analogRawMatriz[i].append(lista[i]) | |
|
75 | contador=contador+1 | |
|
76 | #Leo los datos de la configuracion | |
|
77 | archivo_txt = 'commandPotencia.txt' | |
|
78 | datos_numericos = leer_datos_desde_txt(archivo_txt) | |
|
79 | if len(datos_numericos)!=10: | |
|
80 | print("Hay más(o menos) valores de los que debería") | |
|
81 | else: | |
|
82 | IPP_km=datos_numericos[0] | |
|
83 | IPP=IPP_km*1/150#IPP en ms | |
|
84 | Dutty=datos_numericos[1] | |
|
85 | ancho=IPP*Dutty*pow(10,3)/100 | |
|
86 | potenciaNominal=datos_numericos[2:] | |
|
87 | ||
|
88 | # Configurar el cliente MQTT | |
|
89 | client = mqtt.Client() | |
|
90 | # Configurar los callbacks | |
|
91 | client.on_connect = on_connect | |
|
92 | client.on_message = on_message | |
|
93 | client.connect(broker_address, port=1883, keepalive=60) | |
|
94 | #client.loop_forever() | |
|
95 | ||
|
96 | client.loop_start() | |
|
97 | try: | |
|
98 | while True: | |
|
99 | # Verifica si ha pasado el intervalo de tiempo | |
|
100 | current_time = time.time() | |
|
101 | elapsed_time = current_time - interval_start_time | |
|
102 | if elapsed_time >= interval_duration: | |
|
103 | procesamiento_data(analogRawMatriz) # Calcula la potencia y envía los datos al nuevo tópico | |
|
104 | interval_start_time = current_time # Reinicia el temporizador del intervalo | |
|
105 | analogRawMatriz=[[],[],[],[],[],[],[],[]] | |
|
106 | # Puedes ajustar el tiempo de espera según tus necesidades | |
|
107 | time.sleep(1) | |
|
108 | ||
|
109 | except KeyboardInterrupt: | |
|
110 | print("Programa detenido por el usuario.") | |
|
111 | #procesamiento_data() # Asegúrate de enviar los datos acumulados antes de salir | |
|
112 | client.disconnect() | |
|
113 | client.loop_stop() |
@@ -1,13 +1,11 | |||
|
1 | import paho.mqtt.client as mqtt | |
|
1 | 2 | import paho.mqtt.publish as publish |
|
2 | 3 | |
|
3 | # Especificar la dirección del broker MQTT como localhost | |
|
4 | broker_address = "localhost" | |
|
5 | ||
|
6 | # Especificar el tópico al que deseas publicar | |
|
7 | topico = "tu_topico_aqui" | |
|
8 | ||
|
9 | # Mensaje que deseas publicar | |
|
10 | mensaje = "Hola, mundo MQTT!" | |
|
4 | MQTT_SERVER="10.10.10.102" | |
|
5 | MQTT_PORT = 1883 | |
|
11 | 6 | |
|
7 | publishTopic="tesis/test" | |
|
8 | broker_address = "localhost" | |
|
9 | mensaje = "[10,20,30,40,50,60,70,80]" | |
|
12 | 10 | # Publicar el mensaje en el tópico |
|
13 |
publish.single( |
|
|
11 | publish.single(publishTopic, mensaje, hostname=MQTT_SERVER) No newline at end of file |
@@ -8,7 +8,7 analogRawMatriz=[] | |||
|
8 | 8 | contador = 0 |
|
9 | 9 | data=[] |
|
10 | 10 | def init(): |
|
11 |
ax.set_ylim(0, |
|
|
11 | ax.set_ylim(900, 1800) # Ajusta los límites de la gráfica según tus necesidades | |
|
12 | 12 | ax.set_xlim(0,30) |
|
13 | 13 | line.set_data([], []) |
|
14 | 14 | return line, |
@@ -36,11 +36,11 def on_message(client, userdata, msg): | |||
|
36 | 36 | if contador==500: |
|
37 | 37 | contador=0 |
|
38 | 38 | valor_promedio = sum(analogRawMatriz) / len(analogRawMatriz) |
|
39 |
potencia= |
|
|
40 |
data.append( |
|
|
39 | # potencia=1.5476*valor_promedio-91.898 | |
|
40 | data.append(valor_promedio) | |
|
41 | 41 | data[:] = data[-30:] |
|
42 | 42 | analogRawMatriz=[] |
|
43 |
print( |
|
|
43 | print(valor_promedio) | |
|
44 | 44 | # Configurar el cliente MQTT |
|
45 | 45 | client = mqtt.Client() |
|
46 | 46 | # Configurar los callbacks |
@@ -61,5 +61,5 client.loop_start() | |||
|
61 | 61 | plt.show() |
|
62 | 62 | client.loop_stop() |
|
63 | 63 | """" |
|
64 |
PARA UN IPP DE 10MS Y 10 US DE ANCHO, la ecuación sera:PotLinea= |
|
|
64 | PARA UN IPP DE 10MS Y 10 US DE ANCHO, la ecuación sera:PotLinea= 1.5476*AnalogRaw-91.898 | |
|
65 | 65 | """ No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now