   }
 }
 
-// Process a complete serial line, and publish results to MQTT
-void handleSerial() {
-  char cleanChars[MAXCHARS];  //serialChars, with first 2 chars 'removed'
-  strncpy(cleanChars, serialChars, MAXCHARS);
-  for(int i=2; i < MAXCHARS; i++) {
-    cleanChars[i-2] = cleanChars[i];
-  }
-
-  // Check the first character, and process the packet based on that.
-  //Wind Update
-  if (serialChars[0] == 'W') {
-    MQTT_Helper.publishMQTT("home/roof/weather/wind", cleanChars, false);
-  }
-  //Temperature Update
-  if (serialChars[0] == 'T') {
-    MQTT_Helper.publishMQTT("home/roof/weather/temp", cleanChars, false);
-  }
-  //Humidity Update
-  if (serialChars[0] == 'H') {
-    MQTT_Helper.publishMQTT("home/roof/weather/humid", cleanChars, false);
-  }
-  //'Battery' Update
-  if (serialChars[0] == 'B') {
-    MQTT_Helper.publishMQTT("home/roof/weather/battery", cleanChars, true);
-  }
-  //Rain Flip
-  if (serialChars[0] == 'R') {
-    MQTT_Helper.publishMQTT("home/roof/weather/rain", serialChars, false);
-  }
+// Initial set up routines
+void setup() {
+  Serial.begin(9600);
+  dht.begin();
+  WiFi.mode(WIFI_STA);
+  wifi_station_set_hostname("ESP_Attic");
+  connectWifi();
+  MQTT_Helper.setup();
 }
 
-float getTemperature() {
-  return dht.readTemperature(fahrenheit);
+// Main program loop
+void loop() {
+  MQTT_Helper.mqttLoop();
+  checkTempHumid();
+  yield();
+  checkSerial();
+  delay(100); //saves considerable power & heat
 }
-
-float getHumidity() {
-  return dht.readHumidity();
-}

unsigned long lastTempHumidSend = millis();
#define TEMP_HUMID_UPDATE_FREQUENCY 60000

// Initial set up routines
void setup() {
  Serial.begin(9600);
  dht.begin();
  WiFi.mode(WIFI_STA);
  wifi_station_set_hostname("ESP_Attic");
  connectWifi();
  MQTT_Helper.setup();
}

// Main program loop
void loop() {
  MQTT_Helper.mqttLoop();
  checkTempHumid();
  yield();
  checkSerial();
  delay(100); //saves considerable power & heat
}

// Initial connection to WiFi
// We wait for 5 seconds to connect, but do not block on the connection.
void connectWifi() {
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  delay(5000);
}

// Send temp / humidity update if need be

    MQTT_Helper.publishMQTT("home/attic/controller/temp",  temp,  false);
    MQTT_Helper.publishMQTT("home/attic/controller/humid", humid, false);
    lastTempHumidSend = millis();
  }
}

// Process a complete serial line, and publish results to MQTT
void handleSerial() {
  char cleanChars[MAXCHARS];  //serialChars, with first 2 chars 'removed'
  strncpy(cleanChars, serialChars, MAXCHARS);
  for(int i=2; i < MAXCHARS; i++) {
    cleanChars[i-2] = cleanChars[i];
  }

  // Check the first character, and process the packet based on that.
  //Wind Update
  if (serialChars[0] == 'W') {
    MQTT_Helper.publishMQTT("home/roof/weather/wind", cleanChars, false);
  }
  //Temperature Update
  if (serialChars[0] == 'T') {
    MQTT_Helper.publishMQTT("home/roof/weather/temp", cleanChars, false);
  }
  //Humidity Update
  if (serialChars[0] == 'H') {
    MQTT_Helper.publishMQTT("home/roof/weather/humid", cleanChars, false);
  }
  //'Battery' Update
  if (serialChars[0] == 'B') {
    MQTT_Helper.publishMQTT("home/roof/weather/battery", cleanChars, true);
  }
  //Rain Flip
  if (serialChars[0] == 'R') {
    MQTT_Helper.publishMQTT("home/roof/weather/rain", serialChars, false);
  }
}

