Advertisement
Guest User

arduino

a guest
Jun 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <ArduinoJson.h>
  3. SoftwareSerial s(5,6);
  4.  
  5. int pin = 13;
  6. float rads = 57.29577951; // 1 radian = approx 57 deg.
  7. float degree = 360;
  8. float frequency = 50;
  9. float nano = 1 * pow (10,-6);
  10. #include "EmonLib.h"            
  11. EnergyMonitor emon1;            
  12. float pf;
  13. float angle;
  14. float pf_max = 0;
  15. float angle_max = 0;
  16. int ctr;
  17. float powerFactor(void);
  18. float power(void);
  19. void setup()
  20. {
  21.   pinMode(pin, INPUT);
  22. //  Serial.begin(9600);
  23.  
  24.   s.begin(115200);
  25.  
  26.   emon1.voltage(2, 170, 1.7);  // Voltage: input pin, calibration, phase_shift
  27.   emon1.current(1, 49);       // Current: input pin, calibration.
  28. }
  29. StaticJsonBuffer<1000> jsonBuffer;
  30. JsonObject& root = jsonBuffer.createObject();
  31. void loop()
  32. {
  33. //  float pf_data = powerFactor();
  34. //  float power_data = power();
  35. //
  36.  
  37.   root["pf"]= powerFactor();
  38.   root["power"] = power();
  39.  
  40.   if(s.available()>0)
  41.   {
  42.    root.printTo(s);
  43.   }
  44.  
  45.  
  46. }
  47.  
  48.  
  49. float powerFactor(void)
  50. {
  51.   for (ctr = 0; ctr <= 4; ctr++) // Perform 4 measurements then reset
  52.   {
  53.   // 1st line calculates the phase angle in degrees from differentiated time pulse
  54.   // Function COS uses radians not Degree's hence conversion made by dividing angle / 57.2958
  55.    angle = ((((pulseIn(pin, HIGH)) * nano)* degree)* frequency);
  56.   // pf = cos(angle / rads);
  57.  
  58.    if (angle > angle_max) // Test if the angle is maximum angle
  59.      {
  60.       angle_max = angle; // If maximum record in variable "angle_max"
  61.       pf_max = cos(angle_max / rads); // Calc PF from "angle_max"
  62.      }
  63.    }
  64.    if (angle_max > 360) // If the calculation is higher than 360 do following...
  65.    {
  66.     angle_max = 0; // assign the 0 to "angle_max"
  67.     pf_max = 1; // Assign the Unity PF to "pf_max"
  68.    }
  69.    if (angle_max == 0) // If the calculation is higher than 360 do following...
  70.    {
  71.     angle_max = 0; // assign the 0 to "angle_max"
  72.     pf_max = 1; // Assign the Unity PF to "pf_max"
  73.    }
  74.    Serial.print(angle_max, 2); // Print the result
  75.    Serial.print(",");
  76.    Serial.println(pf_max, 2);
  77.    angle = 0; // Reset variables for next test
  78.    angle_max = 0;
  79.    return pf_max;
  80. }
  81.  
  82.  
  83. float power(void)
  84. {
  85.  
  86.     emon1.calcVI(20, 2000);
  87.  
  88.   return emon1.realPower;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement