Advertisement
kevin2458

Lectura Energetica

Jul 24th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <Bridge.h>
  2.  
  3. #include <BridgeClient.h>
  4.  
  5. #include <BridgeServer.h>
  6.  
  7. #include <HttpClient.h>
  8.  
  9. #include <ArduinoJson.h>
  10.  
  11. BridgeServer _Server;
  12.  
  13. // ===================================================
  14. //  ** PowerCheck - DIVERTEKA (J.C.G.P.) **    v. 1.0
  15. // ===================================================
  16. //
  17. // -- variables and pins definition ------------------
  18. const int analogInPin = A0;        // analog input pin
  19. int retardo = 2 ;    // (tiempo (s.) entre visionados)
  20. float lectura,ff,pKW,iA,vV,vS,S_Ratio;
  21.  
  22. // -- initialize serial comm & parameters ------------
  23. void setup() {
  24.   Serial.begin(9600);
  25.  
  26.  
  27.   digitalWrite(13, LOW);
  28.  
  29.   Bridge.begin();
  30.  
  31.   digitalWrite(13, HIGH);
  32.  
  33.   _Server.listenOnLocalhost();
  34.  
  35.   _Server.begin();
  36.  
  37.  
  38.   S_Ratio = 36.5;      // Sensor/ratio (mV/mA ) : 36.5
  39.   vV = 110;            // valor de tension a computar
  40.   ff = 4.15; // freq. factor / (50Hz -> 5 / 60Hz -> 4.15)      
  41. }
  42.  
  43. // -- smooth read routine ----------------------------
  44. float smoothread(float fc){   // fc (factor corrector)
  45.   int ni = 35;          // n. de iteraciones => smooth
  46.   //  (ni) => rango 10 a 50 mejor promedio [smoothing]
  47.   float retorno = 0.0;
  48.   for (int x = 0; x< ni; x++){
  49.   do {                         // espero paso por cero  
  50.       delayMicroseconds(100);
  51.       } while (analogRead(0) != 0) ;
  52.       delay (ff);            // espera centro de ciclo
  53.       delay (10);            // estabilizacion CAD
  54.       retorno = retorno +(analogRead(0)*fc);
  55.     }
  56.   return retorno / ni;
  57. }
  58.  
  59. // -- main loop --------------------------------------
  60. void loop() {
  61.   lectura = smoothread (1) / 1.41;    // lectura (rms)  
  62.   vS = (lectura * 0.0048);          // valor de C.A.D.
  63.   iA = (lectura * S_Ratio)/1000;     // Intensidad (A)
  64.   pKW = (vV * iA);               // Potencia (kW)
  65.  
  66.   Serial.print("\n" );
  67.   Serial.print("\n================================" );
  68.   Serial.print("\n *** PowerCheck - DIVERTEKA *** " );
  69.   Serial.print("\n================================\n" );
  70.   Serial.print("\n- Tension predefinida  [V] --> " );
  71.   Serial.print(vV,0);    
  72.   Serial.print("\n- Lectura del sensor   [V] --> " );
  73.   Serial.print(vS,3);    
  74.   Serial.print("\n- Intensidad calculada [A] --> " );
  75.   Serial.print(iA,3);  
  76.   Serial.print("\n- Potencia calculada  [kW] --> " );
  77.   Serial.print(pKW,3);  
  78.   Serial.print("\n-------------------------------\n" );
  79.   delay(retardo * 1000);
  80.  
  81.   BridgeClient client = _Server.accept();
  82.  
  83.   if (client) {
  84.     StaticJsonBuffer<200> jsonBuffer;
  85.  
  86.     JsonObject& json = jsonBuffer.createObject();
  87.  
  88.     json["vS"] = vV;
  89.  
  90.     json["iA"] = validateValue(iA).toFloat();
  91.  
  92.     json["pKW"] = validateValue(pKW).toFloat();
  93.  
  94.     json["pKW"] = validateValue(pKW).toFloat();
  95.  
  96.     json["phKW"] = validateValue(pKW).toFloat() * 60;
  97.    
  98.     json.prettyPrintTo(client);
  99.  
  100.     client.stop();
  101.   }
  102. }
  103.  
  104. String validateValue(float value) {
  105.   return String(abs(value), 2);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement