Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Gyula Ősi
Published © CC BY-NC

Smart Thermostat, Airing and Lights Control with Bluetooth

Just connect your Arduino smart home and control the heater, airing, and lights via Bluetooth!

BeginnerFull instructions provided1 hour24,415

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
DS18B20 Programmable Resolution 1-Wire Digital Thermometer
Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer
×1
oled 1306 I2C bicolor
×1
4-CHANNEL RELAY CONTROLLER FOR I2C
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
×1

Software apps and online services

Morich Serial Bluetooth Terminal for Android
Arduino Bluetooth Control Device for Android

Story

Read more

Schematics

Simplified for try out

For jumper wire test...

Detailed schematics

You have to solder it

Code

Thermostat v11 - Celsius version.ino

Arduino
The program code explains itself, useful for beginners. Well commented, describes every important steps; what, why and how. Divided into modules by functions, that’s why easy to overview.
// Celsius version v1.1
// Program code of an intelligent Arduino smarthome thermostat solution, 
// based on Arduino Nano (or higher) board, DS18B20 thermo sensor, HC-05 Bluetooth adapter, I2C 128X64 bicolor OLED display.
// The system can be controlled by buttons and Android smartphone via bluetooth.
//
// It handles the HEATER gas boiler, the bathroom AIRING ventilator and the kitchen LIGHTING - swithed with relays.
// The heater has two working ways. 1: One Time Heating (15 mins) timed mode, 2: Thermostat mode (higher priority). The adjusted target tempr.
// stored in EEPROM. The program detects hardware errors and window opening - in these cases the heater stops and/or will not start.
//
// Designed and programmed by Gyula Osi.
// All rights reserved.
// ------------------------------------------------------------------------------------------------------------------------------------

// ---- Display                               I2C Bus, SDA(TX) -> A4, SCL(RX) -> A5
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // display constructor
byte frame = 0;                               // start logo pointer

// ---- Ports and related declarations
const byte buzzer = 9;   // buzzer to D9
int btnVal;              // stores analog values from buttons
const byte relayA = 11;  // airing
bool aState = 0;
bool aStateByCmd;        // if "window alert" is gone, the airing control goes back to the original state
const byte relayL = 13;  // lighting
bool lState = 0;
const byte relayH = 10;  // heater
const byte ledH = 6;     // PWM ports allow output level adjustment used to change brightness
const byte ledA = 5;     
const byte ledL = 3;
const byte bright[3] = {0, 10, 100};
byte brightHeat = bright[2];
#define ledInterval 1000 // heater led blinking interval
unsigned long prev = 0;

// ---- Strings for Display and Serial Reports 
#define STATE_ITEMS 3    // device state indicator strings for outputs
// i ->                                  0      1       2
const String state_str[STATE_ITEMS] = {"on.", "off.", "auto."}; 
String heaterState;               
String airingState;
String lightingState;
#define FUNCT_ITEMS 11
// i ->                            0             1             2               3                4                  5               6                  7                  8                 9     10              
String funct_str[FUNCT_ITEMS] = {"Heater is ", "Airing is ", "Lighting is ", "Window Alert!", "Hardware Error!", "Hit a Key >>", "or send a Code!", "Target tempr = ", "Temperature = ", " * ", " -"};

// ---- Temperature Measurement and Heater Related Features
#include <elapsedMillis.h>        
elapsedMillis timer0;              // 8-bit, PWM timer, used by function elapsedMillis()
#define sftyTmrInterval 15 * 60000 // one Time Heating (15 mins) timed mode interval [ms]
bool sftyTmrEnded;                 // boolean startup, the timer0 has ended
#include <EEPROM.h>
byte tTarget;                      // adjusted target tempr
const int addr = 0;                // the current address of the tTarget variable in the EEPROM memory
bool hState = 0;                   // heater boolean state
bool hThermostat = 0;              // thermostat boolean state
#include "OneWire.h" 
#include "DallasTemperature.h"
#define DS18B20 2                  // setup the OneWire bus on D2
OneWire temprWire(DS18B20);        // setup DS18B20 to work on the OneWire bus
DallasTemperature sensors(&temprWire);
float tempr;                       // measured value
float temprPrev;                   // copy of measured value for trend analysis
bool windowAlrt = 0;               // specified degree of tempr drop
bool measError = 0;                // measured tempr value is out of the range specified as normal
const long temprInterval = 60000;  // cyclic tempr measurement interval [ms]
unsigned long temprTmrPrev = 0;    // the elapsed will be the previous when temprMeas() called
float heatCorrVal;                 // declares the degree of overheating and cooling back, see tempMeas() function

// ---- Configuration of Serial Communication on virtual RXD/TXD
#include <SoftwareSerial.h>        // SW serial RX & TX pins for HC-05
const int RX1 = 8;
const int TX1 = 4;
SoftwareSerial sUART(RX1,TX1); 
char RX[2];                        // store received serial data
bool errMsgSentBySys = 0;   
bool startup = 1;                  // keep avoid a duplicate BT report at startup

const uint8_t frame1[] U8G_PROGMEM = {  // XBM map
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00, 0xFE, 0xFF, 
  0xFF, 0xFF, 0x07, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 0xFC, 0xFF, 0x7F, 0xF0, 
  0x3F, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0x07, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 
  0xFC, 0xFF, 0x7F, 0xF0, 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xFF, 
  0xFF, 0xFF, 0x1F, 0xFF, 0xF1, 0xFF, 0xFF, 0xFF, 0x8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, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x7E, 0x06, 0xE6, 0x3F, 0x06, 0xC6, 0x7F, 0xFE, 0xE7, 0x3F, 0x7E, 
  0xFE, 0xC7, 0x7F, 0x00, 0x00, 0x30, 0x06, 0x66, 0x60, 0x06, 0x66, 0x00, 
  0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 0x00, 0x30, 0x06, 0x66, 
  0x60, 0x06, 0x66, 0x00, 0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 
  0x00, 0x30, 0x1E, 0x66, 0x60, 0x06, 0x66, 0x00, 0x60, 0x60, 0x60, 0x30, 
  0x06, 0x60, 0x00, 0x00, 0x00, 0x30, 0x3E, 0x66, 0x60, 0x06, 0x66, 0x00, 
  0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 0x00, 0x3C, 0x7E, 0xE6, 
  0x61, 0x1E, 0xC7, 0x3F, 0x70, 0xE0, 0x3F, 0x3C, 0xFE, 0xC3, 0x3F, 0x00, 
  0x00, 0x3C, 0x7E, 0xE6, 0x61, 0x1E, 0xC7, 0x3F, 0x70, 0xE0, 0x3F, 0x3C, 
  0xFE, 0xC3, 0x3F, 0x00, 0x00, 0x3C, 0xDE, 0xE7, 0x61, 0x1E, 0x07, 0x70, 
  0x70, 0xE0, 0x1D, 0x3C, 0x1E, 0x00, 0x70, 0x00, 0x00, 0x3C, 0x1E, 0xE7, 
  0x61, 0x1E, 0x07, 0x70, 0x70, 0xE0, 0x31, 0x3C, 0x1E, 0x00, 0x70, 0x00, 
  0x00, 0x3C, 0x1E, 0xE6, 0x61, 0x1E, 0x07, 0x70, 0x70, 0xE0, 0x61, 0x3C, 
  0x1E, 0x00, 0x70, 0x00, 0x00, 0x3C, 0x1E, 0xE6, 0x61, 0x1E, 0x07, 0x70, 
  0x70, 0xE0, 0x61, 0x3C, 0x1E, 0x00, 0x70, 0x00, 0x00, 0x7F, 0x1E, 0xE6, 
  0x3F, 0xFC, 0xE3, 0x3F, 0x70, 0xE0, 0x61, 0x7E, 0xFE, 0xE7, 0x3F, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 
};

