 unsigned long stime = 1800000; // Zeit zwischen den Sendezeiten
 unsigned long mtime = 900000; // Zeit zwischen den Messungen
 
-boolean fanOn; // Boolean Feld fuer den Status vom Luefter.
+boolean isopen; // Boolean Feld fuer den Status vom Luefter.
+
+// Memory pool for JSON object tree.
+StaticJsonBuffer<200> jsonBuffer;
+JsonObject& root = jsonBuffer.createObject();
+
+void servo_ctrl(char cmd){
+  if(cmd=='open'){
+    if(!isopen){
+      // servo go to 90 degrees (open)
+      servo1.write(90);
+      isopen=true;
+      delay(1000);
+    } else {
+      // do nothing
+    }
+  } else {
+    if(isopen){
+      // servo go to 0 degrees (close)
+      servo1.write(0);
+      isopen=false;
+      delay(1000);
+    } else {
+      // do nothing
+    }
+  }
+}
+
+void connect_wifi(String cmd, int t){
+  int temp=0,i=0;
+  while(1)
+  {
+    Serial.println(cmd);
+    wifi.println(cmd);
+    while(wifi.available()) {
+      if(wifi.find("OK"))
+      i=8;
+    }
+    delay(t);
+    if(i>5)
+    break;
+    i++;
+  }
+  if(i==8)
+  Serial.println("OK");
+  else
+  Serial.println("Error");
+}
+
+void check_ip(int t1){
+  int t2=millis();
+  while(t2+t1>millis()){
+      while(wifi.available()>0){
+        if(wifi.find("WIFI GOT IP")){
+          NO_IP=true;
+        }
+      }
+  }
+}
+
+void get_ip(){
+  IP="";
+  char ch=0;
+  while(1){
+    wifi.println("AT+CIFSR");
+    while(wifi.available()>0){
+      if(wifi.find("STAIP,")){
+        delay(1000);
+        Serial.println("IP Address;");
+        while(wifi.available()>0){
+          ch=wifi.read();
+          if(ch=='+')
+          break;
+          IP+=ch;
+        }
+      }
+      if(ch=='+')
+      break;
+    }
+    if(ch=='+')
+    break;
+    delay(1000);
+  }
+  Serial.print(IP);
+  Serial.print("Port:");
+  Serial.println(80);
+}
+
+void wifi_init(){
+  connect_wifi("AT",100);
+  connect_wifi("AT+CWMODE=3",100);
+  connect_wifi("AT+CWQAP",100);
+  connect_wifi("AT+RST",5000);
+  check_ip(5000);
+  if(!NO_IP){
+    Serial.println("Connecting Wifi...");
+    connect_wifi("AT+CWJAP=\"tlapse\",\"timelapse\"",7000); //username,password,waittime
+  } else {
+  }
+  Serial.println("Wifi Connect");
+  get_ip();
+  connect_wifi("AT+CIPMUX=1",100);
+  connect_wifi("AT+CIPSERVER=1,80",100);
+}
+
 
 void setup() {
-  //Serial.begin(115200);
+  Serial.begin(115200);
+  wifi.begin(9600);
+  wifi_init();
+
+  root["ID"] = ID;
+  root["stime"] = stime;
+  root["mtime"] = mtime;
+
+  /*
   // VirtualWire Initialise the IO and ISR
-  
   vw_set_ptt_inverted(false); // Required for RF Link module
   vw_set_tx_pin(txPin);
   vw_setup(1200); //Bits pro Sekunde
+  */
+
+  // We need to attach the servo to the used pin number 
+  servo1.attach(servo_pin);
   
-  pinMode(out1, OUTPUT);
-  digitalWrite(out1, LOW);
-  fanOn = false;
+  
+  pinMode(servo_pin, OUTPUT);
+  digitalWrite(servo_pin, LOW);
   
   dht.setup(dhtPin);
   //Serial.println(F("setup end"));

  unsigned long currmillis = millis();

  //falls der Luefter laeuft pruefe alle 5 Sekunden die Werte
  if (fanOn) {
    if ((unsigned long)(currmillis - task2) >= 15000) {
      byte i;
      //Serial.println(F("check if hum"));
      if ((float)(get_hum() <= 65.0 || get_hum() <= 70.0)) {
        if (!fanOn) {
          //oeffne die Belueftung und starte den Luefter
          //Serial.println(F("fan ON"));
          digitalWrite(out1, HIGH);
          fanOn = true;
        }
      } else {
        if (fanOn) {
          // switch fan1 off und schliesse den Schlitz
          //Serial.println(F("fan off"));
          digitalWrite(out1, LOW);
          fanOn = false;
        }
      }
      task2 = millis();

    byte i;
    //Serial.println(F("check if hum"));
    if ((float)(get_hum() <= 65.0 || get_hum() <= 70.0)) {
      if (!isopen) {
        //oeffne die Belueftung und starte den Luefter
        //Serial.println(F("Switch fan on"));
        servo_ctrl("open");
      }
    } else {
      if (isopen) {
        // switch fan1 off und schliesse den Schlitz
        //Serial.println(F("Switch fan off"));
        servo_ctrl("close");
      }
    }
    task2 = millis();
