TempCTRL v2 firmware

From Open Source Ecology - Germany
Jump to: navigation, search

Hier die aktuelle Firmware-Version für TempCTRL V.2, namens "heizkreisrelais_esp_lua5". (Stand 30.01.2016)

Ganz interessant ist bei dem Arduino-LCD-keypad (ein Standard-Bauteil, das von verschiedenen Herstellern angeboten wird), dass 5 der Microtaster pin-sparend lediglich an einem ADC-Pin hängen. Die Unterscheidung der einzelnen Tasten kann dadurch rein softwaremässig anhand unterschiedlicher vom ADC gemessener Widerstandswerte erfolgen.

// include the library code:
#include <LiquidCrystal.h>


#include <Boards.h>
#include <OneWire.h>
#include <DallasTemperature.h>


// initialize the library with the numbers of the interface pins
// Standard-Connection:
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// LCD-Keypad-Connection:
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);   // select the pins used on the LCD panel


// define some values used by the panel and buttons
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int lcd_key     = 0;
int adc_key_in  = 0;


int key1State = 0;         // variable for reading the pushbutton status
int key2State = 0;         // variable for reading the pushbutton status
int key3State = 0;         // variable for reading the pushbutton status
int key4State = 0;         // variable for reading the pushbutton status
int key5State = 0;         // variable for reading the pushbutton status

// int grad = 75;  // default soll-Wert für Heizung
int grad = 27;  // zum testen mit Körpertemperatur
int hysterese = 3; // erlaubte Schwankungsbreite bzw. Abweichung nach oben oder unten

// int MAXGRAD = 124; // Maximale Temp vom DS18B20 Temperatursensor
// int MINGRAD = -55; // Minimale Temp vom DS18B20 Temperatursensor
int MAXGRAD = 100; // Temperatur soll unter 100 Grad bleiben
int MINGRAD = 0; // Temperatur soll über 0 Grad bleiben

int MAXHYS = 50; // Schwankungsbreite = 1/2 MAXGRAD
int MINHYS = 0;

// Relais ist activeLow
#define RELAY_ON 0
#define RELAY_OFF 1
const int relPin =  33;      // the number of the relay pin
int rs =  0;      // the current Relais-mode as integer: 
String relState = "0";
String str;

float U = 0;
float mytemp = 0;
float temps0[8];
float temps1[8];
int i = 0;
int count0 = 0;
int count1 = 0;

// Data wire is plugged into analog port A0 on the Arduino
#define BUS0 45
#define BUS1 33


#define TEMPERATURE_PRECISION 9

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire ow_bus0(BUS0);
OneWire ow_bus1(BUS1);


// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors0(&ow_bus0);
DallasTemperature sensors1(&ow_bus1);


// arrays to hold device addresses
uint8_t sensoradresses0[8][8];
uint8_t sensoradresses1[8][8];

// refreshrate / data-logging interval

unsigned long previousMillis = 0; // speichert wie viele Sekunden seit derletzten Änderung vergangen sind
unsigned long interval = 1000;    // serial datalogging refreshrate in milliseconds


void setup()
{ 
   digitalWrite(relPin, RELAY_OFF);  
   // initialize the Relay pin as an output:
  pinMode(relPin, OUTPUT);      

  
  lcd.begin(16, 2);                // start the LCD library
  lcd.setCursor(0,0);              // set cursor position at start
  lcd.print("ThermoRel. ESP");
  delay(1000);
 
  Serial.begin(9600);
  Serial.println("ThermoRelay ESP");
 
  Serial1.begin(9600);
  
  
  
  //check Bus 0
  sensors0.begin();
  count0 = sensors0.getDeviceCount();
  Serial.print("Bus0 Adresses found: ");
  Serial.println(count0);


  for(i=0; i<count0; i++)
  {
    if (!sensors0.getAddress(sensoradresses0[i], i)) Serial.println("Unable to find address for Device " + i); 
  
    // show the addresses we found on the bus
    // Serial.print("Device 0 Address: ");
    printAddress(sensoradresses0[i]);
    Serial.println();

    // set the resolution to 9 bit
    sensors0.setResolution(sensoradresses0[i], 9);
 
    // sensors.requestTemperatures(); // Send the command to get temperatures
  
  } // end for
  sensors0.setWaitForConversion(false);
 
   // Startzustand: Relais ist ausgeschaltet
  relState = "0"; 
  rs=0;
 
} // end of setup()



// function to print a device address
void printAddress(uint8_t deviceAddress[])
{
  for (uint8_t i = 0; i < 8; i++)
  {
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) Serial.print(" ");
  }
}



