 #include <stdio.h>
 #include <SPI.h>
 #include <WiFi.h>
-#include <SPI.h>
 #include <WiFiUdp.h>
 #include <Time.h>
-
+#include <SD.h>
 
 // private info [made generic here]
 char ssid[] = "SSID";


// checking connection
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000; // wait 10 seconds
boolean canConnect = false;

// setting time
unsigned int localPort = 2390;      // local port to listen for UDP packets

  Udp.endPacket();
}

void get_and_set_time(void) {
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    unsigned long secsSince1900 = highWord << 16 | lowWord;

    const unsigned long seventyYears = 2208988800UL;
    unsigned long epoch = secsSince1900 - seventyYears;
    setTime(epoch);
}

void time_to_string(unsigned long epoch, char* time_as_string) {
    epoch = now() + TIMEZONE * 3600;
    sprintf(time_as_string, "%02d/%02d/%4d %d:%02d:%02d",
          month(epoch), day(epoch), year(epoch),
          hour(epoch), minute(epoch), second(epoch));
}

void open_file(void) {
  char filename[] = "log_****-11-11_12:59:59.csv";

  sprintf(filename, "log_%4d-%02d-%02d_%02d:%02d:%02d.csv",
          year(), month(), day(), hour(), minute(), second());

  logfile = SD.open(filename, FILE_WRITE);
}
