miércoles, 23 de noviembre de 2016

Termostato de calefacción con Arduino Mega y 2.4'' TFT

Termostato básico para calefacción con arduino mega, tft 2.4'' y 3 sondas de temperatura por cable.
(Para la calibración de la temperatura he utilizado código de calibracion temperatura según Sketch de David Losada, basado en trabajos de Rafael Torrales y Antonio Moles.)  Supondo que sería más facil usar componentes dedicados, pero  teníalas sondas por casa y cuando me dí cuenta ya las estaba utilizando.  :)

Para el funcionamiento de la tft me he basado en el texto y codigo de esta página y las librerías Adafruit_TFTLCD, Adafruit_GFX y TouchScreen.
 En mi pantalla ftf los ejemplos aportados por estas librerías dieron algún problema de orientación, que pude solucionar facilmente cambiando el valor de tft.rotation()

El accionamiento es mediante optoacoplador, que deberá ser adecuado a la tensión e intensidad que vienen de la caldera. En mi caso han sido 18V y 7mA. Es necesario respetar la polaridad en las entradas del optoacoplador. 

Muestra una gráfica  con las temperaturas de las últimas 26 horas, dispone de  una alarma que suena si la temperatura del sensor nº2 baja de 1ºC (para la habitación donde está la caldera )

En esta página muestro la versión con temporizador.

Descargar  sketch











Aspecto general.






                                           Sondas de temperatura.






Menú ptrincipal.







                         Fijando la temperatura de disparo.





 Gráfica de temperaturas








Código:



/*  ===============================================================
Termostato Arduino Mega y tft 2.4''
    ==================================================================   */

//#include

// *** SPFD5408 change -- Begin
#include     // Core graphics library
#include // Hardware-specific library
#include
// *** SPFD5408 change -- End


#if defined(__SAM3X8E__)
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

//Puertas de lectura del TFT
#define YP A1
#define XM A2
#define YM 7
#define XP 6
 
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

 

#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
 
//Colores
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
 
  Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
 
//Calibracion temperatura según Sketch de David Losada, basado en trabajos de Rafael Torrales y Antonio Moles.

//Para ahorrar cálculos, lo vamos a calcular antes del programa
const float TPto1 = -12; //Temperatura en ºC punto 1
const float RPto1 = 69200; // Ohms en punto 1
const float TPto2 = 33.5; // Temp. punto 2
const float RPto2 = 7270; // Ohms en punto 2
const float resistor = 8120; //El valor en ohmnios de la resistencia del termistor a 25ºC
const float voltage = 4.93; // El voltaje real en el punto 5Vcc de tu placa Arduino
//Para ahorrar cálculos lo definimos como constante en esta parte del programa
const float K = 273.15; //Para pasar a grados Kelvin
const float e = 2.718281828459045; //Constante matemática
const float B = log(RPto2 / RPto1) / (1 / (TPto2 + K) - (1 / (TPto1 + K))); //Valor Beta de tu termistor
const float unodivr = 1 / (resistor * pow(e, (-B / 298.15))); //Con pow elevamos e al resultado

unsigned long inicio;// control del tiempo



   bool optoac=0;
  String texto="Encender";

float T[3] = {0}; //Declaramos la variable Temperatura
float tmedia[3] = {0}; //Variable para calcular media
int cnt; //temperatura media y contador

short tp1[320]; // almacen de temperaturas1 para grafico
short tp2[320]; // almacen de temperaturas2 para grafico
short tp3[320]; // almacen de temperaturas3 para grafico
int tp; // puntero almacet Tª
int tpp;

float termostato=21.0; // Temperatura del termostato a 21ºC

char enl;

void setup()   {
  // Iniciamos comunicacion serie con pantalla     
  Serial.begin(9600);

pinMode(45, OUTPUT); // Preparar salida audio pin 45
pinMode(38, OUTPUT); // Salida  optoacoplador
  //
  tft.reset();
  uint16_t identifier = tft.readID();
  Serial.print(F("LCD driver chip: "));
  Serial.println(identifier, HEX);
  tft.begin(identifier);
  tft.fillScreen(BLACK);
  pinMode(13, OUTPUT);
  
  inicio = millis();
 
}

#define MINPRESSURE 100
#define MAXPRESSURE 1000

void loop()
{

  // Selecciona la entrada para el termistor
 
  int sensorValue[3]; // Aqui almacenamos el valor de las sondas
  byte sonda; // sondas
   
  // Parte 1:  Leemos el puerto analógico 0 y convertimos el valor en voltios.
  sensorValue[0] = analogRead(A13); //Leemos analógico 0
  sensorValue[1] = analogRead(A14); //Leemos analógico 1
  sensorValue[2] = analogRead(A15); //Leemos analógico 2

  // medimos la temperatura 10 veces y la almacenamos en tmedia
  for ( cnt = 0; cnt < 10; cnt++)
  {
    sensorValue[0] = analogRead(A13); //Leemos analógico 0
    sensorValue[1] = analogRead(A14); //Leemos analógico 1
    sensorValue[2] = analogRead(A15); //Leemos analógico 2

    tmedia[0] = tmedia[0] + sensorValue[0];
    tmedia[1] = tmedia[1] + sensorValue[1];
    tmedia[2] = tmedia[2] + sensorValue[2];

  }

  for (sonda = 0; sonda < 3; sonda = sonda + 1) { // Bucle para las tres sondas

    // calculamos la media de las medidas
    tmedia[sonda] = tmedia[sonda] / 10;

    //Convertimos a voltios :)
    float v2 = (voltage * float(tmedia[sonda])) / 1024.0f;

    tmedia[sonda] = 0; // limpiamos la variable

    // Parte 2: Calcular la resistencia con el valor de los voltios mediante la ecuación del divisor de voltaje
    //voltage = 4.83
    //R2 = 10000
    //R1 = Thermistor resistance
    //V2= v2
    //so V2=(R2*V)/(R1+R2)
    //and r1=((r2*v)/v2)-r2 <--final br="">
    float r1a = (voltage * float(resistor)) / v2;
    float r1 = r1a - resistor;


    //Parte 3: Calcular la temperatura basandose en la ecuación Steinhart-Hart y la ecuación del valor Beta.
    // T=B/ln(r1/rinfinit)


    T[sonda] = int((10*((B / log(r1 * unodivr)) - 273.15))); T[sonda]=T[sonda]/10; //Convertimos a ºC y ya tenemos la temperatura


  } // Fin for sondas

  if ( millis() - inicio > 300000 ) { // ciclo muestreo datos cada 5 minutos (300000)
    inicio = millis();

      tp=tp+1;
      if (tp>=320){ tp=0;}

    for (sonda = 0; sonda < 3; sonda = sonda + 1) {
     
      // Almacenaje temperaturas
      if (sonda==0){
        tp1[tp]=T[sonda]*100;
      }
     
      if (sonda==1){
        tp2[tp]=T[sonda]*100;
      }
     
      if (sonda==2){
        tp3[tp]=T[sonda]*100;
      }
        }     
  } // Fin del if

        temp(); //Mostrar temperaturas en el tft

if (optoac==1){    // Encendido apagado calefacc´on. Comprobar termostato
  if (T[0]<=(termostato-0.5)){  // Activar   
  digitalWrite(38, HIGH);
  }
 
    if (T[0]>(termostato+0.2)){// Desactivar     
  digitalWrite(38, LOW);
   }
  }  // fin  if pulsacionn 


if (T[1]<1 -galer="" 1000="" 1500="" a="" alarma="" br="" de="" en="" helada="" nbsp="" t="" tone="">       
}  // FIN LOOP


  ////////        MOSTRAR MENU PRINCIPAL
void MenuP()
{
byte esquinax;
int ciclo;

//  digitalWrite(13, HIGH);
//  digitalWrite(13, LOW);
    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);
TSPoint p;

  //Texto botoes
  tft.fillScreen(BLACK);
   tft.setRotation(1);
  tft.setTextSize(3);
  tft.setCursor(90, 20);
  tft.setTextColor(YELLOW);
  tft.println("Temperatura");
  tft.setCursor(90, 80);
  tft.setTextColor(GREEN);
  tft.println("Grafica");
  tft.setCursor(90, 140);
  tft.setTextColor(CYAN);
  tft.println(texto);
  tft.setTextColor(WHITE);
  tft.setCursor(90, 200);
  tft.print("Termostato");
    tft.setRotation(0);

//  triangulos
  for (esquinax=25; esquinax<200 esquinax="esquinax+60){<br"> tft.fillTriangle(esquinax+7, 5, esquinax, 75, esquinax-7, 5, (RED));
}
 tft.fillTriangle(200, 5, 213, 5,206 ,75, (RED));

 //Menu principal

  for (ciclo=0; ciclo<10000 br="" ciclo="">      p = ts.getPoint();
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  {
    p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, tft.width(), 0));
    p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));

   tone (45, 400, 180);
    // PULSACIÓN
   
       if (p.y <70 br="" temperatura="" testar="">       {
        temp();
              }
             
       else if (p.y <140 amp="" p.y=""> 70)         //Testar grafica
       {        
         lecturaD();
                }
               
       else if (p.y < 210 & p.y >140)            // On/off 
       {
         enc();       
       }
      
       else if (p.y >210)        //Fija la temperatura del termostato
       {
       termost();
       ciclo=10000;
         }
  }
}// fin for ciclo
}


