@@ -1,17 +1,17 | |||||
1 | import paho.mqtt.client as mqtt |
|
1 | import paho.mqtt.client as mqtt | |
2 | from datetime import datetime |
|
2 | import paho.mqtt.publish as publish | |
|
3 | from datetime import datetime, timezone, timedelta | |||
3 | import numpy as np |
|
4 | import numpy as np | |
4 | import json |
|
5 | import json | |
5 | import time |
|
6 | import time | |
6 |
subscribe_topic="tesis/ |
|
7 | subscribe_topic="tesis/analogRaw" | |
7 |
publish_topic=" |
|
8 | publish_topic="atrad/test3"#"tesis/potencia" | |
8 |
broker_address = " |
|
9 | broker_address = "10.10.10.102"#10.10.10.102"#"192.168.43.149" | |
9 | analogRawMatriz=[[],[],[],[],[],[],[],[]] |
|
10 | analogRawMatriz=[[],[],[],[],[],[],[],[]]#guardamos los valores del | |
10 | numMaxdatos=1000 |
|
11 | numMaxdatos=1000 #definimos el numero máximo de datos a promediar | |
11 | contador = 0 |
|
12 | contador = 0 | |
12 | data=[] |
|
|||
13 | interval_start_time = time.time() |
|
13 | interval_start_time = time.time() | |
14 |
interval_duration= |
|
14 | interval_duration=30 #duracion del intervalo (en segundos) para el envio de datos | |
15 | def potenciaAncho10us(AnalogRaw): |
|
15 | def potenciaAncho10us(AnalogRaw): | |
16 | potencia=[0,0,0,0,0,0,0,0] |
|
16 | potencia=[0,0,0,0,0,0,0,0] | |
17 | m=1.5476 |
|
17 | m=1.5476 | |
@@ -22,11 +22,20 def potenciaAncho10us(AnalogRaw): | |||||
22 |
|
22 | |||
23 | def potenciaAncho20us(AnalogRaw): |
|
23 | def potenciaAncho20us(AnalogRaw): | |
24 | potencia=[0,0,0,0,0,0,0,0] |
|
24 | potencia=[0,0,0,0,0,0,0,0] | |
25 |
m= |
|
25 | m=0.6233 | |
26 |
b= |
|
26 | b=62.891 | |
27 | for i in range(8): |
|
27 | for i in range(8): | |
28 | potencia[i]=m*AnalogRaw[i]+b |
|
28 | potencia[i]=m*AnalogRaw[i]+b | |
29 | return potencia |
|
29 | return potencia | |
|
30 | #la potencia default considera en general valores mayores a 100 us | |||
|
31 | def potenciaDefault(AnalogRaw): | |||
|
32 | potencia=[0,0,0,0,0,0,0,0] | |||
|
33 | m=0.6233 | |||
|
34 | b=62.891 | |||
|
35 | for i in range(8): | |||
|
36 | potencia[i]=m*AnalogRaw[i]+b | |||
|
37 | return potencia | |||
|
38 | ||||
30 |
|
39 | |||
31 | def leer_datos_desde_txt(archivo): |
|
40 | def leer_datos_desde_txt(archivo): | |
32 | try: |
|
41 | try: | |
@@ -51,11 +60,18 def procesamiento_data(analogRawMatriz): | |||||
51 | potencia=potenciaAncho10us(average_raw) |
|
60 | potencia=potenciaAncho10us(average_raw) | |
52 | if ancho>15 and ancho<25: |
|
61 | if ancho>15 and ancho<25: | |
53 | potencia=potenciaAncho20us(average_raw) |
|
62 | potencia=potenciaAncho20us(average_raw) | |
54 | processed_data = {"Ancho_us":IPP,"average_potencia": potencia, "timestamp": datetime.now()} |
|
63 | timestamp_actual = int(time.time()) | |
55 | #client.publish(publish_topic, json.dumps(processed_data)) |
|
64 | fecha_utc=datetime.utcfromtimestamp(timestamp_actual) | |
|
65 | zona_horaria_utc_menos_5 = timezone(timedelta(hours=-5)) | |||
|
66 | fecha_utc_menos_5 = fecha_utc.replace(tzinfo=timezone.utc).astimezone(zona_horaria_utc_menos_5) | |||
|
67 | fecha_legible=fecha_utc_menos_5.strftime('%d-%m-%Y %H:%M:%S') | |||
|
68 | estado=[1 if x > 0 else 0 for x in potenciaNominal] | |||
|
69 | processed_data = {"Ancho_us":IPP,"average_potencia": potencia, "timestamp": fecha_legible,"potenciaNominal":potenciaNominal,"estado":estado} | |||
|
70 | client.publish(publish_topic, json.dumps(processed_data)) | |||
56 | print("---------------------------------------") |
|
71 | print("---------------------------------------") | |
57 | print(average_raw) |
|
72 | print(average_raw) | |
58 | print(processed_data) |
|
73 | print(processed_data) | |
|
74 | print(type(processed_data["average_potencia"])) | |||
59 |
|
75 | |||
60 | def on_connect(client, userdata, flags, rc): |
|
76 | def on_connect(client, userdata, flags, rc): | |
61 | if rc == 0: |
|
77 | if rc == 0: | |
@@ -73,7 +89,7 def on_message(client, userdata, msg): | |||||
73 | for i in range(8): |
|
89 | for i in range(8): | |
74 | analogRawMatriz[i].append(lista[i]) |
|
90 | analogRawMatriz[i].append(lista[i]) | |
75 | contador=contador+1 |
|
91 | contador=contador+1 | |
76 | #Leo los datos de la configuracion |
|
92 | #Leo los datos de la configuracion y halla el ancho | |
77 | archivo_txt = 'commandPotencia.txt' |
|
93 | archivo_txt = 'commandPotencia.txt' | |
78 | datos_numericos = leer_datos_desde_txt(archivo_txt) |
|
94 | datos_numericos = leer_datos_desde_txt(archivo_txt) | |
79 | if len(datos_numericos)!=10: |
|
95 | if len(datos_numericos)!=10: | |
@@ -84,14 +100,12 else: | |||||
84 | Dutty=datos_numericos[1] |
|
100 | Dutty=datos_numericos[1] | |
85 | ancho=IPP*Dutty*pow(10,3)/100 |
|
101 | ancho=IPP*Dutty*pow(10,3)/100 | |
86 | potenciaNominal=datos_numericos[2:] |
|
102 | potenciaNominal=datos_numericos[2:] | |
87 |
|
||||
88 | # Configurar el cliente MQTT |
|
103 | # Configurar el cliente MQTT | |
89 | client = mqtt.Client() |
|
104 | client = mqtt.Client() | |
90 | # Configurar los callbacks |
|
105 | # Configurar los callbacks | |
91 | client.on_connect = on_connect |
|
106 | client.on_connect = on_connect | |
92 | client.on_message = on_message |
|
107 | client.on_message = on_message | |
93 | client.connect(broker_address, port=1883, keepalive=60) |
|
108 | client.connect(broker_address, port=1883, keepalive=60) | |
94 | #client.loop_forever() |
|
|||
95 |
|
109 | |||
96 | client.loop_start() |
|
110 | client.loop_start() | |
97 | try: |
|
111 | try: | |
@@ -100,14 +114,13 try: | |||||
100 | current_time = time.time() |
|
114 | current_time = time.time() | |
101 | elapsed_time = current_time - interval_start_time |
|
115 | elapsed_time = current_time - interval_start_time | |
102 | if elapsed_time >= interval_duration: |
|
116 | if elapsed_time >= interval_duration: | |
103 |
procesamiento_data(analogRawMatriz) # Calcula la potencia y envía los datos |
|
117 | procesamiento_data(analogRawMatriz) # Calcula la potencia y envía los datos | |
104 | interval_start_time = current_time # Reinicia el temporizador del intervalo |
|
118 | interval_start_time = current_time # Reinicia el temporizador del intervalo | |
105 | analogRawMatriz=[[],[],[],[],[],[],[],[]] |
|
119 | analogRawMatriz=[[],[],[],[],[],[],[],[]] | |
106 | # Puedes ajustar el tiempo de espera según tus necesidades |
|
|||
107 | time.sleep(1) |
|
120 | time.sleep(1) | |
108 |
|
121 | |||
109 | except KeyboardInterrupt: |
|
122 | except KeyboardInterrupt: | |
110 | print("Programa detenido por el usuario.") |
|
123 | print("Programa detenido por el usuario.") | |
111 | #procesamiento_data() # Asegúrate de enviar los datos acumulados antes de salir |
|
|||
112 | client.disconnect() |
|
124 | client.disconnect() | |
113 | client.loop_stop() |
|
125 | client.loop_stop() | |
|
126 | #client.loop_forever() No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now