@@ -1,87 +1,66 | |||||
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=1;//Un valor de 1 indica que se debe ingresar por monitor serial |
|
8 | bool serialData=1;//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 | total=0; |
|
|||
31 | //analogValue = analogRead(pinADC); |
|
|||
32 | /* |
|
|||
33 | for (int i = 0; i < numSamples; i++) { |
|
|||
34 | total += analogRead(pinADC); |
|
|||
35 | }*/ |
|
|||
36 | //int averageValue = total / numSamples; |
|
|||
37 | int averageValue=analogRead(pinADC); |
|
30 | int averageValue=analogRead(pinADC); | |
38 | Vout = 0.8291*averageValue+90.27;//Ajuste realizado para el ADC |
|
31 | Vout = 0.8291*averageValue+90.27;//Ajuste realizado para el ADC | |
39 | //Serial.println(Vout); |
|
|||
40 | 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 | |
41 | //dif=abs(Plinea-Plcal)*100/Plinea;//Hallamos la diferencia porcentual |
|
33 | //dif=abs(Plinea-Plcal)*100/Plinea;//Hallamos la diferencia porcentual | |
42 |
if (Plcal> |
|
34 | if (Plcal>4){//valores por debajo de este umbral serán leidos como tierra | |
43 | Parray[contador]=Plcal; |
|
35 | Parray[contador]=Plcal; | |
44 | contador =contador+1; |
|
36 | contador =contador+1; | |
45 | } |
|
37 | } | |
46 | //cuando llenamos los valores hacemos la |
|
38 | //cuando llenamos los valores hacemos la | |
47 |
if (contador |
|
39 | if (contador>(SIZE-1)){ | |
48 | p=&Parray[0]; |
|
40 | p=&Parray[0]; | |
49 | Pmax=find_maximun(p); |
|
41 | Pmax=find_maximun(p); | |
50 | contador=0; |
|
42 | contador=0; | |
51 | dif=abs(Plinea-Pmax)*100/Plinea; |
|
43 | dif=abs(Plinea-Pmax)*100/Plinea; | |
52 | difPlow=abs(Plinea-Pmax); |
|
44 | difPlow=abs(Plinea-Pmax); | |
53 | if(dif>10 && difPlow>7) Serial.println(Pmax); |
|
45 | Serial.print("Valor normal: "); Serial.println(Pmax); | |
54 | } |
|
46 | if(dif>10 && difPlow>9) { | |
55 |
|
47 | Serial.print("Nivel anómalo: "); | ||
56 |
|
48 | Serial.println(Pmax); | ||
57 | /* |
|
|||
58 | if(dif>10 && Vout>120 ){ |
|
|||
59 | Parray[contador]=Plcal; |
|
|||
60 | contador =contador+1; |
|
|||
61 | if(contador==SIZE){ |
|
|||
62 | p=&Parray[0]; |
|
|||
63 | Pmax=find_maximun(p); |
|
|||
64 | contador=0; |
|
|||
65 | difPlow=abs(Plinea-Pmax); |
|
|||
66 | if (difPlow>8) { |
|
|||
67 | Serial.print("Potencia anomala en el transmisor: "); |
|
|||
68 | Serial.println(Pmax); |
|
|||
69 | } |
|
|||
70 | } |
|
49 | } | |
71 |
|
|
50 | } | |
72 | } |
|
51 | } | |
73 | float ecuacionLineal(float Vpk){ |
|
52 | float ecuacionLineal(float Vpk){ | |
74 | float m=175.19,b=-101; |
|
53 | float m=175.19,b=-101; | |
75 | return Vout=m*Vpk+b; |
|
54 | return Vout=m*Vpk+b; | |
76 | } |
|
55 | } | |
77 | float find_maximun(float *p){ |
|
56 | float find_maximun(float *p){ | |
78 | float maxi=*p; |
|
57 | float maxi=*p; | |
79 | float *q; |
|
58 | float *q; | |
80 | q=p; |
|
59 | q=p; | |
81 | for(int i=0;i<SIZE;i++){ |
|
60 | for(int i=0;i<SIZE;i++){ | |
82 | if(maxi<*(p+i)) { |
|
61 | if(maxi<*(p+i)) { | |
83 | maxi=*(p+i); |
|
62 | maxi=*(p+i); | |
84 | } |
|
63 | } | |
85 | } |
|
64 | } | |
86 | return maxi; |
|
65 | return maxi; | |
87 | } No newline at end of file |
|
66 | } |
@@ -1,9 +1,83 | |||||
1 | void setup() { |
|
1 | #include <WiFi.h> | |
2 | // put your setup code here, to run once: |
|
2 | #include <PubSubClient.h> | |
|
3 | ||||
|
4 | const char *ssid = "HUAWEI P smart"; | |||
|
5 | const char *password = "12345678"; | |||
|
6 | const char *mqtt_server = "192.168.43.149"; | |||
|
7 | const int mqtt_port = 1883; | |||
|
8 | const char *mqtt_client_id = "ESP32_Client"; | |||
|
9 | const char *subscribe_topic = "tesis/potenciaNominal"; | |||
|
10 | const char *publish_topic = "tesis/potencia"; | |||
|
11 | const char *publish_topic_voltage = "tesis/ReferenceVoltage"; | |||
3 |
|
12 | |||
|
13 | WiFiClient espClient; | |||
|
14 | PubSubClient client(espClient); | |||
|
15 | float Vpk=0,Vout=0; | |||
|
16 | void setup_wifi() { | |||
|
17 | delay(10); | |||
|
18 | Serial.println(); | |||
|
19 | Serial.print("Conectando a "); | |||
|
20 | Serial.println(ssid); | |||
|
21 | WiFi.begin(ssid, password); | |||
|
22 | while (WiFi.status() != WL_CONNECTED) { | |||
|
23 | delay(500); | |||
|
24 | Serial.print("."); | |||
|
25 | } | |||
|
26 | Serial.println(""); | |||
|
27 | Serial.println("Conectado a la red WiFi"); | |||
|
28 | Serial.println("Dirección IP: "); | |||
|
29 | Serial.println(WiFi.localIP()); | |||
4 | } |
|
30 | } | |
5 |
|
31 | |||
6 | void loop() { |
|
32 | void callback(char *topic, byte *payload, unsigned int length) { | |
7 | // put your main code here, to run repeatedly: |
|
33 | Serial.print("Mensaje recibido en el tópico: "); | |
|
34 | Serial.println(topic); | |||
|
35 | // Convierte el payload a una cadena de caracteres | |||
|
36 | char receivedValue[length + 1]; | |||
|
37 | strncpy(receivedValue, (char *)payload, length); | |||
|
38 | receivedValue[length] = '\0'; | |||
|
39 | // Convierte la cadena a un número (en este caso, asume que es un float) | |||
|
40 | float potenciaNominal = atof(receivedValue); | |||
|
41 | // Realiza cálculos basados en la potencia nominal recibida (sustituye con tu lógica) | |||
|
42 | Vpk= sqrt(10*potenciaNominal)/5; | |||
|
43 | Vout=175.19*Vpk-101; | |||
|
44 | ||||
|
45 | // Publica el resultado en el tópico de voltaje | |||
|
46 | char result[10]; | |||
|
47 | snprintf(result, sizeof(result), "%.2f", Vout); | |||
|
48 | client.publish(publish_topic_voltage, result); | |||
|
49 | } | |||
|
50 | ||||
|
51 | void reconnect() { | |||
|
52 | while (!client.connected()) { | |||
|
53 | Serial.print("Intentando conexión MQTT..."); | |||
8 |
|
54 | |||
|
55 | if (client.connect(mqtt_client_id)) { | |||
|
56 | Serial.println("Conectado al servidor MQTT"); | |||
|
57 | client.subscribe(subscribe_topic); | |||
|
58 | } else { | |||
|
59 | Serial.print("Falló, rc="); | |||
|
60 | Serial.print(client.state()); | |||
|
61 | Serial.println(" Intentando de nuevo en 5 segundos"); | |||
|
62 | delay(5000); | |||
|
63 | } | |||
|
64 | } | |||
|
65 | } | |||
|
66 | ||||
|
67 | void setup() { | |||
|
68 | Serial.begin(115200); | |||
|
69 | setup_wifi(); | |||
|
70 | client.setServer(mqtt_server, mqtt_port); | |||
|
71 | client.setCallback(callback); | |||
|
72 | } | |||
|
73 | ||||
|
74 | void loop() { | |||
|
75 | if (!client.connected()) { | |||
|
76 | reconnect(); | |||
|
77 | } | |||
|
78 | client.loop(); | |||
|
79 | char str[16]; | |||
|
80 | sprintf(str, "%u", random(100)); | |||
|
81 | client.publish(publish_topic, str); | |||
|
82 | delay(500); | |||
9 | } |
|
83 | } |
General Comments 0
You need to be logged in to leave comments.
Login now