//                Mostrar temperatura en tft
void temp() {
  char temp[2]; 
  byte num;
 
    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);

     tft.fillScreen(BLACK);
   tft.setRotation(1);
  
 // Mostrar Tª Habitación
  tft.setTextSize(7);
  tft.setTextColor(WHITE);          
 //tft.fillRect(10, 0, 309,104, BLACK);
  tft.setCursor(20, 15);
   tft.print(T[0]);
  tft.println("C");
 
 tft.setTextSize(3);
  tft.setCursor(60,90);
  tft.setTextColor(WHITE);
  tft.println("Habitacion");


 // Mostrar Tª galeria
 tft.setTextSize(3);
  tft.setTextColor(YELLOW);
//  tft.fillRect(20, 160, 159,49, BLACK);
  tft.setCursor(10, 160);    

   tft.print(T[1]);         
  tft.println("C");

   tft.setTextSize(2);
  tft.setCursor(25, 200);
  tft.setTextColor(YELLOW);
  tft.println("Galeria");


 // Mostrar Tª exterior
 tft.setTextSize(3);
  tft.setTextColor(CYAN); 
  tft.setCursor(180, 160);      

   tft.print(T[2]);         
  tft.println("C");

    tft.setTextSize(2);
  tft.setCursor(190, 200);
  tft.setTextColor(CYAN);
  tft.println("Exterior");
  
waitOneTouch();

}


void lecturaD() {// Hacer grafica de temperaturas
   
short mx; //Maximo
short mn; //Minimo
float escala;

int x1;

    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);


  tft.setRotation(3);
   tft.fillScreen(BLACK);
       tft.setTextSize(2);
      
    mx=int(tp1[1]);      mn=int(tp1[1]); //Define máximos y mínimos

   for (tpp =0; tpp < 320; tpp = tpp+ 1) {          //Registro máximos y mínimos
    mx=max(mx,tp1[tpp]);    mn=min(mn,tp1[tpp]);
    mx=max(mx,tp2[tpp]);    mn=min(mn,tp2[tpp]);
    mx=max(mx,tp3[tpp]);    mn=min(mn,tp3[tpp]);
   }
   mx=mx+100;
if (mn>0){ mn=0; }
else {mn=mn-100;}

 //  mx=mx/100; mn=mn/100-1 ;
escala= 240/(float(mx)-float(mn)); // =240/(mx-mn)/100, alto/100

// dibujar escala
tft.setCursor(300,(0-mn)*(escala));
 tft.setTextSize(2);
  tft.setTextColor(RED);
  tft.println("0");