void setup() {  
  sUART.begin(9600);
  pinMode(relayH, OUTPUT);   
  relayHandlerH(4);             // get the working state
  pinMode(ledH, OUTPUT);
  pinMode(relayA, OUTPUT);
  relayHandlerA(4);
  pinMode(ledA, OUTPUT);
  pinMode(relayL, OUTPUT);
  relayHandlerL(4);
  pinMode(ledL, OUTPUT);
  pinMode(buzzer, OUTPUT);
  sensors.begin();              // start DS18B20
  temprMeas();                  // do not wait for the meas.timer, call the function once at startup
  tTarget = EEPROM.read(addr);  // read the previously stored tTarget value from the current address of the EEPROM at startup
  startup = 0;
  if (!measError) {
    sTX(8);                     // call for instant report of working states after startup
  }
  else {
    sTX(2);
  }
}

void loop() { 
  temprTimer();
  ledHandler();
  btnReadings();
  sRX();
  safetyTmr();
  
  u8g.firstPage();         // scr loop
    do {
      draw();
    } while( u8g.nextPage() );
    if (frame == 0) { 
    delay(3000);
    frame = 1;
    clrScr(); 
    }
}

void btnReadings() {              // --------- Btn Readings
  btnVal = analogRead(A0);                     // read analog val from A0
  if (btnVal >= 510 && btnVal <= 516) {        // btn Lighting
    delay(100);                                // btn debounce
    buzz(3, 1);
    relayHandlerL(2);                          // call proper function with logical state flip opcode as parameter
  }
  else if (btnVal >= 849 && btnVal <= 855){    // btn Airing
    if (windowAlrt == 1) {                     // if the system is in Window Alert mode, disable it
      windowAlrt = 0;                                                        
      buzz(4, 3);                                                            
      relayHandlerA(3);
    }
    else {                                     // else turn on/off the airing ventilator
    delay(100);
    buzz(3, 1);
    relayHandlerA(2);
    }
  }
  else if (btnVal >= 927  && btnVal <= 933){   // btn One Time Heating (15 mins) timed mode
    delay(100);
    buzz(3, 1);
    relayHandlerH(2);
  }
  else if (btnVal >= 767 && btnVal <= 777) {   // btn decrease tTarget
    delay(100);
    tTargetHandler(0);
  }
  else if (btnVal >= 687 && btnVal <= 697) {   // btn increase tTarget
    delay(100);
    tTargetHandler(1);
  }
  else if (btnVal >= 856 && btnVal <= 862) {   // inc & dec btns at the same time
    tTarget = 14;                              // <==== initial value - press these buttons at the very first powerup!
  }
}

void sRX() {     // ------------- Receive Serial Data
  while (sUART.available() > 0) { // if data is available to read
    for (byte i = 0; i < 2; i++) {
      RX[i] = sUART.read();
    }
  }  
  int iRX[2]; 
  for (byte i = 0; i < 2; i++) {
    iRX[i] = RX[i] - '0';
  } 
  
  switch (RX[0]) {     // ------------ accept SINGLE ALPHABETICAL control codes
    case 'A':
      relayHandlerH(1);   // 1 = on
      break;
    case 'a':
      relayHandlerH(0);   // 0 = off
      break;                           // Received serial data can be a single alphabetical letter from "Arduino 
    case 'B':                          // Bluetooth Control Device" Android app. If a proper alphabetical
      relayHandlerA(1);                // character arrives, the program code will not wait for the second one,
      break;                           // but calls the applicable function with a proper operation code.
    case 'b':                          // Cases of combined numeric data can be seen below.
      relayHandlerA(0);    
      break;
    case 'C':
      relayHandlerL(1);
      break;
    case 'c':
      relayHandlerL(0);
      break;
    case 'D':
    case 'd':
      tTarget = 21;                           
      tTargetEEPROM();
      buzz(3, 1);
      break; 
    case 'E':
    case 'e':
      tTarget = 19;                           
      tTargetEEPROM();
      buzz(3, 1);
      break;
    case 'F':
    case 'f':
      tTarget = 14;                          
      tTargetEEPROM();
      buzz(3, 1);
      break;
    case 'R':             // call for an overview report about controlled devices
    case 'r':
      sTX(8);
      break;
    case 'W':             // disable Window Alert state
    case 'w':
      windowAlrt = 0;                                                        
      buzz(4, 3);                                                            
      relayHandlerA(3);
      break;
  }
             // ----------------------- accept COMBINED NUMERIC control codes
                        // In this case a two-digit numeric control code arrives in char format,
                        // from an Android bluetooth serial app for instance. After a char to integer
                        // conversion (only if the first char is '1' or '2') a merge-process will follow, 
                        // and the system of conditions and statements will make a decision and execute it.
                        // Appropriate numeric codes are:
                        //          
                        // ---------------- Target Temperature:
                        // 10 - 24 values will be accepted as a target temperature for the thermostat function.
                        //
                        // ---------------- Device Control Codes:
                        // First = device code, Second = operator code
                        // 30, 31, 32 turns the lighting: 30=off, 31=on
                        //                                32=flip logical state (on -> off / off -> on) 
                        // 40, 41, 42 will do the same to the airing ventilator
                        // 50, 51, 52 handles the One Time Heating (15 mins) timed heater program as above 
                        //
                        // ---------------- Classified Operator Codes:
                        // X3, X4 are classified, used only for function calls by inner program sequences            
  if (RX[0] == '3') {                               
    relayHandlerL(iRX[1]);                            
  }                                                 
  if (RX[0] == '4') {                               
    relayHandlerA(iRX[1]);                            
  }                                               
  if (RX[0] == '5') {                               
    relayHandlerH(iRX[1]);                            
  }
                                                 
  if ((iRX[0] * 10 + iRX[1] <= 24) && (iRX[0] * 10 + iRX[1] >= 10)) { // accept only numeric values between 10 & 24
    tTarget = iRX[0] * 10 + iRX[1];                                   // merge two integers and set tTarget
    tTargetEEPROM();                                                  // after set, call the EEPROM handler function, and
    buzz(3, 1);                                                       // write the tTarget value to the appropriate byte of the EEPROM
  }
  if (RX[0] =='0') {                      // test
    for (byte i = 1; i < 5; i++) {
      buzz(5, 2);
    }
  }
  for (byte i = 0; i < 2; i++) {          // empty all message receiver and conversion variables
    RX[i] = 'Z';
  }                    
}

