   }
 
   // Sync RTC to time beacon once/day
-  if (topic == "time/beacon") {
+  if (topic == sTime) {
     if (lastRTCSync == 0 || ((unsigned long)(millis() - lastRTCSync) > millisPerDay)) {
       RTC.adjust(strtoul(data.c_str(), NULL, 0));
       lastRTCSync = millis();

const boolean Debugging = true;

// Digital & analog pins for various components
const int tempSense     =  2; // DS18S20 Temperature Sensor
const int lightSense    = A0; // Light Sensor
const int lampRelay     = 10; // Lamp Relay
const int heaterRelay   =  9; // Water Heater Relay
const int doorOpen      = 37; // MotorA left
const int doorClose     = 38; // MotorA right
const int doorSpeed     = 39; // MotorA speed
const int doorTop       = 40; // Reed Switch
const int doorBottom    = 41; // Reed Switch
const int doorOpenLED   = 11; // Indicator light (blinks during open/close steady on when open)
const int doorClosedLED = 12; // Indicator Light (blinks during open/close steady on when closed)
const int fan           = 35; // MotorB left
const int fanSpeed      = 36; // MotorB speed
const int rtcSDA        = 20; // Real Time Clock SDA (i2c arduino mega)
const int rtcSCL        = 21; // Real Time Clock SCL (i2c arduino mega)

// LCD Pins
const int lcd_4  = 24; // LCD RS
const int lcd_5  = 25; // LCD R/W
const int lcd_6  = 26; // LCD Enable
const int lcd_11 = 27; // LCD Data 4
const int lcd_12 = 28; // LCD Data 5
const int lcd_13 = 29; // LCD Data 6
const int lcd_14 = 30; // LCD Data 7
const int lcd_bl = 13; // LCD Backlight+

// ESP8266 Settings
#define debugger Serial // Pins 0 and 1


// Misc Settings
const unsigned long millisPerDay    = 86400000; // Milliseconds per day
const unsigned long debounceWait    =      200; // Door debounce timer (200 ms)
const unsigned long ledBlinkRate    =      250; // Delay between LED blinks (1/4 sec)
const unsigned long lcdChange       =     5000; // How often to change LCD pages (5 sec)
const unsigned long lightReadRate   =     1000; // How often to read light level (1 sec)
const unsigned long remoteOverride  =   600000; // Length of time to lockout readings. (10 min)
const unsigned long publishInterval =    60000; // How often to publish light/temp sensor readings. (1 min)
const int           fanThreshold    =       78; // When temperature (Farenheit) exceeds this threshold, turn on fan
const int           heaterThreshold =       36; // When temperature (Farenheit) is below this threshold, turn on water heater
const int           lsLow           =        0; // Light Sensor lowest reading
const int           lsHigh          =      900; // Light Sensor highest reading

// Night time lockout to prevent reaction to light sensor readings if an exterior light source causes
// a reading otherwise bright enough to activate the interior light and/or door.
const boolean       nightLock      =      true; // Enable night time lockout


/*************************************************
       DO   NOT   EDIT   BELOW   THIS   LINE
 *************************************************/
 
// Runtime variables
int           nightLockStart     =    22; // Hour (in 24hr time) to initiate night time lockout (10pm)
int           nightLockEnd       =     4; // Hour (in 24hr time) to end night time lockout (4am)
unsigned long lastPublish        =     0;
unsigned long lastDebounce       =     0;
unsigned long lastLightRead      =     0;
unsigned long lastRTCSync        =     0;
unsigned long lastLEDBlink       =     0;
unsigned long remoteLockStart    =     0;
unsigned long lastLCDChange      =     0;
String        doorState          =    ""; // Values will be one of: closed, closing, open, opening
String        doorStatePrev      =    "";
boolean       heaterState        = false;
boolean       lampState          = false;
boolean       fanState           = false;
float         tempC              =     0;
float         tempF              =     0;
int           brightness         =     0;
int           doorTopVal         =     0;
int           doorTopVal2        =     0;
int           doorTopState       =     0;
int           doorTopPrev        =     0;
int           doorBottomVal      =     0;
int           doorBottomVal2     =     0;
int           doorBottomState    =     0;
int           doorBottomPrev     =     0;
int           lcdPage            =     0; // Toggle between LCD page states
uint32_t      bootTime           =     0;

RTC_DS1307 RTC;
OneWire ds(tempSense);
LiquidCrystal lcd(lcd_4, lcd_6, lcd_11, lcd_12, lcd_13, lcd_14);

#if Debugging
  ESP esp(&espPort, &debugger, chpdPin);
#else
  ESP esp(&espPort, chpdPin);
#endif

MQTT mqtt(&esp);
boolean wifiConnected = false;
