@@ -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 |
|
||||
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 |
|
|
48 | Serial.println(Pmax); | |
69 |
|
|
49 | } | |
70 |
|
|
50 | } | |
71 | } */ |
|
|||
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,80 +1,83 | |||||
1 | #include <WiFi.h> |
|
1 | #include <WiFi.h> | |
2 | #include <PubSubClient.h> |
|
2 | #include <PubSubClient.h> | |
3 |
|
3 | |||
4 |
const char *ssid = " |
|
4 | const char *ssid = "HUAWEI P smart"; | |
5 | const char *password = "12345678"; |
|
5 | const char *password = "12345678"; | |
6 |
const char *mqtt_server = "192.168. |
|
6 | const char *mqtt_server = "192.168.43.149"; | |
7 | const int mqtt_port = 1883; |
|
7 | const int mqtt_port = 1883; | |
8 | const char *mqtt_client_id = "ESP32_Client"; |
|
8 | const char *mqtt_client_id = "ESP32_Client"; | |
9 | const char *subscribe_topic = "tesis/potenciaNominal"; |
|
9 | const char *subscribe_topic = "tesis/potenciaNominal"; | |
10 | const char *publish_topic = "tesis/potencia"; |
|
10 | const char *publish_topic = "tesis/potencia"; | |
|
11 | const char *publish_topic_voltage = "tesis/ReferenceVoltage"; | |||
11 |
|
12 | |||
12 | WiFiClient espClient; |
|
13 | WiFiClient espClient; | |
13 | PubSubClient client(espClient); |
|
14 | PubSubClient client(espClient); | |
14 |
|
15 | float Vpk=0,Vout=0; | ||
15 | void setup_wifi() { |
|
16 | void setup_wifi() { | |
16 | delay(10); |
|
17 | delay(10); | |
17 | Serial.println(); |
|
18 | Serial.println(); | |
18 | Serial.print("Conectando a "); |
|
19 | Serial.print("Conectando a "); | |
19 | Serial.println(ssid); |
|
20 | Serial.println(ssid); | |
20 | WiFi.begin(ssid, password); |
|
21 | WiFi.begin(ssid, password); | |
21 | while (WiFi.status() != WL_CONNECTED) { |
|
22 | while (WiFi.status() != WL_CONNECTED) { | |
22 | delay(500); |
|
23 | delay(500); | |
23 | Serial.print("."); |
|
24 | Serial.print("."); | |
24 | } |
|
25 | } | |
25 | Serial.println(""); |
|
26 | Serial.println(""); | |
26 | Serial.println("Conectado a la red WiFi"); |
|
27 | Serial.println("Conectado a la red WiFi"); | |
27 | Serial.println("Dirección IP: "); |
|
28 | Serial.println("Dirección IP: "); | |
28 | Serial.println(WiFi.localIP()); |
|
29 | Serial.println(WiFi.localIP()); | |
29 | } |
|
30 | } | |
30 |
|
31 | |||
31 | void callback(char *topic, byte *payload, unsigned int length) { |
|
32 | void callback(char *topic, byte *payload, unsigned int length) { | |
32 | Serial.print("Mensaje recibido en el tópico: "); |
|
33 | Serial.print("Mensaje recibido en el tópico: "); | |
33 | Serial.println(topic); |
|
34 | Serial.println(topic); | |
34 | // Convierte el payload a una cadena de caracteres |
|
35 | // Convierte el payload a una cadena de caracteres | |
35 | char receivedValue[length + 1]; |
|
36 | char receivedValue[length + 1]; | |
36 | strncpy(receivedValue, (char *)payload, length); |
|
37 | strncpy(receivedValue, (char *)payload, length); | |
37 | receivedValue[length] = '\0'; |
|
38 | receivedValue[length] = '\0'; | |
38 | // Convierte la cadena a un número (en este caso, asume que es un float) |
|
39 | // Convierte la cadena a un número (en este caso, asume que es un float) | |
39 | float potenciaNominal = atof(receivedValue); |
|
40 | float potenciaNominal = atof(receivedValue); | |
40 | // Realiza cálculos basados en la potencia nominal recibida (sustituye con tu lógica) |
|
41 | // Realiza cálculos basados en la potencia nominal recibida (sustituye con tu lógica) | |
41 | float voltajeCalculado = potenciaNominal * 1.2; |
|
42 | Vpk= sqrt(10*potenciaNominal)/5; | |
|
43 | Vout=175.19*Vpk-101; | |||
|
44 | ||||
42 | // Publica el resultado en el tópico de voltaje |
|
45 | // Publica el resultado en el tópico de voltaje | |
43 | char result[10]; |
|
46 | char result[10]; | |
44 |
snprintf(result, sizeof(result), "%.2f", |
|
47 | snprintf(result, sizeof(result), "%.2f", Vout); | |
45 | client.publish(publish_topic, result); |
|
48 | client.publish(publish_topic_voltage, result); | |
46 | } |
|
49 | } | |
47 |
|
50 | |||
48 | void reconnect() { |
|
51 | void reconnect() { | |
49 | while (!client.connected()) { |
|
52 | while (!client.connected()) { | |
50 | Serial.print("Intentando conexión MQTT..."); |
|
53 | Serial.print("Intentando conexión MQTT..."); | |
51 |
|
54 | |||
52 | if (client.connect(mqtt_client_id)) { |
|
55 | if (client.connect(mqtt_client_id)) { | |
53 | Serial.println("Conectado al servidor MQTT"); |
|
56 | Serial.println("Conectado al servidor MQTT"); | |
54 | client.subscribe(subscribe_topic); |
|
57 | client.subscribe(subscribe_topic); | |
55 | } else { |
|
58 | } else { | |
56 | Serial.print("Falló, rc="); |
|
59 | Serial.print("Falló, rc="); | |
57 | Serial.print(client.state()); |
|
60 | Serial.print(client.state()); | |
58 | Serial.println(" Intentando de nuevo en 5 segundos"); |
|
61 | Serial.println(" Intentando de nuevo en 5 segundos"); | |
59 | delay(5000); |
|
62 | delay(5000); | |
60 | } |
|
63 | } | |
61 | } |
|
64 | } | |
62 | } |
|
65 | } | |
63 |
|
66 | |||
64 | void setup() { |
|
67 | void setup() { | |
65 | Serial.begin(115200); |
|
68 | Serial.begin(115200); | |
66 | setup_wifi(); |
|
69 | setup_wifi(); | |
67 | client.setServer(mqtt_server, mqtt_port); |
|
70 | client.setServer(mqtt_server, mqtt_port); | |
68 | client.setCallback(callback); |
|
71 | client.setCallback(callback); | |
69 | } |
|
72 | } | |
70 |
|
73 | |||
71 | void loop() { |
|
74 | void loop() { | |
72 | if (!client.connected()) { |
|
75 | if (!client.connected()) { | |
73 | reconnect(); |
|
76 | reconnect(); | |
74 | } |
|
77 | } | |
75 | client.loop(); |
|
78 | client.loop(); | |
76 | char str[16]; |
|
79 | char str[16]; | |
77 | sprintf(str, "%u", random(100)); |
|
80 | sprintf(str, "%u", random(100)); | |
78 | client.publish(publish_topic, str); |
|
81 | client.publish(publish_topic, str); | |
79 | delay(500); |
|
82 | delay(500); | |
80 | } |
|
83 | } |
General Comments 0
You need to be logged in to leave comments.
Login now