
#include <Wire.h>    //I2C Bus
#include <DS1631.h>
DS1631 Temp1(0);  // initialize DS1631 object, with bus address
// A bus address of 0 means pins A2,A1,A0 are all tied to 
// ground.ä 
// Thermoskanne besteht aus
//Pumpe pin 5~ 0 bei 130 max 255
int pumpepin = 5;
int pumpe =0;
int aktpumpe = 0;
//Relaissatz + Peltiersteuerung
int relaispin = 4;
boolean relais = false;  // true = heizen false = kühlen
boolean aktrelais = false;
int powerpin = 6;
int power = 1;          // Vorseinstellung für die Power
int aktpower=0;         
//Thermistor zum Schutz
int t1apin = 0;
int t1max = 45;     // maximale Temperatur des Systems
int t1 =0;
//DS1631 für die Umlauftemperatur
int tsoll = 0;
int tist =0;
// An Aus Schalter für alles
boolean onoff = false;
int vstatus = 0;      //Aus = 0 | An = 1 | Error = 2


int command = 0;

void setup() {
  pinMode(pumpepin,OUTPUT);
  pinMode(relaispin,OUTPUT);
  pinMode(powerpin,OUTPUT);
  
   
  Serial.begin(115200);
  Wire.begin(); // start up I2C bus
  delay(500);
  scan();
  int config = Temp1.readConfig();
  Temp1.writeConfig(13);  // Set to 12-bit, 1-shot mode
  config = Temp1.readConfig();  
  vstatus=1;
}

void loop(){
  while (Serial.available()>0){
    Serial.println("1)info 2)powerOn 3)powerOff 4)relaisOn 5)relaisOff 6)doit");
     command = Serial.parseInt();
     if (Serial.read() == '\n') {
        Serial.println(command);
        switch (command) {
          case 1 : info();              
          break;
          case 2 :aktpower = HIGH;
          digitalWrite(powerpin,aktpower);            
          break;
          case 3 :aktpower = LOW;
          digitalWrite(powerpin,aktpower);            
          break;
          case 4 :relais = HIGH;
          digitalWrite(relaispin,relais);            
          break;
          case 5 :relais = LOW;
          digitalWrite(relaispin,relais);            
          break;


        }
     }
  }    
}

void info(){
  t1 = int(Thermistor(analogRead(0)));
  tist = (int)Temp1.readTempOneShot();
  // put your main code here, to run repeatedly:
  Serial.print("termistor ");
  Serial.print(t1);
  Serial.print(" I2C Temp ");
  Serial.print(tist);
  Serial.print(" status ");
  Serial.println(vstatus);
}
void do_loop() {
  
  t1 = int(Thermistor(analogRead(0)));
  tist = (int)Temp1.readTempOneShot();
  if(t1>t1max){
    vstatus = 2; // zu heiß -> error
   } 
  switch (vstatus){
    case 0 : //aus
     aktpumpe = 0;
     aktpower = LOW;
     aktrelais = false;
     //displayInteger(tist);
    break;
    case 1:  //an
      aktpumpe = pumpe;
      aktrelais = relais;
      if (relais==true) { // heizen
        if( tsoll > tist){
          aktpower = HIGH;
        } else {
           aktpower = LOW;  
        }
      } else { // kühlen

        if (tsoll < tist){
          aktpower = HIGH;
        } else {
          aktpower = LOW;
        }   
      }  
     // displayInteger(aktpower);      
    break;
    case 2:  //error
      aktpower = LOW;
      aktrelais =0;
      //displayInteger(1111);
    break;   
  } 
  analogWrite(pumpepin, aktpumpe);
  digitalWrite(relaispin,aktrelais);
  digitalWrite(powerpin,aktpower);
 
  // put your main code here, to run repeatedly:
  Serial.print("termistor ");
  Serial.print(t1);
  Serial.print(" I2C Temp ");
  Serial.print(tist);
  Serial.print(" status ");
  Serial.println(vstatus);
  delay(1000);
  
}

void scan(){
Serial.println(" Scanning I2C Addresses");
uint8_t cnt=0;
for(uint8_t i=0;i<128;i++){
  Wire.beginTransmission(i);
  uint8_t ec=Wire.endTransmission(true);
  if(ec==0){
    if(i<16)Serial.print('0');
    Serial.print(i,HEX);
    cnt++;
  }
  else Serial.print("..");
  Serial.print(' ');
  if ((i&0x0f)==0x0f)Serial.println();
  }
Serial.print("Scan Completed, ");
Serial.print(cnt);
Serial.println(" I2C Devices found.");
}

double Thermistor(int RawADC) {
 double Temp;
 Temp = log(10000.0*((1024.0/RawADC-1)));
//         =log(10000.0/(1024.0/RawADC-1)) // for pull-up configuration
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
 return Temp;
}
