@@ -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,34 | |||||
|
1 | import paho.mqtt.client as mqtt | |||
|
2 | ||||
|
3 | subscribe_topic="tesis/potencia" | |||
|
4 | broker_address = "192.168.43.149" | |||
|
5 | potenciaReal=[] | |||
|
6 | contador = 0 | |||
|
7 | # Callback cuando se establece la conexión con el broker MQTT | |||
|
8 | def on_connect(client, userdata, flags, rc): | |||
|
9 | if rc == 0: | |||
|
10 | print("Conexión exitosa con el broker") | |||
|
11 | # Suscribirse a un tópico después de la conexión exitosa | |||
|
12 | client.subscribe(subscribe_topic) | |||
|
13 | else: | |||
|
14 | print("Error de conexión. Código de retorno =", rc) | |||
|
15 | # Callback cuando se recibe un mensaje en el tópico suscrito | |||
|
16 | def on_message(client, userdata, msg): | |||
|
17 | global potenciaReal | |||
|
18 | global contador | |||
|
19 | mensaje = float(msg.payload.decode()) | |||
|
20 | potenciaReal.append(mensaje) | |||
|
21 | contador=contador+1 | |||
|
22 | if contador==20: | |||
|
23 | contador=0 | |||
|
24 | valor_promedio = sum(potenciaReal) / len(potenciaReal) | |||
|
25 | potenciaReal=[] | |||
|
26 | print(valor_promedio) | |||
|
27 | # Configurar el cliente MQTT | |||
|
28 | client = mqtt.Client() | |||
|
29 | # Configurar los callbacks | |||
|
30 | client.on_connect = on_connect | |||
|
31 | client.on_message = on_message | |||
|
32 | client.connect(broker_address, port=1883, keepalive=60) | |||
|
33 | # Mantener la ejecución del programa para recibir mensajes | |||
|
34 | client.loop_forever() |
@@ -5,7 +5,7 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; | |
@@ -30,13 +30,18 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; | |
@@ -44,10 +49,9 void loop() { | |||||
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; |
General Comments 0
You need to be logged in to leave comments.
Login now