greenhouse.ino 9.87 KB
Newer Older
1
#include <MAX6675.h>
João Lino's avatar
João Lino committed
2 3 4 5
#include <dht.h>

dht DHT;

6 7
//#define __debug
#define __info
João Lino's avatar
João Lino committed
8 9 10 11 12 13 14 15 16 17 18 19 20

#define PROGRAM_VERSION "0.4.0"

#define DHT11_PIN 2
#define RelayPin 3
#define LEDCount 6
#define LED1Pin 4
#define LED2Pin 5
#define LED3Pin 6
#define LED4Pin 7
#define LED5Pin 8
#define LED6Pin 9
#define ButtonPin 13
21
#define TEMPERATURE_SAMPLES 10
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#define TEMPERATURE_SAMPLES_TC 30
#define MINIMUM_TIME_OFF 480000   // 8 min
#define TC_TEMP_OFFSET 0.0


// ##############
int thermoDO = A2;
int thermoCS = A1;
int thermoCLK = A0;
int thermoUnits = 1;
MAX6675 thermocouple(thermoCS, thermoDO, thermoCLK, thermoUnits);
//MAX6675 thermocouple(thermoCS,thermoDO,thermoCLK,units);
double temperaturaQueAchavaTC;
double temperaturaMediaTC[TEMPERATURE_SAMPLES_TC];
int temperaturaMediaIndexTC;
// ##############
João Lino's avatar
João Lino committed
38 39 40 41 42 43

int leds[LEDCount];
double Setpoint;
int SetpointMode;
boolean relayState;
double temperaturaQueAchava;
44 45 46 47
double temperaturaMedia[TEMPERATURE_SAMPLES];
int temperaturaMediaIndex;
boolean tempSensorOK;
//unsigned long elapsed;
João Lino's avatar
João Lino committed
48

49 50 51 52 53
unsigned long currentTime;
unsigned long tcReadTime;
unsigned long dhtReadTime;
unsigned long lastTurnOff;

João Lino's avatar
João Lino committed
54 55 56
void setup()
{
  Serial.begin(115200);
57
  Serial.println("Greenhouse - for beer fermentation");
João Lino's avatar
João Lino committed
58 59 60 61 62
  Serial.print("PROGRAM VERSION: ");
  Serial.println(PROGRAM_VERSION);
  Serial.print("\tDHT LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
63
  Serial.println("Temp Sensor,\tSensor Status,\temperaturaAlvo (C),\temperaturaQueDiz (C),\ttempetaturaQueEuAcho (C),\tRelay State (ON\\OFF)");
64 65 66 67
  
  // ##############
  temperaturaQueAchavaTC = 200.0;
  // ##############
João Lino's avatar
João Lino committed
68 69 70 71 72 73 74

  leds[0] = LED1Pin;
  leds[1] = LED2Pin;
  leds[2] = LED3Pin;
  leds[3] = LED4Pin;
  leds[4] = LED5Pin;
  leds[5] = LED6Pin;
75 76
  Setpoint = 0.0;
  SetpointMode = 0;
João Lino's avatar
João Lino committed
77
  temperaturaQueAchava = 200.0;
78 79 80 81 82 83
  
  // Read Timmers
  currentTime = millis();
  tcReadTime = millis();
  dhtReadTime = millis();
  lastTurnOff = 0;
João Lino's avatar
João Lino committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  // Relay
  relayState = false;
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, HIGH);

  // LEDs
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  pinMode(LED3Pin, OUTPUT);
  pinMode(LED4Pin, OUTPUT);
  pinMode(LED5Pin, OUTPUT);
  pinMode(LED6Pin, OUTPUT);
  digitalWrite(LED1Pin, LOW);
  digitalWrite(LED2Pin, LOW);
  digitalWrite(LED3Pin, LOW);
  digitalWrite(LED4Pin, LOW);
  digitalWrite(LED5Pin, LOW);
  digitalWrite(LED6Pin, LOW);

  // Button
  pinMode(ButtonPin, INPUT);
106 107 108 109

  for( temperaturaMediaIndex = 0; temperaturaMediaIndex < TEMPERATURE_SAMPLES; temperaturaMediaIndex++ ) {
    temperaturaMedia[temperaturaMediaIndex] = Setpoint;
  }
110 111 112
  for( temperaturaMediaIndexTC = 0; temperaturaMediaIndexTC < TEMPERATURE_SAMPLES_TC; temperaturaMediaIndexTC++ ) {
    temperaturaMediaTC[temperaturaMediaIndexTC] = Setpoint;
  }
113 114 115 116 117

  setLEDs(SetpointMode);

  tempSensorOK = false;
  //elapsed = millis();
João Lino's avatar
João Lino committed
118 119 120 121
}