void relayHandlerL(byte lOperator) {  // Lighting Handler Sequence
                                      // operators are: 0=off, 1=on, 2=flip the state, 4=fill/refill the lighting state char var.
  if ((measError) && ((lOperator == 1) || (lOperator == 2))) {
    sTX(4);
    return;                                    
  }
  if ((lOperator == 2) || (lOperator == 0) && (lState) || (lOperator == 1) && (!lState)) {
    lState = !lState;
    digitalWrite(relayL, lState);
    buzz(2, 1);
  }  

  if (lOperator >= 0) {               // fill up the working state char with the proper state indicator string
    if (lState) {
      lightingState = state_str[0];
    }
    else  {
      lightingState = state_str[1];
    }
    if (!startup) {
      sTX(7);
    }
  }
}

void relayHandlerA(byte aOperator) {  // Airing Handler Sequence
  if ((measError) && ((aOperator == 1) || (aOperator == 2))) {  // operators are: 0=off, 1=on, 2=flip the state, 
    sTX(4);                                                     // 3=called by temprMeas() funct., 4=fill/refill the airing state char var.
    return;                                     
  }
  aState = digitalRead(relayA); 
                                  
  if (!windowAlrt) {
    if ((aOperator == 2) || (aState) && (aOperator == 0) || (!aState) && (aOperator == 1)) {
      aState = !aState;
      digitalWrite(relayA, aState);
      aStateByCmd = digitalRead(relayA);
      buzz(2, 1);
    } 
  } 
  if (aOperator == 3) {                 // called by the temprMeas() function, 'windowAlrt' ended or started
    if ((!aState) && (windowAlrt) || (aState) && (!windowAlrt) && (!aStateByCmd)) {
      digitalWrite(relayA, windowAlrt);
    }
  }
  aState = digitalRead(relayA);
  if (aOperator >= 0) {
    if (aState) {
      if (windowAlrt) {
        airingState = state_str[2];
      }
      else  {
        airingState = state_str[0];
      }
    }
    else {
      airingState = state_str[1];
    }
  }
  if (!startup) {
    sTX(6);
  }
}
 
void relayHandlerH(byte hOperator) {  // Heater Handler Sequence 
                                      // operators are: 0=off, 1=on, 2=flip the state, 
                                      // 3=called by temprMeas() funct., 4=fill/refill the heater state char var.
  if ((measError) && ((hOperator == 1) || (hOperator == 2))) {
    sTX(4);
    return;                                    
  }                                    
  if ((!hThermostat) && (!windowAlrt) && (!measError)) {     // turn on/off the One Time Heating (15 mins) timed mode
    if ((hOperator == 2) || (hOperator == 1) && (!hState) || (!hOperator) && (hState)) {
      buzz(2, 1);
      hState = !hState;
      sftyTmrEnded = 0;
      timer0 = 0;   
      digitalWrite(relayH, hState);
    }
  }
      
  if (windowAlrt) {
    sTX(3);
  }
  
  if (hOperator == 3) {               // this function called by the temprMeas() function (op 3) 
                                      // in order to examine windowAlrt & measError booleans                                                                                                                           
    if ((windowAlrt) && (hState)) {   // a window is open and the heater is running
      digitalWrite(relayH, 0);
      buzz(5, 3);
    }
    if ((!windowAlrt) && (!measError)) {
      if ((hThermostat) || (!hThermostat) && (hState) && (sftyTmrEnded)) { 
        digitalWrite(relayH, hThermostat);     // proceed the command of the Thermostat Routine
      }
    }
  }
  hState = digitalRead(relayH);
  if (hOperator >= 0) {
    if (hState) {
      if (hThermostat) {
        heaterState = state_str[2];
      }
      else  {
        heaterState = state_str[0];
      }
    }
    else {
      heaterState = state_str[1];
    }
    if ((((!windowAlrt) && (hOperator != 3)) || (hState)) && (!startup)) {
      sTX(5);
    }
  }
}

void safetyTmr () {     // Timer for the One Time Heating (15 mins timed) mode
  if ((hState) && (!sftyTmrEnded) && (timer0 > sftyTmrInterval) && (!hThermostat)) {
    sftyTmrEnded = 1;
    relayHandlerH(0);
    for (byte i = 1; i < 5; i++) {
      buzz(i, 2);
    }
  }
}

void temprTimer() {     // Cyclic Timer for temprMeas()
  unsigned long temprTmrCurr = millis();
  if (temprInterval <= temprTmrCurr - temprTmrPrev) {
    temprTmrPrev = temprTmrCurr;
    temprMeas();
  } 
}

void temprMeas() {       // ----------- Temperature Measurement & Comparison Sequence
  temprPrev = tempr;                    // save the value for next comparison
  sensors.requestTemperatures();        // update sensor readings
  tempr = sensors.getTempCByIndex(0);   // read remperature

  if ((tempr >= 40) || (tempr <= 0)) {  // extreme meas values:
    if (!errMsgSentBySys) {             // -127, -196.60 are HW errors, +85 is tipically SW error, but
      sTX(4);                           // can be fire, or a broken window
    }
    errMsgSentBySys = 1;
    hThermostat = 0;
    if (hState) {
      relayHandlerH(0);                      
    }
    if (aState) {
      relayHandlerA(0);                      
    }
    if (lState) {
      relayHandlerL(0);
    }  
    measError = 1;
    for (byte i = 1; i < 10; i++) {        
      buzz(4, 1);                          
      delay(50);
    } 
  }                                                       
  else {
    temprPrev = tempr;  
    measError = 0;
    errMsgSentBySys = 0;
  }

  if (!measError) {       // ------------ Start of Temperature Analysis Sequence
    if (tempr <= 17) {                    // Frequent, short-term switching of the heater gas boiler would cut short its lifetime, the
      heatCorrVal = 0.5;                  // heatCorrVal value helps to keep avoid it. Declares the degree of overheating and cooling back.
    }                                     // Lower temperature demands greater heatCorrVal, because the walls are colder and adsorb better the
    if ((tempr > 17) && (tempr < 19)) {   // warmth from the freshly heated-up air, so the above described effect would more effective.
      heatCorrVal = 0.4;
    }
    if (tempr >= 19) {
      heatCorrVal = 0.3;
    }
    
    if (tTarget - tempr >= heatCorrVal) { // subtract measured value from target, if the difference equals or greater than heatCorrVal
      sftyTmrEnded = 1;                   // deactivate the One Time Heating (15 mins) timed program if it is running
      hThermostat = 1;                    // turn on the thermostat
      buzz(1, 1);                      
    }
    if ((tTarget - tempr <= -1 * heatCorrVal) && (hThermostat)) {  
      hThermostat = 0;
    }
    
    if ((temprPrev - tempr >= 0.2) && (!windowAlrt) && (tempr <= 20)) {   // in a measurement cycle and in heating season the temperature
      windowAlrt = 1;                                                     // drops, it will evaluate as a window is open
      sftyTmrEnded = 1;                                    
      for (byte i = 1; i < 5; i++) {
        buzz(4, 1);
        delay(50);
      }
      relayHandlerA(3);                                  // call airing function (opcode = 3), to help refresh the air
    }
    if ((temprPrev - tempr <= -0.12) && (windowAlrt)) {  // the tempr. falling is over, the air became warmer 
      windowAlrt = 0;                                    // due to the heat capacity of the environment, 
      buzz(4, 3);                                        // so switch back to normal mode
      relayHandlerA(3);
    }
    relayHandlerH(3);                                    // the function will examine caller param(3) & windowAlrt & measError booleans
                                                            
    if (!windowAlrt) {
      sTX(1);
    }
  }
}