tft.drawFastHLine(0,((0-mn)*(escala)) , 319, RED); // Dibuja linea 0º

 for (x1=24; x1<= 288; x1 =x1+24) { tft.drawFastVLine(x1,0 , 239, GREEN);} // linea vertical cada 2H


 for (x1=-2500; x1<= 4500; x1 =x1+500) {                  // linea horizontal cada 5º
 if (x1!=0) {
   tft.setRotation(3);
  tft.drawFastHLine(0,((x1-mn)*(escala)), 319, BLUE);
 tft.setRotation(1);
 tft.setCursor(2,240-(x1-mn)*(escala)); // escribir graduación
  tft.setTextColor(BLUE);
  tft.println(x1/100);
 }
 }

 tft.setRotation(3);

   for (tpp =tp+1; tpp < 320; tpp = tpp+ 1) {  // representar 1ª parte de datos

        tft.fillCircle(319-(tpp-tp),(tp1[tpp]-mn)*(escala),1, WHITE);
                tft.fillCircle(319-(tpp-tp),(tp2[tpp]-mn)*(escala),1, CYAN);
                        tft.fillCircle(319-(tpp-tp),(tp3[tpp]-mn)*(escala),1, YELLOW);
}

    for (tpp =0; tpp < tp; tpp = tpp+ 1) {   // representar 2ª parte de datos
       
    tft.fillCircle((tp-tpp),(tp1[tpp]-mn)*(escala), 1, WHITE);
    tft.fillCircle((tp-tpp),(tp2[tpp]-mn)*(escala), 1, CYAN);
    tft.fillCircle((tp-tpp),(tp3[tpp]-mn)*(escala), 1, YELLOW);
}
delay(4000);
   waitOneTouch();
}


// Esperar pulsación

void waitOneTouch() {
int ciclo;
   TSPoint p;
  
       pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);

  for (ciclo=0; ciclo<4000 br="" ciclo="ciclo+1){  " pausa="">    p= ts
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE){ // Detectar Pulsacion pantalla
     tone (45, 400, 180);
     MenuP(); 
     ciclo=4000;  }
  }
}


// Boton Activar termostato/ desactivar

void enc()
{
      if (optoac==0){
      texto="Apagar";
      optoac=1;
    }else{
      texto="Encender";
      optoac=0;
      digitalWrite(38,LOW);
    }
    MenuP();
}


//  Menu Fijar temp. Termostato
void termost()
{
////  digitalWrite(13, HIGH);
//  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

   //Menu
  tft.fillScreen(BLACK);
   tft.setRotation(1);
  
  tft.setTextSize(3);
  tft.setCursor(50, 2);
  tft.setTextColor(WHITE);
  tft.println("Termostato:");
  tft.setTextSize(9);
  tft.setCursor(30, 40);
  tft.println(termostato);
 
  tft.setTextSize(5);
  tft.setCursor(110, 140);
  tft.setTextColor(RED);
  tft.println("Subir");
  tft.setTextColor(CYAN);
  tft.setCursor(110, 200);
  tft.print("Bajar");
    tft.setRotation(0);

//  flechas
 tft.fillTriangle(70,22, 100, 57, 70,92, (RED));
 tft.fillTriangle(35, 22, 5, 57, 35, 92, (CYAN));

   termost2();
}

 
void termost2()
{  //  Captura de pulsación para fijar termostato
  int ciclo;
   TSPoint p;
  
  for (ciclo=0; ciclo<4000 br="" ciclo="ciclo+1){  " pausa="">  Serial.println(ciclo);
   p = ts.getPoint();
 
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  {
    p.x = tft.width()-(map(p.x, TS_MINX, TS_MAXX, tft.width(), 0));
    p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
  
   tone (45, 400, 180);
  
      if (p.y <170 br="" de="" fija="" sale="" termostato="" y="">         {
         // delay(300);
           ciclo=4000;// volver al loop
                }
               
         else if (p.y <256 amp="" p.y=""> 170)  // Sube temp. termostato
         {
               if (termostato<28 termostato="termostato+0.2;}<br">               termost();
           ciclo=1000;
                  }
                 
         else if (p.y >256)    //Baja la temperatura del termostato
        
         {
           if (termostato>5) {termostato=termostato-0.2;}
           ciclo=1000;
           termost();
           }
    }
  }  // fin for ciclo
  tone (45, 400, 180) 

<--final br="">} 

<--final br="">// Fin del  sketch
<--final br="">//

No hay comentarios: