shaan
7
#include <PZEM004Tv30.h>
#include <PID_v1.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
//Solid state relay on PIN 5 on the Arduino
#define RELAY_PIN 5
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=1500, Ki=.5, Kd=20;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
int WindowSize = 10000;
unsigned long windowStartTime;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
PZEM004Tv30 pzem(11, 12);// connection to Power sensor Tx-11 and Rx-12
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(115200);
Serial.println("Starting");
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
mlx.begin();
}
void loop() {
Input = (mlx.readObjectTempC());
Serial.println(Input);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if (millis() - windowStartTime > WindowSize)
{
//time to shift the Relay Window
windowStartTime += WindowSize;
}
if (Output < millis() - windowStartTime)
digitalWrite(RELAY_PIN, LOW);
else
digitalWrite(RELAY_PIN, HIGH);
Serial.println();
delay(0);
float voltage = pzem.voltage();
if(voltage != NAN){
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
} else {
Serial.println("Error reading voltage");
}
float current = pzem.current();
if(current != NAN){
Serial.print("Current: "); Serial.print(current); Serial.println("A");
} else {
Serial.println("Error reading current");
}
float power = pzem.power();
if(current != NAN){
Serial.print("Power: "); Serial.print(power); Serial.println("W");
} else {
Serial.println("Error reading power");
}
float energy = pzem.energy();
if(current != NAN){
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
} else {
Serial.println("Error reading energy");
}
Serial.println();
delay(500);
}