##// END OF EJS Templates
Publish con Python...
JesusTapia-dev -
r13:131c86444edb
parent child
Show More
@@ -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()
@@ -1,66 +1,70
1 1 #include <math.h>
2 2 #define pinADC 26
3 3 float find_maximun(float *p);
4 4 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=1;//Un valor de 1 indica que se debe ingresar por monitor serial
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;
12 12 int contador=0,SIZE=40;
13 13 float *p, Parray[40];
14 14 void setup() {
15 15 Serial.begin(115200);
16 16 analogSetAttenuation(ADC_11db);
17 17 if(serialData){
18 18 Serial.println("Ingrese la potencia en la linea:");
19 19 while (!Serial.available()); // Wait for input
20 20 String potLinChar = Serial.readStringUntil('\n');
21 21 Plinea=atof(potLinChar.c_str());
22 22 }
23 23 Vpk=sqrt(10*Plinea)/5;
24 24 VoutRef=ecuacionLineal(Vpk);
25 25 Serial.print("Voltaje de salida de referencia: ");
26 26 Serial.print(VoutRef);
27 27 Serial.print("Voltaje pkpk:"); Serial.print(" "); Serial.println(Vpk);
28 28 }
29 29 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;
43 48 dif=abs(Plinea-Pmax)*100/Plinea;
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;
54 58 return Vout=m*Vpk+b;
55 59 }
56 60 float find_maximun(float *p){
57 61 float maxi=*p;
58 62 float *q;
59 63 q=p;
60 64 for(int i=0;i<SIZE;i++){
61 65 if(maxi<*(p+i)) {
62 66 maxi=*(p+i);
63 67 }
64 68 }
65 69 return maxi;
66 70 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now