int read_LCD_buttons()                    //function for detection of pressed keypad button
{
 adc_key_in = analogRead(0);              // read the analog value from the sensor 
 if (adc_key_in > 1000) return btnNONE;   // We make this the 1st option for speed reasons since it will be the most likely result
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
 return btnNONE;  // when all others fail, return this...
}


 
void loop () 
{

  
  // Serial.println(count0);
  // printAddress(sensoradresses0[0]);
 
  sensors0.requestTemperatures(); // Send the command to get temperatures
  for(i=0; i<count0; i++)
  {
    temps0[i] = sensors0.getTempCByIndex(i);
  }

  // temps0[i] = 32.5;     // Testwert


 lcd_key = read_LCD_buttons();  // read the buttons function
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 { case btnRIGHT:
     { key1State = HIGH;
       break;
     }
   case btnLEFT:
     { key2State = HIGH;
       break;
     }
   case btnUP:
     { key3State = HIGH;
       break;
     }
   case btnDOWN:
     { key4State = HIGH;
       break;
     }
   case btnSELECT:
     { key5State = HIGH;
       break;
     }
     case btnNONE:
     { 
       key1State = LOW;
       key2State = LOW;
       key3State = LOW;
       key4State = LOW;
       key5State = LOW;
       break;
     }
 }
 delay(40);  //wait 40ms

  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  
  // Sollwert erhöhen
  if (key1State == HIGH) 
  { 
    if ( grad < MAXGRAD ) grad = grad + 1; 
    else grad = 0;
  } 
    // Sollwert verringern
  if (key2State == HIGH) 
  { 
    if ( grad > MINGRAD ) grad = grad - 1; 
    else grad = MAXGRAD;
  } 
  
  
  // Schwankungsbreite erhöhen
  if (key3State == HIGH) 
  { 
    if ( hysterese < MAXHYS ) hysterese = hysterese + 1; 
    else hysterese = 0;
  } 
   // Schwankungsbreite verringern
  if (key4State == HIGH) 
  { 
    if ( hysterese > MINHYS ) hysterese = hysterese - 1; 
    else hysterese = MAXHYS;
  } 


  // Relais per Taste umswitchen
  if (key5State == HIGH) 
  { 
    if (rs == 1) { relState = "0"; rs=0; }
    else { relState = "1"; rs=1; }
    
    Serial.print("Relais: ");
    Serial.println(relState);
  } // end if(key5State)
  


  // Relais automatisch je nach Temperatur umswitchen
  if((temps0[0] >= (grad+hysterese)) & (rs==1))
  {
    rs=0;
    relState = "0";
    Serial.println("Relais off");
    // delay(10);
    digitalWrite(relPin, RELAY_OFF);
  } 

  if((temps0[0] <= (grad-hysterese)) & (rs==0))
  {
    rs=1;
    relState = "1";
    Serial.println("Relais on");
    // delay(10);
    digitalWrite(relPin, HIGH);
    digitalWrite(relPin, RELAY_ON);
  } 


if (millis() - previousMillis > interval) 
{
    previousMillis = millis();   // aktuelle Zeit abspeichern
 
  
// Serial Monitor, zB. Arduino IDE
  Serial.println("          ");
  Serial.print("Ist: ");
  Serial.println(temps0[0]);
  Serial.print("Hys: ");
  Serial.println(hysterese);
  Serial.print("Soll: ");
  Serial.println(grad);
  Serial.print("Rel: ");
  Serial.println(relState);
  Serial.println(" ");


// Serial1 is connected to ESP  
  Serial1.print(":");
  Serial1.print(temps0[0]);
  Serial1.print(":");
  Serial1.print(grad);
  Serial1.print(":");
  Serial1.print(hysterese);
  Serial1.print(":");
  Serial1.print(relState);
  Serial1.println(":");

} // end if previousMillis
 
 
  lcd.setCursor(0,0);
  lcd.print("Ist:      ");
  lcd.setCursor(4,0);
  lcd.print(temps0[0]);

  lcd.setCursor(11, 0);
  lcd.print("Rel:");  // Relais Zustände: dauer-ein, dauer-aus, dem Temperatur-Programm folgend
  lcd.setCursor(15, 0);
  // Relais Zustände: dauer-ein, dauer-aus, dem Temperatur-Programm folgend
  lcd.print(relState);   
  
  
  
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print("Soll:    ");
  lcd.setCursor(5, 1);
  lcd.print(grad);
  lcd.setCursor(10, 1);
  lcd.print("+/-:  ");
  lcd.setCursor(14, 1);
  lcd.print(hysterese); // 1/2 Range

  delay(100);

    
    
} // end loop()