@@ -0,0 +1,13 | |||
|
1 | import paho.mqtt.publish as publish | |
|
2 | ||
|
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!" | |
|
11 | ||
|
12 | # Publicar el mensaje en el tópico | |
|
13 | publish.single(topico, mensaje, hostname=broker_address) |
@@ -0,0 +1,65 | |||
|
1 | import matplotlib.pyplot as plt | |
|
2 | import matplotlib.animation as animation | |
|
3 | import paho.mqtt.client as mqtt | |
|
4 | import numpy as np | |
|
5 | subscribe_topic="tesis/AnalogRaw" | |
|
6 | broker_address = "192.168.43.149" | |
|
7 | analogRawMatriz=[] | |
|
8 | contador = 0 | |
|
9 | data=[] | |
|
10 | def init(): | |
|
11 | ax.set_ylim(0, 500) # Ajusta los límites de la gráfica según tus necesidades | |
|
12 | ax.set_xlim(0,30) | |
|
13 | line.set_data([], []) | |
|
14 | return line, | |
|
15 | def update(frame): | |
|
16 | y_data = np.array(data) | |
|
17 | x_data = np.arange(len(y_data)) | |
|
18 | line.set_data(x_data, y_data) | |
|
19 | return line, | |
|
20 | # Callback cuando se establece la conexión con el broker MQTT | |
|
21 | def on_connect(client, userdata, flags, rc): | |
|
22 | if rc == 0: | |
|
23 | print("Conexión exitosa con el broker") | |
|
24 | # Suscribirse a un tópico después de la conexión exitosa | |
|
25 | client.subscribe(subscribe_topic) | |
|
26 | else: | |
|
27 | print("Error de conexión. Código de retorno =", rc) | |
|
28 | # Callback cuando se recibe un mensaje en el tópico suscrito | |
|
29 | def on_message(client, userdata, msg): | |
|
30 | global analogRawMatriz | |
|
31 | global contador | |
|
32 | global data | |
|
33 | mensaje = float(msg.payload.decode()) | |
|
34 | analogRawMatriz.append(mensaje) | |
|
35 | contador=contador+1 | |
|
36 | if contador==500: | |
|
37 | contador=0 | |
|
38 | valor_promedio = sum(analogRawMatriz) / len(analogRawMatriz) | |
|
39 | potencia=0.6233*valor_promedio+62.891 | |
|
40 | data.append(potencia) | |
|
41 | data[:] = data[-30:] | |
|
42 | analogRawMatriz=[] | |
|
43 | print(potencia) | |
|
44 | # Configurar el cliente MQTT | |
|
45 | client = mqtt.Client() | |
|
46 | # Configurar los callbacks | |
|
47 | client.on_connect = on_connect | |
|
48 | client.on_message = on_message | |
|
49 | client.connect(broker_address, port=1883, keepalive=60) | |
|
50 | # Configuración de la gráfica | |
|
51 | fig, ax = plt.subplots() | |
|
52 | line, = ax.plot([], [], lw=2) | |
|
53 | ax.set_xlabel('Tiempo') | |
|
54 | ax.set_ylabel('Analog Raw') | |
|
55 | ax.grid(True) | |
|
56 | # Configuración de la animación | |
|
57 | ani = animation.FuncAnimation(fig, update, frames=None, init_func=init, blit=True) | |
|
58 | # Mantener la ejecución del programa para recibir mensajes | |
|
59 | #client.loop_forever() | |
|
60 | client.loop_start() | |
|
61 | plt.show() | |
|
62 | client.loop_stop() | |
|
63 | """" | |
|
64 | PARA UN IPP DE 10MS Y 10 US DE ANCHO, la ecuación sera:PotLinea= 0.6233*AnalogRaw+62.891 | |
|
65 | """ No newline at end of file |
@@ -5,7 +5,7 int analogValue=0; | |||
|
5 | 5 | float Vout=0; |
|
6 | 6 | float Plinea=62.5,Vpk=0;// Valor entre 1 y 500 kW, las unidades son kW |
|
7 | 7 | float Plcal=0,dif=0,difPlow=0; |
|
8 |
bool serialData= |
|
|
8 | bool serialData=0;//Un valor de 1 indica que se debe ingresar por monitor serial | |
|
9 | 9 | float VoutRef=1,Pmax; |
|
10 | 10 | int numSamples=20; |
|
11 | 11 | int total=0; |
@@ -30,13 +30,18 void loop() { | |||
|
30 | 30 | int averageValue=analogRead(pinADC); |
|
31 | 31 | Vout = 0.8291*averageValue+90.27;//Ajuste realizado para el ADC |
|
32 | 32 | Plcal=5*pow((Vout+101),2)/(2*175.19*175.19);//Calculamos la potencia en la línea |
|
33 | ||
|
34 | Serial.println(Plcal); | |
|
35 | delay(10); | |
|
33 | 36 | //dif=abs(Plinea-Plcal)*100/Plinea;//Hallamos la diferencia porcentual |
|
37 | /* | |
|
34 | 38 | if (Plcal>4){//valores por debajo de este umbral serán leidos como tierra |
|
35 | 39 | Parray[contador]=Plcal; |
|
36 | 40 | contador =contador+1; |
|
37 | 41 | } |
|
38 | 42 | //cuando llenamos los valores hacemos la |
|
39 | if (contador>(SIZE-1)){ | |
|
43 | int x=SIZE-1; | |
|
44 | if (contador>x){ | |
|
40 | 45 | p=&Parray[0]; |
|
41 | 46 | Pmax=find_maximun(p); |
|
42 | 47 | contador=0; |
@@ -44,10 +49,9 void loop() { | |||
|
44 | 49 | difPlow=abs(Plinea-Pmax); |
|
45 | 50 | Serial.print("Valor normal: "); Serial.println(Pmax); |
|
46 | 51 | if(dif>10 && difPlow>9) { |
|
47 | Serial.print("Nivel anómalo: "); | |
|
48 | 52 | Serial.println(Pmax); |
|
49 | 53 | } |
|
50 |
|
|
|
54 | }*/ | |
|
51 | 55 | } |
|
52 | 56 | float ecuacionLineal(float Vpk){ |
|
53 | 57 | float m=175.19,b=-101; |
General Comments 0
You need to be logged in to leave comments.
Login now