void tTargetHandler (bool set) {  // set the needed tempr by increasing or decreasing
  if (!set) {                     // incr
    if (tTarget < 24) {           // until it reaches the upper limit
      tTarget++ ;
      buzz(3, 1);
      }
    else {
      buzz(2, 3);
    }
  }
  else {                          // decr
    if (tTarget > 10) {
      tTarget-- ;
      buzz(3, 1); 
    }
    else {
      buzz(2, 3);
    }
  }
  tTargetEEPROM();
}

void tTargetEEPROM() {
  EEPROM.write(addr, tTarget);    // after incr/decr/set, write the tTarget value to the appropriate byte of the EEPROM
  delay(10);
  sTX(2);
}

void draw(void) {                 // logo handler
  if (frame == 0) {
    u8g.drawXBMP( 0, 0, 128, 64, frame1);
  }
  else if (frame == 1)
    screenFunctState(); 
}
      
void screenFunctState(void) {     // function state screen
  temprWriteOut(0, 64);
  u8g.drawHLine(0, 46, 128);
  u8g.setFont(u8g_font_unifont);
  if (!windowAlrt) {
    u8g.setPrintPos( 0, 14);
    u8g.print(funct_str[0]);
    u8g.setPrintPos(84, 14);
    u8g.print(heaterState);
  }
  else {
    u8g.setPrintPos( 0, 14);
    u8g.print(funct_str[3]);
  }
  u8g.setPrintPos( 0, 28);
  u8g.print(funct_str[1]);
  u8g.setPrintPos(88, 28);
  u8g.print(airingState);
  u8g.setPrintPos( 0, 42);
  u8g.print(funct_str[2]);
  u8g.setPrintPos(95, 42);
  u8g.print(lightingState);

  if ((!hState) && (!aState) && (!lState)) {
    screenStndby();               // if all of controlled devices are in off, call standby screen
  }
}

void screenStndby() {             // standby scr
  u8g.firstPage(); 
    do {
      u8g.setFontRefHeightText();
      u8g.setFont(u8g_font_unifont);
      if (!measError) {
        u8g.setPrintPos(33, 52);
        u8g.print(funct_str[5]);
        u8g.setPrintPos( 8, 64);
        u8g.print(funct_str[6]);
      }
      else {
        u8g.setPrintPos( 4, 48);
        u8g.print(funct_str[4]);
      }
      temprWriteOut(0, 16) ;
    } while( u8g.nextPage() );
}

void temprWriteOut (byte tX, byte tY) {   // draw tempr & tTarget variables onto different coordinates
  u8g.setFont(u8g_font_courB14);
//u8g.setFont(u8g_font_6x12);             // you can save ~10% of prog.memory using this font with 2x2 scale
  char buftTarget[9];                   
  sprintf (buftTarget, "%d", tTarget);    // int to char
//u8g.setScale2x2();
//tY = tY / 2;
  u8g.setPrintPos(tX, tY); 
  u8g.print(buftTarget);
  u8g.setPrintPos(tX+18, tY); 
  u8g.print(funct_str[9]);
  u8g.setPrintPos(tX+50, tY);           
  u8g.print(tempr);                     
  u8g.print(char(176)); 
  u8g.print("C");
//u8g.undoScale();
}

void clrScr(){
  u8g.firstPage(); 
    do {
    } while( u8g.nextPage() );
}

void ledHandler() {                     // the brightness of a led is low, if the indicated device is off, and high, if its on
  if (aState) {
    analogWrite(ledA, bright[2]); 
  }
  else {
    analogWrite(ledA, bright[1]); 
  }
  if (lState)  {
    analogWrite(ledL, bright[2]); 
  }
  else {
    analogWrite(ledL, bright[1]); 
  }
  if (hState) {
    if (!hThermostat) {
      ledBlnk() ;                       // the heater led blinks when the One Time Heating (15 mins) timed mode is activated,
    }
    else {
      brightHeat = bright[2] ;          // and constant bright, if the thermostat routine is active
    }
  }  
  else {
    brightHeat = bright[1] ; 
  }
  analogWrite(ledH, brightHeat);
}

void ledBlnk() {
  unsigned long curr = millis();
  if (ledInterval <= curr - prev) {     // subtract prev value from current, if the difference equals or greater than ledInterval const.
    prev = curr;                        // overwrite the earlier value with the current and flip brightness level
    if (brightHeat == bright[1]) {      
      brightHeat = bright[2];
    } 
    else {
      brightHeat = bright[1];
    }
  }
  analogWrite(ledH, brightHeat);
}

void buzz(byte b, byte d) {              // call with frequency and delay parameters
  tone(buzzer, b * 1000); 
  delay(d * 100);        
  noTone(buzzer);
}

void sTX(byte reportTX) {              // sending serial reports
  switch (reportTX) {
    case 0:
      for (byte i = 0; i < 9; i++) {
        sUART.print(funct_str[10]);
      }
      sUART.println(funct_str[10]);
      break;
    case 1: 
      sUART.print(funct_str[8]);   // Tempr.
      sUART.print(tempr); 
      sUART.print(char(176)); 
      sUART.println("C");           
      break;
    case 2:
      sUART.print(funct_str[7]);   // TTemp
      sUART.print(tTarget); 
      sUART.print(char(176)); 
      sUART.println("C");  
      break;
    case 3:
      sUART.print(funct_str[3]);   // Window Alert
      sUART.print(funct_str[9]);  
      sUART.print(tempr); 
      sUART.print(char(176)); 
      sUART.println("C");      
      break;
    case 4:
      sUART.println(funct_str[4]); // Error report        
      break;
    case 5:
      sUART.print(funct_str[0]);   // Working state of devices
      sUART.println(heaterState);      
      break;
    case 6:
      sUART.print(funct_str[1]); 
      sUART.println(airingState);         
      break;
    case 7:
      sUART.print(funct_str[2]);
      sUART.println(lightingState);         
      break;
    case 8:                                // Overview report
      sTX(0);
      relayHandlerH(4);            
      relayHandlerA(4);
      relayHandlerL(4);
      sTX(2);
      if (measError) {
        sTX(4);
      }
      break;
  }
}

