Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <ArduinoJson.h>
- SoftwareSerial s(5,6);
- int pin = 13;
- float rads = 57.29577951; // 1 radian = approx 57 deg.
- float degree = 360;
- float frequency = 50;
- float nano = 1 * pow (10,-6);
- #include "EmonLib.h"
- EnergyMonitor emon1;
- float pf;
- float angle;
- float pf_max = 0;
- float angle_max = 0;
- int ctr;
- float powerFactor(void);
- float power(void);
- void setup()
- {
- pinMode(pin, INPUT);
- // Serial.begin(9600);
- s.begin(115200);
- emon1.voltage(2, 170, 1.7); // Voltage: input pin, calibration, phase_shift
- emon1.current(1, 49); // Current: input pin, calibration.
- }
- StaticJsonBuffer<1000> jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- void loop()
- {
- // float pf_data = powerFactor();
- // float power_data = power();
- //
- root["pf"]= powerFactor();
- root["power"] = power();
- if(s.available()>0)
- {
- root.printTo(s);
- }
- }
- float powerFactor(void)
- {
- for (ctr = 0; ctr <= 4; ctr++) // Perform 4 measurements then reset
- {
- // 1st line calculates the phase angle in degrees from differentiated time pulse
- // Function COS uses radians not Degree's hence conversion made by dividing angle / 57.2958
- angle = ((((pulseIn(pin, HIGH)) * nano)* degree)* frequency);
- // pf = cos(angle / rads);
- if (angle > angle_max) // Test if the angle is maximum angle
- {
- angle_max = angle; // If maximum record in variable "angle_max"
- pf_max = cos(angle_max / rads); // Calc PF from "angle_max"
- }
- }
- if (angle_max > 360) // If the calculation is higher than 360 do following...
- {
- angle_max = 0; // assign the 0 to "angle_max"
- pf_max = 1; // Assign the Unity PF to "pf_max"
- }
- if (angle_max == 0) // If the calculation is higher than 360 do following...
- {
- angle_max = 0; // assign the 0 to "angle_max"
- pf_max = 1; // Assign the Unity PF to "pf_max"
- }
- Serial.print(angle_max, 2); // Print the result
- Serial.print(",");
- Serial.println(pf_max, 2);
- angle = 0; // Reset variables for next test
- angle_max = 0;
- return pf_max;
- }
- float power(void)
- {
- emon1.calcVI(20, 2000);
- return emon1.realPower;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement