 
 void setup() {
   Serial.begin(57600);
-  
+
   // Init the Ethernet shield
   Ethernet.begin(mac);
-  
+
   // Connect!
   if (!client.connect(hostname, port))
     Serial.println("Not connected.");
-  
+  else
+    Serial.println("Connected.");
+
   // Catch any received data
   client.setDataArrivedDelegate(onData);
-  
+
   // Second handshake
   client.send("Say welcome");
 }
 
 void loop() {
+  smartCheck();
   client.monitor();
 }
 
 // Websocket message handler
 void onData(SocketIOClient client, char *data) {
   int lightNumber;
-  
+
   // Light 1
-  if(strstr(data, "1")) {
+  if (strstr(data, "1")) {
     lightNumber = 0;
   }
-  
+
   // Light 2
-  if(strstr(data, "2")) {
+  if (strstr(data, "2")) {
     lightNumber = 1;
   }
-  
+
   // Light 3
-  if(strstr(data, "3")) {
+  if (strstr(data, "3")) {
     lightNumber = 2;
   }
-  
+
   // Light 4
-  if(strstr(data, "4")) {
+  if (strstr(data, "4")) {
     lightNumber = 3;
   }
-  
+
   // Turn on the light
-  if(strstr(data, "on")) {
+  if (strstr(data, "on")) {
     transmitter.sendUnit(lightNumber, true);
   } else { // Turn off
     transmitter.sendUnit(lightNumber, false);
   }
 }
 
-void timeUpdate(char *timeString)
-{
+// Check for turing on the light using the smart function
+void smartCheck() {
+  Serial.println("Check");
+  short currentLightValue = analogRead(ldrPin);
+  if (currentLightValue <= lightThreshold) {
+    Serial.println("On");
+    transmitter.sendGroup(true);
+    transmitter.sendUnit(0, true);
+    transmitter.sendUnit(1, true);
+    transmitter.sendUnit(2, true);
+    transmitter.sendUnit(3, true);
+  }
+
+}
+
+void updateSmartValues() {
+  short onTimeMinutes;
+  for (int i = 0; i < numValues; i++) {
+    onTimeMinutes += minutes[i];
+    lightThreshold += lightThresholds[i];
+  }
+  onTimeMinutes /= numValues;
+  lightThreshold /= numValues;
+}
+
+// Sets the current date to the data in the string
+// The used format is "hours,minutes,seconds,day,month,year"
+void timeUpdate(char *timeString) {
   char *p = timeString;
   char *str;
   byte second, minute, hour, day, month;

const int transmitterPin = 7;
const int transmitterPeriod = 254;

NewRemoteTransmitter transmitter(transmitterAddress, transmitterPin, transmitterPeriod);

// Ethernet details

#include "SocketIOClient.h"
#include "Ethernet.h"
#include "SPI.h"
#include <Time.h>
#include <string.h>

// Used for the KAKU remote functionality