Thermostat v11 - Fahrenheit version.ino

Arduino
The program code explains itself, useful for beginners. Well commented, describes every important steps; what, why and how. Divided into modules by functions, that’s why easy to overview.
Works in Fahrenheit, I changed only the Target temperature values (50-76), and the device codes (1, 2 and 3).
// Fahrenheit version v1.1
// Program code of an intelligent Arduino smarthome thermostat solution, 
// based on Arduino Nano (or higher) board, DS18B20 thermo sensor, HC-05 Bluetooth adapter, I2C 128X64 bicolor OLED display.
// The system can be controlled by buttons and Android smartphone via bluetooth.
//
// It handles the HEATER gas boiler, the bathroom AIRING ventilator and the kitchen LIGHTING - swithed with relays.
// The heater has two working ways. 1: One Time Heating (15 mins) timed mode, 2: Thermostat mode (higher priority). The adjusted target tempr.
// stored in EEPROM. The program detects hardware errors and window opening - in these cases the heater stops and/or will not start.
//
// Designed and programmed by Gyula Osi.
// All rights reserved.
// ------------------------------------------------------------------------------------------------------------------------------------

// ---- Display                               I2C Bus, SDA(TX) -> A4, SCL(RX) -> A5
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // display constructor
byte frame = 0;                               // start logo pointer

// ---- Ports and related declarations
const byte buzzer = 9;   // buzzer to D9
int btnVal;              // stores analog values from buttons
const byte relayA = 11;  // airing
bool aState = 0;
bool aStateByCmd;        // if "window alert" is gone, the airing control goes back to the original state
const byte relayL = 13;  // lighting
bool lState = 0;
const byte relayH = 10;  // heater
const byte ledH = 6;     // PWM ports allow output level adjustment used to change brightness
const byte ledA = 5;     
const byte ledL = 3;
const byte bright[3] = {0, 10, 100};
byte brightHeat = bright[2];
#define ledInterval 1000 // heater led blinking interval
unsigned long prev = 0;

// ---- Strings for Display and Serial Reports 
#define STATE_ITEMS 3    // device state indicator strings for outputs
// i ->                                  0      1       2
const String state_str[STATE_ITEMS] = {"on.", "off.", "auto."}; 
String heaterState;               
String airingState;
String lightingState;
#define FUNCT_ITEMS 11
// i ->                            0             1             2               3                4                  5               6                  7                  8                 9     10              
String funct_str[FUNCT_ITEMS] = {"Heater is ", "Airing is ", "Lighting is ", "Window Alert!", "Hardware Error!", "Hit a Key >>", "or send a Code!", "Target tempr = ", "Temperature = ", " * ", " -"};

// ---- Temperature Measurement and Heater Related Features
#include <elapsedMillis.h>        
elapsedMillis timer0;              // 8-bit, PWM timer, used by function elapsedMillis()
#define sftyTmrInterval 15 * 60000 // one Time Heating (15 mins) timed mode interval [ms]
bool sftyTmrEnded;                 // boolean startup, the timer0 has ended
#include <EEPROM.h>
byte tTarget;                      // adjusted target tempr
const int addr = 0;                // the current address of the tTarget variable in the EEPROM memory
bool hState = 0;                   // heater boolean state
bool hThermostat = 0;              // thermostat boolean state
#include "OneWire.h" 
#include "DallasTemperature.h"
#define DS18B20 2                  // setup the OneWire bus on D2
OneWire temprWire(DS18B20);        // setup DS18B20 to work on the OneWire bus
DallasTemperature sensors(&temprWire);
float tempr;                       // measured value
float temprPrev;                   // copy of measured value for trend analysis
bool windowAlrt = 0;               // specified degree of tempr drop
bool measError = 0;                // measured tempr value is out of the range specified as normal
const long temprInterval = 60000;  // cyclic tempr measurement interval [ms]
unsigned long temprTmrPrev = 0;    // the elapsed will be the previous when temprMeas() called
float heatCorrVal;                 // declares the degree of overheating and cooling back, see tempMeas() function

// ---- Configuration of Serial Communication on virtual RXD/TXD
#include <SoftwareSerial.h>        // SW serial RX & TX pins for HC-05
const int RX1 = 8;
const int TX1 = 4;
SoftwareSerial sUART(RX1,TX1); 
char RX[2];                        // store received serial data
bool errMsgSentBySys = 0;   
bool startup = 1;                  // keep avoid a duplicate BT report at startup

const uint8_t frame1[] U8G_PROGMEM = {  // XBM map
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x7E, 0x06, 0xE6, 0x3F, 0x06, 0xC6, 0x7F, 0xFE, 0xE7, 0x3F, 0x7E, 
  0xFE, 0xC7, 0x7F, 0x00, 0x00, 0x30, 0x06, 0x66, 0x60, 0x06, 0x66, 0x00, 
  0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 0x00, 0x30, 0x06, 0x66, 
  0x60, 0x06, 0x66, 0x00, 0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 
  0x00, 0x30, 0x1E, 0x66, 0x60, 0x06, 0x66, 0x00, 0x60, 0x60, 0x60, 0x30, 
  0x06, 0x60, 0x00, 0x00, 0x00, 0x30, 0x3E, 0x66, 0x60, 0x06, 0x66, 0x00, 
  0x60, 0x60, 0x60, 0x30, 0x06, 0x60, 0x00, 0x00, 0x00, 0x3C, 0x7E, 0xE6, 
  0x61, 0x1E, 0xC7, 0x3F, 0x70, 0xE0, 0x3F, 0x3C, 0xFE, 0xC3, 0x3F, 0x00, 
  0x00, 0x3C, 0x7E, 0xE6, 0x61, 0x1E, 0xC7, 0x3F, 0x70, 0xE0, 0x3F, 0x3C, 
  0xFE, 0xC3, 0x3F, 0x00, 0x00, 0x3C, 0xDE, 0xE7, 0x61, 0x1E, 0x07, 0x70, 
  0x70, 0xE0, 0x1D, 0x3C, 0x1E, 0x00, 0x70, 0x00, 0x00, 0x3C, 0x1E, 0xE7, 
  0x61, 0x1E, 0x07, 0x70, 0x70, 0xE0, 0x31, 0x3C, 0x1E, 0x00, 0x70, 0x00, 
  0x00, 0x3C, 0x1E, 0xE6, 0x61, 0x1E, 0x07, 0x70, 0x70, 0xE0, 0x61, 0x3C, 
  0x1E, 0x00, 0x70, 0x00, 0x00, 0x3C, 0x1E, 0xE6, 0x61, 0x1E, 0x07, 0x70, 
  0x70, 0xE0, 0x61, 0x3C, 0x1E, 0x00, 0x70, 0x00, 0x00, 0x7F, 0x1E, 0xE6, 
  0x3F, 0xFC, 0xE3, 0x3F, 0x70, 0xE0, 0x61, 0x7E, 0xFE, 0xE7, 0x3F, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 
};

