@@ -27,48 +27,27 void setup() { | |||||
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; |
@@ -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..."); | |||
|
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 | } | |||
8 |
|
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