void loop()
{
122
  processBtnPress();
123

124 125 126 127 128 129 130 131
  double temperaturaQueDizTC = 0;
  currentTime = millis();
  if (currentTime > (tcReadTime + 50)) {
    tcReadTime = millis();
    temperaturaQueDizTC = thermocouple.read_temp() + TC_TEMP_OFFSET;
  }
  
  if(temperaturaQueDizTC != 0) {
João Lino's avatar
João Lino committed
132

133 134
    if (temperaturaQueAchavaTC == 200.0) {
      temperaturaQueAchavaTC = temperaturaQueDizTC;
João Lino's avatar
João Lino committed
135

136 137 138 139 140 141 142 143 144 145 146
      if(millis() < 1000) {
        for( temperaturaMediaIndexTC = 0; temperaturaMediaIndexTC < TEMPERATURE_SAMPLES_TC; temperaturaMediaIndexTC++ ) {
          temperaturaMediaTC[temperaturaMediaIndexTC] = temperaturaQueAchavaTC;
        }
      }
    }
    
    // Calcular a temperatura de processamento
    temperaturaMediaIndexTC++;
    if ( temperaturaMediaIndexTC >= TEMPERATURE_SAMPLES_TC ) {
      temperaturaMediaIndexTC = 0;
João Lino's avatar
João Lino committed
147 148
    }

149 150 151
    if (temperaturaQueDizTC > (temperaturaQueAchavaTC + 0)) {
      //Serial.print("+0.1, ");
      temperaturaMediaTC[temperaturaMediaIndexTC] = (temperaturaQueAchavaTC + 0.2);
João Lino's avatar
João Lino committed
152 153
    }
    else {
154 155 156 157 158 159 160 161 162
      if (temperaturaQueDizTC < (temperaturaQueAchavaTC - 0.1)) {
        //Serial.print("-0.1, ");
        temperaturaMediaTC[temperaturaMediaIndexTC] = (temperaturaQueAchavaTC - 1.0);
      }
      else {
        //Serial.print(", ");
        temperaturaMediaTC[temperaturaMediaIndexTC] = temperaturaQueDizTC;
      }
    }
João Lino's avatar
João Lino committed
163

164 165 166 167
    // Calcula a média
    double tempetaturaQueEuAchoTC = 0.0;
    for ( int i = 0; i < TEMPERATURE_SAMPLES_TC; i++ ) {
      tempetaturaQueEuAchoTC += temperaturaMediaTC[i];
João Lino's avatar
João Lino committed
168
    }
169 170 171 172
    tempetaturaQueEuAchoTC /= TEMPERATURE_SAMPLES_TC;
    
    // Grava o valor aual para o proximo ciclo
    temperaturaQueAchavaTC = tempetaturaQueEuAchoTC;
João Lino's avatar
João Lino committed
173

174 175 176 177 178 179 180 181 182 183
#ifdef __debug
    Serial.print(millis());
    Serial.print(", ThermoCouple, ");
    Serial.print(Setpoint, 2);
    Serial.print(", ");
    Serial.print(temperaturaQueDizTC, 2);
    Serial.print(", ");
    Serial.println(tempetaturaQueEuAchoTC, 2);
#endif
  }
João Lino's avatar
João Lino committed
184

185 186 187 188 189
  currentTime = millis();
  if (currentTime > (dhtReadTime + 200)) {
    
    // Read data off of the DHT11 sensor
    int chk = DHT.read11(DHT11_PIN);
João Lino's avatar
João Lino committed
190

191 192 193 194 195 196 197 198 199
    // debug received data
    tempSensorOK = debugDHT(chk);
  
    dhtReadTime = millis();
  }
  else {
    tempSensorOK = false;
  }
  
200
  if(tempSensorOK) {
João Lino's avatar
João Lino committed
201

202 203 204 205
    double temperaturaQueDiz = DHT.temperature;
  
    if (temperaturaQueAchava == 200.0) {
      temperaturaQueAchava = temperaturaQueDiz;
206 207 208 209 210 211

      if(millis() < 1000) {
        for( temperaturaMediaIndex = 0; temperaturaMediaIndex < TEMPERATURE_SAMPLES; temperaturaMediaIndex++ ) {
          temperaturaMedia[temperaturaMediaIndex] = temperaturaQueAchava;
        }
      }
212 213 214 215 216 217 218
    }
  
    // Calcular a temperatura de processamento
    temperaturaMediaIndex++;
    if ( temperaturaMediaIndex >= TEMPERATURE_SAMPLES ) {
      temperaturaMediaIndex = 0;
    }
João Lino's avatar
João Lino committed
219

220 221 222 223 224 225 226 227 228 229
    if (temperaturaQueDiz > (temperaturaQueAchava + 0)) {
      temperaturaMedia[temperaturaMediaIndex] = (temperaturaQueAchava + 0.2);
    }
    else {
      if (temperaturaQueDiz < (temperaturaQueAchava - 0.1)) {
        temperaturaMedia[temperaturaMediaIndex] = (temperaturaQueAchava - 1.0);
      }
      else {
        temperaturaMedia[temperaturaMediaIndex] = temperaturaQueDiz;
      }
João Lino's avatar
João Lino committed
230 231
    }

232
    // Calcula a média
233 234 235 236 237 238
    double tempetaturaQueEuAcho = 0.0;
    for ( int i = 0; i < TEMPERATURE_SAMPLES; i++ ) {
      tempetaturaQueEuAcho += temperaturaMedia[i];
    }
    tempetaturaQueEuAcho /= TEMPERATURE_SAMPLES;
    
239 240 241 242 243 244
    // Grava o valor aual para o proximo ciclo
    temperaturaQueAchava = tempetaturaQueEuAcho;
    
#ifdef __debug
    Serial.print(millis());
    Serial.print(", DHT, ");
245 246 247 248
    Serial.print(Setpoint, 2);
    Serial.print(", ");
    Serial.print(temperaturaQueDiz, 2);
    Serial.print(", ");
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    Serial.println(tempetaturaQueEuAcho, 2);
#endif

    executeCompressorControl( tempetaturaQueEuAcho, Setpoint );
  }

  #ifdef __info
    Serial.print(millis());
    Serial.print(", ");
    Serial.print(Setpoint, 2);
    Serial.print(", ");
    Serial.print(temperaturaQueAchava, 2);
    Serial.print(", ");
    Serial.print(temperaturaQueAchavaTC, 2);
    Serial.print(", ");
    Serial.println(relayState?"1":"0");
#endif
}

