This boost console is made by PiHome Smart Heating user and code contributor Terry Adam and he agreed to share his version of boost console. This boost console uses a 2.4inch ILI9341 based TFT screen. It also acts as a local thermometer, but displays the zone control thermometer temperature and the zone target temperature. Also shows the CE and HW on/off state, when on the red square is filled if the control relay is on, else it is not filled. Two buttons to action HW and CE boost.
Php script run as a cron job every minute, it writes 3 entries into the messages_out table, the console has a node_id of 50, child_id 1 is the central heating zone target temperature, child_id 2 is the zone temperature and finally child_id 3 is a 6bit field used to control the display legends. At the top of the script I identify the names as set in the zone table, for the Central Heating, Hot Water and location of the console (Kitchen in my case).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
// _____ _ _ _ // | __ \ (_) | | | | // | |__) | _ | |__| | ___ _ __ ___ ___ // | ___/ | | | __ | / _ \ | |_ \_ \ / _ \ // | | | | | | | | | (_) | | | | | | | | __/ // |_| |_| |_| |_| \___/ |_| |_| |_| \___| // // S M A R T H E A T I N G C O N T R O L // ***************************************************************** // * OneWire DS18B20 Temperature Sensor * // * Version 0.1 Build Date 29/11/2018 * // * Have Fun - PiHome.eu * // ***************************************************************** // Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 // Set LOW transmit power level as default, if you have an amplified NRF-module and // power your radio separately with a good regulator you can turn up PA level. // #define MY_RF24_PA_LEVEL RF24_PA_LOW #define MY_RF24_PA_LEVEL RF24_PA_MAX //#define MY_DEBUG_VERBOSE_RF24 // RF channel for the sensor net, 0-127 #define MY_RF24_CHANNEL 91 //PiHome - Make Sure you change Node ID. #define MY_NODE_ID 50 //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps #define RF24_DATARATE RF24_250KBPS int xpos; int ypos; const int col_1 = 10; const int col_2 = 85; const int col_3 = 200; const int col_4 = 280; const int row_1 = 40 - 20; const int row_2 = 80 - 20; const int row_3 = 140 - 20; const int row_4 = 180 - 20; const int row_5 = 235 - 20; #define ILI9341_GREY 0x5AEB // New colour #include <SPI.h> #include <DallasTemperature.h> #include <OneWire.h> // #include <Bounce2.h> #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" // Enable Soft SPI for NRF radio (note different radio wiring is required) // The ILI9341 TFT module seems to have a hard time co-operate with // radio on the same spi bus. #define MY_SOFTSPI #define MY_SOFT_SPI_SCK_PIN 14 #define MY_SOFT_SPI_MISO_PIN 16 #define MY_SOFT_SPI_MOSI_PIN 15 // When TFT is connected we have to move CE/CSN pins for NRF radio #ifndef MY_RF24_CE_PIN #define MY_RF24_CE_PIN 5 #endif #ifndef MY_RF24_CS_PIN #define MY_RF24_CS_PIN 6 #endif #include <MySensors.h> // For the Adafruit shield, these are the default. #define TFT_RST 8 #define TFT_DC 7 #define TFT_CS 10 #define TFT_MOSI 11 #define TFT_CLK 13 #define TFT_MISO 12 // Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC //Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); // If using the breakout, change pins as desired Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO); #define ledpin 4 // Test LED for one Blink Power On, Solid LED for No sensors, 5 Blinks for no Radio (this settings is in MySensors.cpp) and three blinks for low battery // Define sensor node childs #define CHILD_ID_TEMP 0 // Define button node childs #define CHILD_ID_BUTTON1 4 #define CHILD_ID_BUTTON2 5 #define CHILD_ID_BUTTON3 6 #define BUTTON1_PIN 2 // Arduino Digital I/O pin for button/reed switch #define BUTTON2_PIN 3 // Arduino Digital I/O pin for button/reed switch #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 56000; // Sleep time between reads (in milliseconds) unsigned long WAIT_TIME = 30000; // Dallas Temperature related init OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; bool receivedConfig = false; bool metric = true; // variables will change: volatile int BoostCEState; // variable for reading the pushbutton status volatile int BoostHWState; // variable for reading the pushbutton status volatile int Old_BoostCEState; // variable for reading the pushbutton status volatile int Old_BoostHWState; // variable for reading the pushbutton status String Old_status_msg; // variable for the last status_msg MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgButton1(CHILD_ID_BUTTON1,V_TRIPPED); MyMessage msgButton2(CHILD_ID_BUTTON2,V_TRIPPED); MyMessage msgButton3(CHILD_ID_BUTTON3,V_TRIPPED); void before(){ // Startup up the OneWire library sensors.begin(); } void setup(){ #ifdef MY_DEBUG Serial.println("Start setup"); #endif noInterrupts(); // disable Interrupts //This is LED pin set to output and turn it on for short while pinMode(ledpin, OUTPUT); digitalWrite(ledpin, HIGH); delay(60); digitalWrite(ledpin, LOW); #ifdef MY_DEBUG Serial.println("Start request temp"); #endif // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); // needed for battery soc // use the 1.1 V internal reference #if defined(__AVR_ATmega2560__) analogReference(INTERNAL1V1); #else analogReference(INTERNAL); #endif #ifdef MY_DEBUG Serial.println("Start ILI9341 Setup"); #endif xpos = col_1; ypos = row_1; tft.begin(); tft.setRotation(1); tft.fillScreen(ILI9341_GREY); tft.setTextColor(ILI9341_BLUE); tft.setTextSize(3); tft.setCursor(xpos, ypos); // Set cursor near top left corner of screen // tft.setFreeFont(FSB18); // Select Free Serif 24 point font // tft.println(); // Move cursor down a line tft.print("Set"); // Print the font name onto the TFT screen tft.setCursor(col_2, ypos); tft.print("Temp"); xpos = tft.width() - 40; tft.setCursor(xpos, ypos); tft.print("C"); ypos = row_2; xpos = col_1; tft.setCursor(xpos, ypos); tft.print("Hall"); tft.setCursor(col_2, ypos); tft.print("Temp"); xpos = tft.width() - 40; tft.setCursor(xpos, ypos); tft.print("C"); ypos = row_3; xpos = col_1; tft.setCursor(xpos, ypos); tft.print("CE"); ypos = row_4; xpos = col_1; tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_BLUE); tft.print("HW"); for(int x = 200; x < 203; x++){ tft.drawLine( 0, x, tft.width(), x, ILI9341_BLACK); } ypos = row_5; xpos = col_1; tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("BOOST"); tft.setCursor(xpos + 160, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("HW"); tft.setCursor(xpos + 250, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("CE"); // Setup the button #ifdef MY_DEBUG Serial.println("Start Button Setup"); #endif // Activate internal pull-up pinMode(BUTTON1_PIN,INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON1_PIN), isrB1, FALLING); // Activate internal pull-up pinMode(BUTTON2_PIN,INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON2_PIN), isrB2, FALLING); interrupts(); // enable Interrupts BoostCEState = 0; BoostHWState = 0; Old_BoostCEState = 0; Old_BoostHWState = 0; wait(5000); Old_status_msg = ""; send(msgButton3.set(1)); // initialize the set temp and status #ifdef MY_DEBUG Serial.println("End setup"); #endif } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Kitchen Console", "1.0"); // Fetch the number of attached temperature sensors numSensors = sensors.getDeviceCount(); //Blink LED as number of sensors attached blink_led(numSensors, ledpin); //check if attached sensors number is grater then 0 if no then put led on solid #if numSensors > 0 digitalWrite(ledpin, HIGH); #else digitalWrite(ledpin, LOW); #endif // Present all sensors to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } // Present the buttons to the controller present(CHILD_ID_BUTTON1, S_DOOR); present(CHILD_ID_BUTTON2, S_DOOR); present(CHILD_ID_BUTTON3, S_DOOR); } void loop(){ // Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // query conversion time and sleep until conversion completed int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution()); //sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater) wait(conversionTime); // Read temperatures and send them to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; // Only send data if temperature has changed and no error #if COMPARE_TEMP == 1 if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { #else if (temperature != -127.00 && temperature != 85.00) { #endif // Send in the new temperature send(msgTemp.setSensor(i).set(temperature,1)); // Save new temperatures for next compare lastTemperature[i]=temperature; } } if (BoostCEState != Old_BoostCEState) { Old_BoostCEState = BoostCEState; send(msgButton1.set(BoostCEState==HIGH ? 1 : 0)); } if (BoostHWState != Old_BoostHWState) { Old_BoostHWState = BoostHWState; send(msgButton2.set(BoostHWState==HIGH ? 1 : 0)); } //go to sleep for while wait(WAIT_TIME); } //Blink LED function, pass ping number and number of blinks usage: blink_led(variable or number of time blink, ledpin); void blink_led(int count, int pin){ for(int i=0;i<count;i++){ digitalWrite(pin, HIGH); delay(700); digitalWrite(pin, LOW); delay(700); } } void receive(const MyMessage &message) { noInterrupts(); // disable Interrupts // We only expect one type of message from controller. But we better check anyway. if (message.type==V_STATUS) { // Change relay state // digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom - we dont need to save relay state as controller take care of this, // saveState(message.sensor, message.getBool()); // Message 1 is the Target (Current Schedule) temperature if (message.sensor==1) { tft.fillRect(col_3,10,70,40,ILI9341_GREY); tft.setTextColor(ILI9341_BLUE); ypos = row_1; xpos = col_3; tft.setCursor(xpos, ypos); tft.print(message.getFloat(),1); } // Message 2 is the Control (Hall) temperature if (message.sensor==2) { tft.fillRect(col_3,50,70,40,ILI9341_GREY); tft.setTextColor(ILI9341_BLUE); ypos = row_2; xpos = col_3; tft.setCursor(xpos, ypos); tft.print(message.getFloat(),1); } // Message 3 is the current Status indicators if (message.sensor==3) { String status_msg = message.getString(); // message.substring(0, 1)) = HW Flag // message.substring(1, 2)) = CE Flag // message.substring(2, 3)) = Boost HW Flag // message.substring(3, 4)) = Boost CE Flag // message.substring(4, 5)) = HW Active Flag // message.substring(5, 6)) = CE Active Flag if (status_msg != Old_status_msg) { Old_status_msg = status_msg; // Test CE and Boost CE bits to set ON/OFF text ypos = row_3; xpos = col_3; if(status_msg.substring(1, 2) == "1" || status_msg.substring(3, 4) == "1"){ tft.fillRect(col_3,110,80,40,ILI9341_GREY); // clear text area tft.fillRect(col_4,120,20,20,ILI9341_GREY); // clear active area tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_RED); tft.print("ON"); if(status_msg.substring(5, 6) == "1"){ tft.fillRect(col_4,120,20,20,ILI9341_RED); }else{ tft.drawRect(col_4,120,20,20,ILI9341_RED); tft.drawRect((col_4 + 1),(120 + 1),18,18,ILI9341_RED); } }else{ tft.fillRect(col_3,110,110,40,ILI9341_GREY); tft.fillRect(col_4,120,20,20,ILI9341_GREY); tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("OFF"); } // Test CE Boost State if(status_msg.substring(3, 4) == "0"){ if (BoostCEState == 1) { BoostCEState = 0; Old_BoostCEState = 0; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 250, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("CE"); } } else { if (BoostCEState == 0) { BoostCEState = 1; Old_BoostCEState = 1; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 250, ypos); tft.setTextColor(ILI9341_RED); tft.print("CE"); } } // Test HW and Boost HW bits to set ON/OFF text ypos = row_4; xpos = col_3; if(status_msg.substring(0, 1) == "1" || status_msg.substring(2, 3) == "1"){ tft.fillRect(col_3,150,80,40,ILI9341_GREY); // clear text aera tft.fillRect(col_4,160,20,20,ILI9341_GREY); // clear active area tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_RED); tft.print("ON"); if(status_msg.substring(4, 5) == "1"){ tft.fillRect(col_4,160,20,20,ILI9341_RED); }else{ tft.drawRect(col_4,160,20,20,ILI9341_RED); tft.drawRect((col_4 + 1),(160 + 1),18,18,ILI9341_RED); } }else{ tft.fillRect(col_3,150,80,40,ILI9341_GREY); tft.fillRect(col_4,160,20,20,ILI9341_GREY); tft.setCursor(xpos, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("OFF"); } // Test HW Boost State if(status_msg.substring(2, 3) == "0"){ if (BoostHWState == 1) { BoostHWState = 0; Old_BoostHWState = 0; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 160, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("HW"); } } else { if (BoostHWState == 0) { BoostHWState = 1; Old_BoostHWState = 1; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 160, ypos); tft.setTextColor(ILI9341_RED); tft.print("HW"); } } } } // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming Message: "); Serial.print(message.sensor); Serial.print(" - New Payload: "); Serial.println(message.getFloat()); #endif } interrupts(); // enable Interrupts } void isrB1 () { // Send in the new value if (BoostCEState == 0) { BoostCEState = 1; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 250, ypos); tft.setTextColor(ILI9341_RED); tft.print("CE"); } else { BoostCEState = 0; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 250, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("CE"); } // send(msgButton1.set(BoostCEState==HIGH ? 1 : 0)); } void isrB2 () { if (BoostHWState == 0) { BoostHWState = 1; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 160, ypos); tft.setTextColor(ILI9341_RED); tft.print("HW"); } else { BoostHWState = 0; ypos = row_5; xpos = col_1; tft.setCursor(xpos + 160, ypos); tft.setTextColor(ILI9341_BLACK); tft.print("HW"); } //send(msgButton2.set(BoostHWState==HIGH ? 1 : 0)); } |
Php Console Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
<?php #!/usr/bin/php echo "\033[36m"; echo "\n"; echo " _____ _ _ _ \n"; echo " | __ \ (_) | | | | \n"; echo " | |__) | _ | |__| | ___ _ __ ___ ___ \n"; echo " | ___/ | | | __ | / _ \ | |_ \_ \ / _ \ \n"; echo " | | | | | | | | | (_) | | | | | | | | __/ \n"; echo " |_| |_| |_| |_| \___/ |_| |_| |_| \___| \n"; echo " \033[0m \n"; echo " \033[45m S M A R T H E A T I N G C O N T R O L \033[0m \n"; echo "\033[31m"; echo "*******************************************************\n"; echo "* Console Script Version 0.01 Build Date 20/07/2019 *\n"; echo "* Update on 20/07/2019 *\n"; echo "* Have Fun - PiHome.eu *\n"; echo "*******************************************************\n"; echo " \033[0m \n"; require_once(__DIR__.'../../st_inc/connection.php'); require_once(__DIR__.'../../st_inc/functions.php'); //Set php script execution time in seconds ini_set('max_execution_time', 40); $date_time = date('Y-m-d H:i:s'); echo "\033[36m".date('Y-m-d H:i:s'). "\033[0m - Console Script Started \n"; $Display_Status = 0; //Controle, Hot Water and Console Node and Zone names $z_name[0] = "Ground Floor"; $z_name[1] = "Ch. Hot Water"; $z_name[2] = "Kitchen"; //Following variable set to current day of the week. $dow = idate('w'); //Find the node ID of the Central Heating Zone $query = "SELECT * FROM zone_view WHERE name = '$z_name[0]' LIMIT 1;"; $result = $conn->query($query); $heating_zone = mysqli_fetch_array($result); $z_id[0] = $heating_zone['id']; //Find the node ID of the Hot Water Zone $query = "SELECT * FROM zone_view WHERE name = '$z_name[1]' LIMIT 1;"; $result = $conn->query($query); $water_zone = mysqli_fetch_array($result); $z_id[1] = $water_zone['id']; //Find the node ID of the display console $query = "SELECT * FROM zone_view WHERE name = '$z_name[2]' LIMIT 1;"; $result = $conn->query($query); $console_zone = mysqli_fetch_array($result); $z_id[2] = $console_zone['id']; $console_sensors_id = $console_zone['sensors_id']; //Determine check holidays status $query = "SELECT * FROM holidays WHERE NOW() between start_date_time AND end_date_time AND status = '1' LIMIT 1"; $result = $conn->query($query); $rowcount=mysqli_num_rows($result); if ($rowcount > 0) { $holidays = mysqli_fetch_array($result); $holidays_status = $holidays['status']; }else { $holidays_status = 0; } //Loop for both zones for ($x = 0; $x <= 1; $x++) { //Get identification data for each Zone Controller $query = "SELECT * FROM zone_view where name = '$z_name[$x]' LIMIT 1;"; $results = $conn->query($query); $row = mysqli_fetch_array($results); $zone_id=$row['id']; $zone_sensor_id=$row['sensors_id']; $zone_sensor_child_id=$row['sensor_child_id']; //Get the last temperature from messages_in table if ($x == 0) { $query = "SELECT * FROM messages_in_view_24h WHERE node_id = {$zone_sensor_id} AND child_id = {$zone_sensor_child_id} ORDER BY datetime desc LIMIT 1;"; $result = $conn->query($query); $sensor = mysqli_fetch_array($result); $control_c = $sensor['payload']; $temp_reading_time = $sensor['datetime']; } //Check schedule for the zone if ($holidays_status == 0) { $query = "SELECT * FROM schedule_daily_time_zone_view WHERE CURTIME() between `start` AND `end` AND tz_status = 1 AND zone_name = '$z_name[$x]' AND time_status = '1' AND (WeekDays & (1 << {$dow})) > 0 AND holidays_id IS NULL LIMIT 1;"; } else { $query = "SELECT * FROM schedule_daily_time_zone_view WHERE CURTIME() between `start` AND `end` AND tz_status = 1 AND zone_name = '$z_name[$x]' AND time_status = '1' AND (WeekDays & (1 << {$dow})) > 0 AND holidays_id IS NOT NULL LIMIT 1;"; } $result = $conn->query($query); $rowcount=mysqli_num_rows($result); if ($rowcount > 0) { $schedule = mysqli_fetch_array($result); if (($schedule['WeekDays'] & (1 << $dow)) > 0) { if ($x == 0) { $Display_Status = $Display_Status | 0b010000; } else { $Display_Status = $Display_Status | 0b100000; } } if ($x == 0) { $target_c = $schedule['temperature']; } } else if ($x == 0) { unset($target_c); } //Check BOOST for Zone $query = "SELECT * FROM boost_view WHERE name = '$z_name[$x]' AND status = 1 LIMIT 1;"; $result = $conn->query($query); $rowcount=mysqli_num_rows($result); if ($rowcount > 0) { if ($x == 0) { $Display_Status = $Display_Status | 0b000101; } else { $Display_Status = $Display_Status | 0b001010; } } //Use zone_logs to determine current zone ON/OFF state $query = "SELECT * FROM zone_log_view WHERE zone_id = {$zone_id} AND status = 1 ORDER BY `boiler_log_id` desc LIMIT 1;"; $result = $conn->query($query); $zone_status = mysqli_fetch_array($result); $status = $zone_status['status']; $zone_start_datetime = strtotime($zone_status['start_datetime']); $zone_stop_datetime = strtotime($zone_status['stop_datetime']); $now=strtotime(date('Y-m-d H:i:s')); if ($x == 0) { if (($now >= $zone_start_datetime) && (empty($zone_stop_datetime))) { $Display_Status = $Display_Status | 0b000001; } } else { if (($now >= $zone_start_datetime) && (empty($zone_stop_datetime))) { $Display_Status = $Display_Status | 0b000010; } } } //End of For loop //Construct 6 digit binary string for messages_out entry // xxxxxx Bits SET for ON, Bits CLEAR for OFF // ||||||__ Hot Water Schedule ON/OFF // |||||___ Central Heating Schedule ON/OFF // ||||____ Hot Water Boost ON/OFF // |||_____ Central Heating Boost ON/OFF // ||_______ Hot Water Relay ON/OFF // |________ Central Heating Relay ON/OFF $Control_Bits = decbin($Display_Status); $Control_Bits = str_pad($Control_Bits, 6, '0', STR_PAD_LEFT); /******************************************************************************************************************************************************************** Update the Console Display by using the messages_out table /********************************************************************************************************************************************************************/ $update = 0; //used to set bits to indicate which row has been updated $query = "SELECT * FROM messages_out WHERE node_id ='{$console_sensors_id}' ORDER BY child_id ASC;"; $result = $conn->query($query); $rowcount=mysqli_num_rows($result); if ($rowcount == 0) { // no enties, so create all 3 for ($x = 0; $x <= 2; $x++) { switch ($x) { case 0: if (empty($target_c)) { $target_c = 0; } $payload = $target_c; break; case 1: if (empty($control_c)) { $control_c = 0; } $payload = $control_c; break; case 2: $payload = $Control_Bits; break; } $query = "INSERT INTO `messages_out`(`sync`, `purge`, `node_id`, `child_id`, `sub_type`, `ack`, `type`, `payload`, `sent`, `zone_id`) VALUES (0,0,'{$console_sensors_id}','{$x}' + 1,1,1,2,'{$payload}',1,'{$z_id[2]}');"; $conn->query($query); } } else { //Update any console records already prescent while( $row = mysqli_fetch_array($result)){ if ($row['child_id'] == 1) { $max_c = $row['payload']; if (empty($target_c)) { //no new active schedule temperature available $target_c = $max_c; } else if ($target_c != $max_c) { // update table if a new active schedule temperature is available $query = "UPDATE messages_out SET sent = '0', payload = '{$target_c}' WHERE zone_id ='{$z_id[2]}' AND child_id = '1' LIMIT 1;"; $conn->query($query); } $update = $update | 0b001; } else if ($row['child_id'] == 2) { //If control temperature has shanged then update the console display $c = $row['payload']; if ($control_c != $c) { $query = "UPDATE messages_out SET sent = '0', payload = '{$control_c}' WHERE zone_id ='{$z_id[2]}' AND child_id = '2' LIMIT 1;"; $conn->query($query); } $update = $update | 0b010; } else { $mask = $row['payload']; //If changed the update table if ($mask != $Control_Bits) { $query = "UPDATE messages_out SET sent = '0', payload = '{$Control_Bits}' WHERE zone_id ='{$z_id[2]}' AND child_id = '3' LIMIT 1;"; $conn->query($query); } $update = $update | 0b100; } } //End of while loop //Check and insert any missing console records for ($x = 0; $x <= 2; $x++) { if (($update & pow(2,$x)) == 0) { switch ($x) { case 0: if (empty($target_c)) { $target_c = 0; } $payload = $target_c; break; case 1: if (empty($control_c)) { $control_c = 0; } $payload = $control_c; break; case 2: $payload = $Control_Bits; break; } $query = "INSERT INTO `messages_out`(`sync`, `purge`, `node_id`, `child_id`, `sub_type`, `ack`, `type`, `payload`, `sent`, `zone_id`) VALUES (0,0,'{$console_sensors_id}','{$x}' + 1,1,1,2,'{$payload}',1,'{$z_id[2]}');"; $conn->query($query); } } } echo "---------------------------------------------------------------------------------------- \n"; echo "\033[36m".date('Y-m-d H:i:s'). "\033[0m - Control Temperature: \033[41m".$target_c."\033[0m \n"; echo "\033[36m".date('Y-m-d H:i:s'). "\033[0m - Zone Temperature: \033[41m".$control_c."\033[0m \n"; echo "\033[36m".date('Y-m-d H:i:s'). "\033[0m - Control Bits: \033[41m".$Control_Bits."\033[0m \n"; echo "---------------------------------------------------------------------------------------- \n"; echo "\033[36m".date('Y-m-d H:i:s'). "\033[0m - Console Script Ended \n"; echo "---------------------------------------------------------------------------------------- \n"; echo "\033[32m****************************************************************************************\033[0m \n"; if(isset($conn)) { $conn->close();} ?> |