     if (Debugging) {
       debugger.println("Fan off.");
     }
-    mqtt.publish(pStatus, "fan|off");
+    if (wifiConnected) {
+      mqtt.publish(pStatus, "fan|off", 2, 0);
+    }
   }
   else {
     digitalWrite(fan, HIGH);

void loop() {
  esp.process();

  // Only fetch new sensor data if it's been long enough since the last reading.
  if ((unsigned long)(millis() - readingInterval) >= lastReading) {
    // Read new data from sensors
    readSensors();

    // Respond ot sensor data
    handleSensorReadings();
  }

  // Move the door as needed
  doorMove();

  // Update door LEDs as needed
  doorLEDs();

  // Update the LCD display
  updateLCD();
}

 * Initialization on startup.
 */
void setup() {
  debugger.begin(19200);
  if (Debugging) {
    debugger.println("Initialising...");
  }
  // Init LCD
  pinMode(lcd_bl, OUTPUT);
  digitalWrite(lcd_bl, HIGH);
  RTC.begin();
  lcd.begin(16,2);
  lcdWrite("Initializing...");

  espPort.begin(19200);
  lcdWrite("Initializing...", "WIFI Config");
  esp.enable();
  delay(500);
  esp.reset();
  delay(500);
  while(!esp.ready());
  
  if (Debugging) {
    debugger.println("ARDUINO: Setup MQTT client");
  }
  lcdWrite("Initializing...", "MQTT Config");
  if (!mqtt.begin(mClientID, mUsername, mPassword, 20, 1)) { // client_id, username, password, keepalive time, cleansession boolean
    if (Debugging) {
      debugger.println("ARDUINO: Failed to setup MQTT");
    }
    lcdWrite("MQTT Connect", "Failure");
    while(1);
  }
  mqtt.lwt("/lwt", "Offline");

  mqtt.connectedCb.attach(&mqttConnected);
  mqtt.disconnectedCb.attach(&mqttDisconnected);
