 
 // =============================================
 
-//#include <Adafruit_Sensor.h>
 #include <Wire.h>
 #include "RTClib.h"
 #include "DHT.h"
-#include <SD.h>
+#include "Fat16.h"
 
 #ifdef TRANSMITTER_WIFI
   #include <libmaple/iwdg.h>

  float heat_index;

  uint32_t current_time = rtc.now().unixtime();
  read_temperatures(&temperature_f, &humidity, &heat_index);
  log_to_sd(temperature_f, humidity, heat_index, current_time);
  watchdog_feed();
  
  transmit(temperature_f, humidity, heat_index, current_time);

  watchdog_feed();
  delay(READING_DELAY_MS);
}

void read_temperatures(float *temperature_f, float *humidity, float *heat_index) {
//  while (true) {
//    sensors_event_t event;
//    bool success = true;
//
//    dht.temperature().getEvent(&event);
//    
//    if (!isnan(event.temperature)) {
//      *temperature_f = dht_util.convertCtoF(event.temperature);
//    } else {
//      success = false;
//    }
//    
//    dht.humidity().getEvent(&event);
//
//    if (!isnan(event.temperature)) {
//      *humidity = event.relative_humidity;
//    } else {
//      success = false;
//    }
//    
//    if (success) {  
//      *heat_index = dht_util.computeHeatIndex(*temperature_f, *humidity);
//  
//      Serial.print("Temperature: ");
//      Serial.print(*temperature_f);
//      Serial.println(" *F");
//      
//      Serial.print("Humidity: ");
//      Serial.print(*humidity);
//      Serial.println("%");
//      
//      Serial.print("Heat index: ");
//      Serial.println(*heat_index);
//
//      return;
//      
//    } else {
//      Serial.println("Error reading temperatures!");
//    }
//  
//    delay(2000);
//
//    // if we continue to fail to read a temperature, the watchdog will
//    // eventually cause a reboot
//  }
}

void log_to_sd(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
  File data_file;
  
  if (data_file = SD.open("data.csv", FILE_WRITE)) {
    data_file.print(current_time); data_file.print(",");
    data_file.print(temperature_f); data_file.print(",");
    data_file.print(humidity); data_file.print(",");

#endif

#ifdef TRANSMITTER_GSM
  #include "Adafruit_FONA.h"
  #include <SoftwareSerial.h>
  #include <Adafruit_SleepyDog.h>

  #define DHT_DATA  A2
  #define DHT_VCC   A1         
