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