@@ -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 |
@@ -1,66 +1,70 | |||||
1 | #include <math.h> |
|
1 | #include <math.h> | |
2 | #define pinADC 26 |
|
2 | #define pinADC 26 | |
3 | float find_maximun(float *p); |
|
3 | float find_maximun(float *p); | |
4 | int analogValue=0; |
|
4 | int analogValue=0; | |
5 | float Vout=0; |
|
5 | float Vout=0; | |
6 | float Plinea=62.5,Vpk=0;// Valor entre 1 y 500 kW, las unidades son kW |
|
6 | float Plinea=62.5,Vpk=0;// Valor entre 1 y 500 kW, las unidades son kW | |
7 | float Plcal=0,dif=0,difPlow=0; |
|
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 | float VoutRef=1,Pmax; |
|
9 | float VoutRef=1,Pmax; | |
10 | int numSamples=20; |
|
10 | int numSamples=20; | |
11 | int total=0; |
|
11 | int total=0; | |
12 | int contador=0,SIZE=40; |
|
12 | int contador=0,SIZE=40; | |
13 | float *p, Parray[40]; |
|
13 | float *p, Parray[40]; | |
14 | void setup() { |
|
14 | void setup() { | |
15 | Serial.begin(115200); |
|
15 | Serial.begin(115200); | |
16 | analogSetAttenuation(ADC_11db); |
|
16 | analogSetAttenuation(ADC_11db); | |
17 | if(serialData){ |
|
17 | if(serialData){ | |
18 | Serial.println("Ingrese la potencia en la linea:"); |
|
18 | Serial.println("Ingrese la potencia en la linea:"); | |
19 | while (!Serial.available()); // Wait for input |
|
19 | while (!Serial.available()); // Wait for input | |
20 | String potLinChar = Serial.readStringUntil('\n'); |
|
20 | String potLinChar = Serial.readStringUntil('\n'); | |
21 | Plinea=atof(potLinChar.c_str()); |
|
21 | Plinea=atof(potLinChar.c_str()); | |
22 | } |
|
22 | } | |
23 | Vpk=sqrt(10*Plinea)/5; |
|
23 | Vpk=sqrt(10*Plinea)/5; | |
24 | VoutRef=ecuacionLineal(Vpk); |
|
24 | VoutRef=ecuacionLineal(Vpk); | |
25 | Serial.print("Voltaje de salida de referencia: "); |
|
25 | Serial.print("Voltaje de salida de referencia: "); | |
26 | Serial.print(VoutRef); |
|
26 | Serial.print(VoutRef); | |
27 | Serial.print("Voltaje pkpk:"); Serial.print(" "); Serial.println(Vpk); |
|
27 | Serial.print("Voltaje pkpk:"); Serial.print(" "); Serial.println(Vpk); | |
28 | } |
|
28 | } | |
29 | void loop() { |
|
29 | void loop() { | |
30 | int averageValue=analogRead(pinADC); |
|
30 | int averageValue=analogRead(pinADC); | |
31 | Vout = 0.8291*averageValue+90.27;//Ajuste realizado para el ADC |
|
31 | Vout = 0.8291*averageValue+90.27;//Ajuste realizado para el ADC | |
32 | Plcal=5*pow((Vout+101),2)/(2*175.19*175.19);//Calculamos la potencia en la línea |
|
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 | //dif=abs(Plinea-Plcal)*100/Plinea;//Hallamos la diferencia porcentual |
|
36 | //dif=abs(Plinea-Plcal)*100/Plinea;//Hallamos la diferencia porcentual | |
|
37 | /* | |||
34 | if (Plcal>4){//valores por debajo de este umbral serán leidos como tierra |
|
38 | if (Plcal>4){//valores por debajo de este umbral serán leidos como tierra | |
35 | Parray[contador]=Plcal; |
|
39 | Parray[contador]=Plcal; | |
36 | contador =contador+1; |
|
40 | contador =contador+1; | |
37 | } |
|
41 | } | |
38 | //cuando llenamos los valores hacemos la |
|
42 | //cuando llenamos los valores hacemos la | |
39 | if (contador>(SIZE-1)){ |
|
43 | int x=SIZE-1; | |
|
44 | if (contador>x){ | |||
40 | p=&Parray[0]; |
|
45 | p=&Parray[0]; | |
41 | Pmax=find_maximun(p); |
|
46 | Pmax=find_maximun(p); | |
42 | contador=0; |
|
47 | contador=0; | |
43 | dif=abs(Plinea-Pmax)*100/Plinea; |
|
48 | dif=abs(Plinea-Pmax)*100/Plinea; | |
44 | difPlow=abs(Plinea-Pmax); |
|
49 | difPlow=abs(Plinea-Pmax); | |
45 | Serial.print("Valor normal: "); Serial.println(Pmax); |
|
50 | Serial.print("Valor normal: "); Serial.println(Pmax); | |
46 | if(dif>10 && difPlow>9) { |
|
51 | if(dif>10 && difPlow>9) { | |
47 | Serial.print("Nivel anómalo: "); |
|
|||
48 | Serial.println(Pmax); |
|
52 | Serial.println(Pmax); | |
49 | } |
|
53 | } | |
50 |
|
|
54 | }*/ | |
51 | } |
|
55 | } | |
52 | float ecuacionLineal(float Vpk){ |
|
56 | float ecuacionLineal(float Vpk){ | |
53 | float m=175.19,b=-101; |
|
57 | float m=175.19,b=-101; | |
54 | return Vout=m*Vpk+b; |
|
58 | return Vout=m*Vpk+b; | |
55 | } |
|
59 | } | |
56 | float find_maximun(float *p){ |
|
60 | float find_maximun(float *p){ | |
57 | float maxi=*p; |
|
61 | float maxi=*p; | |
58 | float *q; |
|
62 | float *q; | |
59 | q=p; |
|
63 | q=p; | |
60 | for(int i=0;i<SIZE;i++){ |
|
64 | for(int i=0;i<SIZE;i++){ | |
61 | if(maxi<*(p+i)) { |
|
65 | if(maxi<*(p+i)) { | |
62 | maxi=*(p+i); |
|
66 | maxi=*(p+i); | |
63 | } |
|
67 | } | |
64 | } |
|
68 | } | |
65 | return maxi; |
|
69 | return maxi; | |
66 | } No newline at end of file |
|
70 | } |
General Comments 0
You need to be logged in to leave comments.
Login now