void setup() {  
  sUART.begin(9600);
  pinMode(relayH, OUTPUT);   
  relayHandlerH(4);             // get the working state
  pinMode(ledH, OUTPUT);
  pinMode(relayA, OUTPUT);
  relayHandlerA(4);
  pinMode(ledA, OUTPUT);
  pinMode(relayL, OUTPUT);
  relayHandlerL(4);
  pinMode(ledL, OUTPUT);
  pinMode(buzzer, OUTPUT);
  sensors.begin();              // start DS18B20
  temprMeas();                  // do not wait for the meas.timer, call the function once at startup
  tTarget = EEPROM.read(addr);  // read the previously stored tTarget value from the current address of the EEPROM at startup
  startup = 0;
  if (!measError) {
    sTX(8);                     // call for instant report of working states after startup
  }
  else {
    sTX(2);
  }
}

void loop() { 
  temprTimer();
  ledHandler();
  btnReadings();
  sRX();
  safetyTmr();
  
  u8g.firstPage();         // scr loop
    do {
      draw();
    } while( u8g.nextPage() );
    if (frame == 0) { 
    delay(3000);
    frame = 1;
    clrScr(); 
    }
}

void btnReadings() {              // --------- Btn Readings
  btnVal = analogRead(A0);                     // read analog val from A0
  if (btnVal >= 510 && btnVal <= 516) {        // btn Lighting
    delay(100);                                // btn debounce
    buzz(3, 1);
    relayHandlerL(2);                          // call proper function with logical state flip opcode as parameter
  }
  else if (btnVal >= 849 && btnVal <= 855){    // btn Airing
    if (windowAlrt == 1) {                     // if the system is in Window Alert mode, disable it
      windowAlrt = 0;                                                        
      buzz(4, 3);                                                            
      relayHandlerA(3);
    }
    else {                                     // else turn on/off the airing ventilator
    delay(100);
    buzz(3, 1);
    relayHandlerA(2);
    }
  }
  else if (btnVal >= 927  && btnVal <= 933){   // btn One Time Heating (15 mins) timed mode
    delay(100);
    buzz(3, 1);
    relayHandlerH(2);
  }
  else if (btnVal >= 767 && btnVal <= 777) {   // btn decrease tTarget
    delay(100);
    tTargetHandler(0);
  }
  else if (btnVal >= 687 && btnVal <= 697) {   // btn increase tTarget
    delay(100);
    tTargetHandler(1);
  }
  else if (btnVal >= 856 && btnVal <= 862) {   // inc & dec btns at the same time
    tTarget = 14;                              // <==== initial value - press these buttons at the very first powerup!
  }
}

void sRX() {     // ------------- Receive Serial Data
  while (sUART.available() > 0) { // if data is available to read
    for (byte i = 0; i < 2; i++) {
      RX[i] = sUART.read();
    }
  }  
  int iRX[2]; 
  for (byte i = 0; i < 2; i++) {
    iRX[i] = RX[i] - '0';
  } 
  
  switch (RX[0]) {     // ------------ accept SINGLE ALPHABETICAL control codes
    case 'A':
      relayHandlerH(1);   // 1 = on
      break;
    case 'a':
      relayHandlerH(0);   // 0 = off
      break;                           // Received serial data can be a single alphabetical letter from "Arduino 
    case 'B':                          // Bluetooth Control Device" Android app. If a proper alphabetical
      relayHandlerA(1);                // character arrives, the program code will not wait for the second one,
      break;                           // but calls the applicable function with a proper operation code.
    case 'b':                          // Cases of combined numeric data can be seen below.
      relayHandlerA(0);    
      break;
    case 'C':
      relayHandlerL(1);
      break;
    case 'c':
      relayHandlerL(0);
      break;
    case 'D':
    case 'd':
      tTarget = 21;                           
      tTargetEEPROM();
      buzz(3, 1);
      break; 
    case 'E':
    case 'e':
      tTarget = 19;                           
      tTargetEEPROM();
      buzz(3, 1);
      break;
    case 'F':
    case 'f':
      tTarget = 14;                          
      tTargetEEPROM();
      buzz(3, 1);
      break;
    case 'R':             // call for an overview report about controlled devices
    case 'r':
      sTX(8);
      break;
    case 'W':             // disable Window Alert state
    case 'w':
      windowAlrt = 0;                                                        
      buzz(4, 3);                                                            
      relayHandlerA(3);
      break;
  }
             // ----------------------- accept COMBINED NUMERIC control codes
                        // In this case a two-digit numeric control code arrives in char format,
                        // from an Android bluetooth serial app for instance. After a char to integer
                        // conversion (only if the first char is '1' or '2') a merge-process will follow, 
                        // and the system of conditions and statements will make a decision and execute it.
                        // Appropriate numeric codes are:
                        //          
                        // ---------------- Target Temperature:
                        // 50 - 76 values will be accepted as a target temperature for the thermostat function.
                        //
                        // ---------------- Device Control Codes:
                        // First = device code, Second = operator code
                        // 10, 11, 12 turns the lighting: 10=off, 11=on
                        //                                12=flip logical state (on -> off / off -> on) 
                        // 20, 21, 22 will do the same to the airing ventilator
                        // 30, 31, 32 handles the One Time Heating (15 mins) timed heater program as above 
                        //
                        // ---------------- Classified Operator Codes:
                        // X3, X4 are classified, used only for function calls by inner program sequences            
  if (RX[0] == '1') {                               
    relayHandlerL(iRX[1]);                            
  }                                                 
  if (RX[0] == '2') {                               
    relayHandlerA(iRX[1]);                            
  }                                               
  if (RX[0] == '3') {                               
    relayHandlerH(iRX[1]);                            
  }
                                                 
  if ((iRX[0] * 10 + iRX[1] <= 76) && (iRX[0] * 10 + iRX[1] >= 50)) { // accept only numeric values between 50 & 76
    tTarget = iRX[0] * 10 + iRX[1];                                   // merge two integers and set tTarget
    tTargetEEPROM();                                                  // after set, call the EEPROM handler function, and
    buzz(3, 1);                                                       // write the tTarget value to the appropriate byte of the EEPROM
  }
  if (RX[0] =='0') {                      // test
    for (byte i = 1; i < 5; i++) {
      buzz(5, 2);
    }
  }
  for (byte i = 0; i < 2; i++) {          // empty all message receiver and conversion variables
    RX[i] = 'Z';
  }                    
}

