Code:
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
//Serial.print("Resolution: ");
//Serial.print(sensors.getResolution(deviceAddress));
//Serial.println();
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
//Serial.print("Device Address: ");
printAddress(deviceAddress);
//Serial.print(" ");
printTemperature(deviceAddress);
//Serial.println();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
//Serial.println("DONE");
Task2.check(ReadDS18B20,4000);
// print the device information
printData(bufferThermometer);
Buffer = DStemp;
printData(dhwhThermometer);
DhwH = DStemp;
printData(dhwlThermometer);
DhwL = DStemp;
printData(outsideThermometer);
Outside = DStemp;
printData(dsHPFlow);
HpFlow = DStemp;
printData(dsHPReturn);
HpRet = DStemp;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Buf");
lcd.setCursor(4, 0);
lcd.print(Buffer);
lcd.setCursor(11, 0);
lcd.print("OS");
lcd.setCursor(15, 0);
lcd.print(Outside);
lcd.setCursor(0, 1);
lcd.print("Dhw");
lcd.setCursor(4, 1);
lcd.print(DhwH);
lcd.setCursor(11, 1);
lcd.print(DhwL);
lcd.setCursor(0, 2);
//lcd.print(HpWattsOut);
lcd.print("SP");
lcd.setCursor(3, 2);
lcd.print(WCtarget);
lcd.setCursor(10, 2);
if (HpWattsIn > 500)
{
HpCOP = ((float)HpWattsOut / (float)HpWattsIn);
HpRunning = true;
}
else
{
HpCOP = 0;
HpRunning = false;
}
if (heatWater == false)
{
lcd.print("Heating");
}
else
{
lcd.print("H Water");
}
lcd.setCursor(0, 3);
lcd.print(HpWattsIn);
lcd.setCursor(7, 3);
lcd.print(HpWattsOut);
lcd.setCursor(14, 3);
lcd.print(HpCOP);
if (HpChAct == false && heatWater == false)
{
digitalWrite(R6, LOW);
}
else if (HpChAct == true || heatWater == true)
{
digitalWrite(R6, HIGH);
}
if (HpRunning == true)
{
HpWattsOut = (((HpFlow - HpRet) * 4184 * HpLPM) / 60);
}
else
{
HpWattsOut = 0;
}
//txEmon();
//Task1.check(txEmon,10000);
Task1.check(RTCevents,1000);
Task3.check(txEmon,10000);
Task4.check(readFlows,30000);
Task5.check(weathercomp,30000);
//Task6.check(ReadThermsCal,5000);
Task6.check(ReadTherms,5000);
Task7.check(FlowSW,4000);
//Task8.check(DumpFile,3000);
}
/////////////////////////End of main loop/////////////////////////////////////////
void txEmon()
{
mydata.rxOS = (Outside * 10);
mydata.rxDHWh = (DhwH * 10);
mydata.rxDHWl = (DhwL * 10);
mydata.rxBuffer = (Buffer * 10);
if (heatWater == false)
{
mydata.rxHeating = (((HpFlow - HpRet) * 4184 * HpLPM) / 60);
mydata.rxWater = 0;
}
else
{
mydata.rxHeating = 0;
mydata.rxWater = (((HpFlow - HpRet) * 4184 * HpLPM) / 60);
}
//send the data
ET.sendData();
}
void RTCevents()
{
tmElements_t tm;
int Hr;
int Mint;
RTC.read(tm);
Hr = int(tm.Hour);
Mint = int(tm.Minute);
if (Hr == 5 || Hr == 12 || Hr == 13 || Hr == 19 || (Hr == 20 && DhwH <= 40)) { //select DHW from 12 till 2
heatWater = true;
digitalWrite(R1, HIGH);
digitalWrite(R4W, LOW);
}
else
{
heatWater = false;
digitalWrite(R1, LOW);
}
if (LastHr != Hr)
{
LastHr = Hr;
Uptime++;
predictCorrection = ((osLastHr - Outside) * 0.5);
osLastHr = Outside;
LogSlow();
}
if (LastMint != Mint)
{
DailyWatts += HpWattsIn;
DailyKWh = (DailyWatts / 60);
if (HpRunning == true) HpMinsRun++ ;
if (Hr == 0 && Mint == 0)
{
HpMinsRunY = HpMinsRun;
HpMinsRun = 0;
DailyWatts = 0;
DailyKWh = 0;
}
LastMint = Mint;
LogQuick();
}
if (heatWater == false && Outside < 5 && HpChAct == false && lastMint != Mint)
{
if (Mint == 0 || Mint == 10 || Mint == 20 || Mint == 30 || Mint == 40 || Mint == 50)
{
digitalWrite(R6, LOW);
delay(2000);
digitalWrite(R6, HIGH);
lastMint = Mint;
}
}
}
void ReadDS18B20()
{
// print the device information
printData(bufferThermometer);
Buffer = DStemp;
printData(dhwhThermometer);
DhwH = DStemp;
printData(dhwlThermometer);
DhwL = DStemp;
printData(outsideThermometer);
Outside = DStemp;
printData(dsHPFlow);
HpFlow = DStemp;
printData(dsHPReturn);
HpRet = DStemp;
}
void FlowSW()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
interrupts(); //Enables interrupts
delay (500); //Wait .5 second
noInterrupts(); //Disable interrupts
if (NbTopsFan > LowFlow) {
digitalWrite(R2, LOW);
}
else {
digitalWrite(R2, HIGH);
}
double Irms = emon1.calcIrms(1480); // Calculate Irms only
HpWattsIn = (Irms*245.0); // Apparent power
//Serial.print(" ");
//Serial.println(Irms); // Irms
}
void readFlows()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
NbShowerFlow = 0; //Set NbTops to 0 ready for calculations
interrupts(); //Enables interrupts
delay (5000); //Wait 5 second
noInterrupts(); //Disable interrupts
HpLPM = (NbTopsFan / 13.75);
//ShowerLPM = (NbShowerFlow / 225);
ShowerLPM = NbShowerFlow;
}
void ReadThermsCal()
{
// Routine used for calibration only
int val = 0;
// val=(analogRead(A8)); // read ADC and convert it to Celsius
// HpFlow = val;
// val=(analogRead(A9)); // read ADC and convert it to Celsius
// HpRet = val;
val=(analogRead(A10)); // read ADC and convert it to Celsius
ChFlow = val;
val=(analogRead(A11)); // read ADC and convert it to Celsius
ChRet = val;
val=(analogRead(A12)); // read ADC and convert it to Celsius
ShowerRecIn = val;
val=(analogRead(A13)); // read ADC and convert it to Celsius
ShowerRecOut = val;
}
void ReadTherms()
{
int val = 0;
//val=(analogRead(A8)); // read ADC and convert it to Celsius
//HpFlow = multiMap(val, inA8, outA8, 8);
//HpFlow = (HpFlow / 100);
//val=(analogRead(A9)); // read ADC and convert it to Celsius
//HpRet = multiMap(val, inA9, outA9, 8);
//HpRet = (HpRet / 100);
val=(analogRead(A10)); // read ADC and convert it to Celsius
ChFlow = multiMap(val, inA10, outA10, 8);
ChFlow = (ChFlow / 100);
val=(analogRead(A11)); // read ADC and convert it to Celsius
ChRet = multiMap(val, inA11, outA11, 8);
ChRet = (ChRet / 100);
val=(analogRead(A12)); // read ADC and convert it to Celsius
ShowerRecIn = multiMap(val, inA12, outA12, 8);
ShowerRecIn = (ShowerRecIn / 100);
val=(analogRead(A13)); // read ADC and convert it to Celsius
ShowerRecOut = multiMap(val, inA13, outA13, 8);
ShowerRecOut = (ShowerRecOut / 100);
}
void DumpFile()
{
dumpFilePB = digitalRead(12);
if (dumpFilePB == HIGH) {
//Serial.begin(57600);
delay(500);
File dataFile = SD.open("quicklog.csv");
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening QUICKLOG.CSV");
}
Serial.end();
}
dumpFilePB = LOW;
}