void processBtnPress() {
  if ( hasBtnPressed() ) {

    // increase mode
    SetpointMode++;

    // reset mode back to 0
    if ( SetpointMode > 63 ) {
      SetpointMode = 0;
    }

    // change leds
    setLEDs(SetpointMode);

    if ( SetpointMode > 31 ) {

      ///TODO set automatic fermentation temperatures
      Setpoint = SetpointMode - 32 + 0.5;
286 287
    }
    else {
288 289 290

      // set manual temperatures
      Setpoint = SetpointMode;
291
    }
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  }
}

void executeCompressorControl( double tempetaturaAtual, double temperaturaTarget ) {
  if ( tempetaturaAtual >= (temperaturaTarget + 1.0) ) {
    setCompressor(true);
  }
  else {
    if ( tempetaturaAtual <= (temperaturaTarget - 0.0) ) {
      setCompressor(false);
    }
  }
}

void setCompressor( boolean turnIt ) {
  currentTime = millis();

  if(relayState) {
    if(turnIt) {
      // Nothing to do, it is on, will stay on
312 313
    }
    else {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
      // turn off compressor to stop cooling
      lastTurnOff = currentTime;
      relayState = false;
      digitalWrite( RelayPin, HIGH );
    }
  }
  else {
    if(turnIt) {
      if( currentTime >= (lastTurnOff + MINIMUM_TIME_OFF)) {
        // turn on compressor to start cooling
        relayState = true;
        digitalWrite( RelayPin, LOW );
      }
    }
    else {
      // Nothing to do, it is off, will stay off
330
    }
João Lino's avatar
João Lino committed
331
  }
332 333 334 335 336 337 338 339 340 341 342 343 344

#ifdef __debug
  Serial.print(millis());
  Serial.print(", Compressor, ");
  Serial.print(Setpoint, 2);
  Serial.print(", ");
  if (relayState) {
    Serial.println( "ON" );
  }
  else {
    Serial.println( "OFF" );
  }
#endif
João Lino's avatar
João Lino committed
345 346 347 348 349
}

void setLEDs( int number ) {
  int calcNumber = number;

350 351
  for ( int i = (LEDCount - 1); i >= 0; i-- ) {
    boolean zero = ((calcNumber % 2) == 0) ? true : false;
João Lino's avatar
João Lino committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    calcNumber /= 2;

    if (zero) {
      digitalWrite(leds[i], LOW);
    }
    else {
      digitalWrite(leds[i], HIGH);
    }
  }
}

boolean hasBtnPressed() {
  if ( digitalRead(ButtonPin) == HIGH ) {

    while ( digitalRead(ButtonPin) ) {
      delay(20);
    }

    // debounce
    delay(20);

    return true;
  }
  else {
    return false;
  }
}

380 381
boolean debugDHT( int chk ) {
  boolean ret = false;
João Lino's avatar
João Lino committed
382
#ifdef __debug
383
  //Serial.print("DHT11,   ");
João Lino's avatar
João Lino committed
384 385 386 387 388
#endif
  
  switch (chk)
  {
    case DHTLIB_OK:
389
      ret = true;
390
/*#ifdef __debug
391
      Serial.print("DHT11,   ");
João Lino's avatar
João Lino committed
392
      Serial.print("OK,  ");
393
#endif*/
João Lino's avatar
João Lino committed
394 395 396 397 398 399 400 401
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,  ");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,  ");
      break;
    case DHTLIB_ERROR_CONNECT:
402 403 404 405 406 407 408 409
      //Serial.print("Connect error,  ");
/*
      relayState = false;
      digitalWrite( RelayPin, HIGH );
      setLEDs(63);

      while(true) Serial.println("ERROR, SAFEMODE ON!!!!");
  */    
João Lino's avatar
João Lino committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
      break;
    case DHTLIB_ERROR_ACK_L:
      Serial.print("Ack Low error,  ");
      break;
    case DHTLIB_ERROR_ACK_H:
      Serial.print("Ack High error,  ");
      break;
    default:
      Serial.print("Unknown error,  ");
      break;
  }

#ifdef __debug
  //Serial.print(DHT.humidity, 1);
  //Serial.print(",  ");
#endif
426 427

  return ret;
João Lino's avatar
João Lino committed
428 429
}