void relayHandlerL(byte lOperator) {  // Lighting Handler Sequence
                                      // operators are: 0=off, 1=on, 2=flip the state, 4=fill/refill the lighting state char var.
  if ((measError) && ((lOperator == 1) || (lOperator == 2))) {
    sTX(4);
    return;                                    
  }
  if ((lOperator == 2) || (lOperator == 0) && (lState) || (lOperator == 1) && (!lState)) {
    lState = !lState;
    digitalWrite(relayL, lState);
    buzz(2, 1);
  }  

  if (lOperator >= 0) {               // fill up the working state char with the proper state indicator string
    if (lState) {
      lightingState = state_str[0];
    }
    else  {
      lightingState = state_str[1];
    }
    if (!startup) {
      sTX(7);
    }
  }
}

void relayHandlerA(byte aOperator) {  // Airing Handler Sequence
  if ((measError) && ((aOperator == 1) || (aOperator == 2))) {  // operators are: 0=off, 1=on, 2=flip the state, 
    sTX(4);                                                     // 3=called by temprMeas() funct., 4=fill/refill the airing state char var.
    return;                                     
  }
  aState = digitalRead(relayA); 
                                  
  if (!windowAlrt) {
    if ((aOperator == 2) || (aState) && (aOperator == 0) || (!aState) && (aOperator == 1)) {
      aState = !aState;
      digitalWrite(relayA, aState);
      aStateByCmd = digitalRead(relayA);
      buzz(2, 1);
    } 
  } 
  if (aOperator == 3) {                 // called by the temprMeas() function, 'windowAlrt' ended or started
    if ((!aState) && (windowAlrt) || (aState) && (!windowAlrt) && (!aStateByCmd)) {
      digitalWrite(relayA, windowAlrt);
    }
  }
  aState = digitalRead(relayA);
  if (aOperator >= 0) {
    if (aState) {
      if (windowAlrt) {
        airingState = state_str[2];
      }
      else  {
        airingState = state_str[0];
      }
    }
    else {
      airingState = state_str[1];
    }
  }
  if (!startup) {
    sTX(6);
  }
}
 
void relayHandlerH(byte hOperator) {  // Heater Handler Sequence 
                                      // operators are: 0=off, 1=on, 2=flip the state, 
                                      // 3=called by temprMeas() funct., 4=fill/refill the heater state char var.
  if ((measError) && ((hOperator == 1) || (hOperator == 2))) {
    sTX(4);
    return;                                    
  }                                    
  if ((!hThermostat) && (!windowAlrt) && (!measError)) {     // turn on/off the One Time Heating (15 mins) timed mode
    if ((hOperator == 2) || (hOperator == 1) && (!hState) || (!hOperator) && (hState)) {
      buzz(2, 1);
      hState = !hState;
      sftyTmrEnded = 0;
      timer0 = 0;   
      digitalWrite(relayH, hState);
    }
  }
      
  if (windowAlrt) {
    sTX(3);
  }
  
  if (hOperator == 3) {               // this function called by the temprMeas() function (op 3) 
                                      // in order to examine windowAlrt & measError booleans                                                                                                                           
    if ((windowAlrt) && (hState)) {   // a window is open and the heater is running
      digitalWrite(relayH, 0);
      buzz(5, 3);
    }
    if ((!windowAlrt) && (!measError)) {
      if ((hThermostat) || (!hThermostat) && (hState) && (sftyTmrEnded)) { 
        digitalWrite(relayH, hThermostat);     // proceed the command of the Thermostat Routine
      }
    }
  }
  hState = digitalRead(relayH);
  if (hOperator >= 0) {
    if (hState) {
      if (hThermostat) {
        heaterState = state_str[2];
      }
      else  {
        heaterState = state_str[0];
      }
    }
    else {
      heaterState = state_str[1];
    }
    if ((((!windowAlrt) && (hOperator != 3)) || (hState)) && (!startup)) {
      sTX(5);
    }
  }
}

void safetyTmr () {     // Timer for the One Time Heating (15 mins timed) mode
  if ((hState) && (!sftyTmrEnded) && (timer0 > sftyTmrInterval) && (!hThermostat)) {
    sftyTmrEnded = 1;
    relayHandlerH(0);
    for (byte i = 1; i < 5; i++) {
      buzz(i, 2);
    }
  }
}

void temprTimer() {     // Cyclic Timer for temprMeas()
  unsigned long temprTmrCurr = millis();
  if (temprInterval <= temprTmrCurr - temprTmrPrev) {
    temprTmrPrev = temprTmrCurr;
    temprMeas();
  } 
}

void temprMeas() {       // ----------- Temperature Measurement & Comparison Sequence
  temprPrev = tempr;                    // save the value for next comparison
  sensors.requestTemperatures();        // update sensor readings
  tempr = sensors.getTempFByIndex(0);   // read remperature

  if ((tempr >= 104) || (tempr <= 32)) {  // extreme meas values:
    if (!errMsgSentBySys) {             // -127, -196.60 are HW errors, +85 is tipically SW error, but
      sTX(4);                           // can be fire, or a broken window
    }
    errMsgSentBySys = 1;
    hThermostat = 0;
    if (hState) {
      relayHandlerH(0);                      
    }
    if (aState) {
      relayHandlerA(0);                      
    }
    if (lState) {
      relayHandlerL(0);
    }  
    measError = 1;
    for (byte i = 1; i < 10; i++) {        
      buzz(4, 1);                          
      delay(50);
    } 
  }                                                       
  else {
    temprPrev = tempr;  
    measError = 0;
    errMsgSentBySys = 0;
  }

  if (!measError) {         // ------------ Start of Temperature Analysis Sequence
    if (tempr <= 62.6) {                    // Frequent, short-term switching of the heater gas boiler would cut short its lifetime, the
      heatCorrVal = 0.9;                    // heatCorrVal value helps to keep avoid it. Declares the degree of overheating and cooling back.
    }                                       // Lower temperature demands greater heatCorrVal, because the walls are colder and adsorb better the
    if ((tempr > 62.6) && (tempr < 66.2)) { // warmth from the freshly heated-up air, so the above described effect would more effective.
      heatCorrVal = 0.72;
    }
    if (tempr >= 66.2) {
      heatCorrVal = 0.54;
    }
    
    if (tTarget - tempr >= heatCorrVal) {   // subtract measured value from target, if the difference equals or greater than heatCorrVal
      sftyTmrEnded = 1;                     // deactivate the One Time Heating (15 mins) timed program if it is running
      hThermostat = 1;                      // turn on the thermostat
      buzz(1, 1);                      
    }
    if ((tTarget - tempr <= -1 * heatCorrVal) && (hThermostat)) {  
      hThermostat = 0;
    }
    
    if ((temprPrev - tempr >= 0.36) && (!windowAlrt) && (tempr <= 68)) {  // in a measurement cycle and in heating season the temperature
      windowAlrt = 1;                                                     // drops, it will evaluate as a window is open
      sftyTmrEnded = 1;                                    
      for (byte i = 1; i < 5; i++) {
        buzz(4, 1);
        delay(50);
      }
      relayHandlerA(3);                                  // call airing function (opcode = 3), to help refresh the air
    }
    if ((temprPrev - tempr <= -0.216) && (windowAlrt)) { // the tempr. falling is over, the air became warmer 
      windowAlrt = 0;                                    // due to the heat capacity of the environment, 
      buzz(4, 3);                                        // so switch back to normal mode
      relayHandlerA(3);
    }
    relayHandlerH(3);                                    // the function will examine caller param(3) & windowAlrt & measError booleans
                                                            
    if (!windowAlrt) {
      sTX(1);
    }
  }
}

void tTargetHandler (bool set) {  // set the needed tempr by increasing or decreasing
  if (!set) {                     // incr
    if (tTarget < 76) {           // until it reaches the upper limit
      tTarget++ ;
      buzz(3, 1);
      }
    else {
      buzz(2, 3);
    }
  }
  else {                          // decr
    if (tTarget > 50) {
      tTarget-- ;
      buzz(3, 1); 
    }
    else {
      buzz(2, 3);
    }
  }
  tTargetEEPROM();
}

void tTargetEEPROM() {
  EEPROM.write(addr, tTarget);    // after incr/decr/set, write the tTarget value to the appropriate byte of the EEPROM
  delay(10);
  sTX(2);
}

void draw(void) {                 // logo handler
  if (frame == 0) {
    u8g.drawXBMP( 0, 0, 128, 64, frame1);
  }
  else if (frame == 1)
    screenFunctState(); 
}
      
void screenFunctState(void) {     // function state screen
  temprWriteOut(0, 64);
  u8g.drawHLine(0, 46, 128);
  u8g.setFont(u8g_font_unifont);
  if (!windowAlrt) {
    u8g.setPrintPos( 0, 14);
    u8g.print(funct_str[0]);
    u8g.setPrintPos(84, 14);
    u8g.print(heaterState);
  }
  else {
    u8g.setPrintPos( 0, 14);
    u8g.print(funct_str[3]);
  }
  u8g.setPrintPos( 0, 28);
  u8g.print(funct_str[1]);
  u8g.setPrintPos(88, 28);
  u8g.print(airingState);
  u8g.setPrintPos( 0, 42);
  u8g.print(funct_str[2]);
  u8g.setPrintPos(95, 42);
  u8g.print(lightingState);

  if ((!hState) && (!aState) && (!lState)) {
    screenStndby();               // if all of controlled devices are in off, call standby screen
  }
}

void screenStndby() {             // standby scr
  u8g.firstPage(); 
    do {
      u8g.setFontRefHeightText();
      u8g.setFont(u8g_font_unifont);
      if (!measError) {
        u8g.setPrintPos(33, 52);
        u8g.print(funct_str[5]);
        u8g.setPrintPos( 8, 64);
        u8g.print(funct_str[6]);
      }
      else {
        u8g.setPrintPos( 4, 48);
        u8g.print(funct_str[4]);
      }
      temprWriteOut(0, 16) ;
    } while( u8g.nextPage() );
}

void temprWriteOut (byte tX, byte tY) {   // draw tempr & tTarget variables onto different coordinates
  u8g.setFont(u8g_font_courB14);
//u8g.setFont(u8g_font_6x12);             // you can save ~10% of prog.memory using this font with 2x2 scale
  char buftTarget[9];                   
  sprintf (buftTarget, "%d", tTarget);    // int to char
//u8g.setScale2x2();
//tY = tY / 2;
  u8g.setPrintPos(tX, tY); 
  u8g.print(buftTarget);
  u8g.setPrintPos(tX+18, tY); 
  u8g.print(funct_str[9]);
  u8g.setPrintPos(tX+50, tY);           
  u8g.print(tempr);                     
//u8g.print(char(176)); 
  u8g.print("F");
//u8g.undoScale();
}

void clrScr(){
  u8g.firstPage(); 
    do {
    } while( u8g.nextPage() );
}

void ledHandler() {                     // the brightness of a led is low, if the indicated device is off, and high, if its on
  if (aState) {
    analogWrite(ledA, bright[2]); 
  }
  else {
    analogWrite(ledA, bright[1]); 
  }
  if (lState)  {
    analogWrite(ledL, bright[2]); 
  }
  else {
    analogWrite(ledL, bright[1]); 
  }
  if (hState) {
    if (!hThermostat) {
      ledBlnk() ;                       // the heater led blinks when the One Time Heating (15 mins) timed mode is activated,
    }
    else {
      brightHeat = bright[2] ;          // and constant bright, if the thermostat routine is active
    }
  }  
  else {
    brightHeat = bright[1] ; 
  }
  analogWrite(ledH, brightHeat);
}

void ledBlnk() {
  unsigned long curr = millis();
  if (ledInterval <= curr - prev) {     // subtract prev value from current, if the difference equals or greater than ledInterval const.
    prev = curr;                        // overwrite the earlier value with the current and flip brightness level
    if (brightHeat == bright[1]) {      
      brightHeat = bright[2];
    } 
    else {
      brightHeat = bright[1];
    }
  }
  analogWrite(ledH, brightHeat);
}

void buzz(byte b, byte d) {              // call with frequency and delay parameters
  tone(buzzer, b * 1000); 
  delay(d * 100);        
  noTone(buzzer);
}

void sTX(byte reportTX) {              // sending serial reports
  switch (reportTX) {
    case 0:
      for (byte i = 0; i < 9; i++) {
        sUART.print(funct_str[10]);
      }
      sUART.println(funct_str[10]);
      break;
    case 1: 
      sUART.print(funct_str[8]);   // Tempr.
      sUART.print(tempr); 
    //sUART.print(char(176)); 
      sUART.println("F");           
      break;
    case 2:
      sUART.print(funct_str[7]);   // TTemp
      sUART.print(tTarget); 
    //sUART.print(char(176)); 
      sUART.println("F");  
      break;
    case 3:
      sUART.print(funct_str[3]);   // Window Alert
      sUART.print(funct_str[9]);  
      sUART.print(tempr); 
    //sUART.print(char(176)); 
      sUART.println("F");      
      break;
    case 4:
      sUART.println(funct_str[4]); // Error report        
      break;
    case 5:
      sUART.print(funct_str[0]);   // Working state of devices
      sUART.println(heaterState);      
      break;
    case 6:
      sUART.print(funct_str[1]); 
      sUART.println(airingState);         
      break;
    case 7:
      sUART.print(funct_str[2]);
      sUART.println(lightingState);         
      break;
    case 8:                        // Overview report
      sTX(0);
      relayHandlerH(4);            
      relayHandlerA(4);
      relayHandlerL(4);
      sTX(2);
      if (measError) {
        sTX(4);
      }
      break;
  }
}

Credits

Gyula Ősi
4 projects • 33 followers

Comments