 
 #define DEBUG_RELAYS_UNIT 0
 
+#define VERSION_STRING "0.0.2"
+
+#define CRLF "\r\n"
+
+#define SIZE(array) (sizeof(array) / sizeof(*array))
+
 // CHANGE THIS TO YOUR OWN UNIQUE VALUE
 static uint8_t mac[6] = { 0xA9, 0x10, 0x14, 0xE7, 0xAC, 0xCC };  
 
 // CHANGE THIS TO MATCH YOUR HOST NETWORK
 static uint8_t ip[4] = { 192, 168, 2, 15 };
 
-/* all URLs on this server will start with /pins because of how we
- * define the PREFIX value.  We also will listen on port 80, the
- * standard HTTP service port */
-#define PREFIX "/pins"
+#define PREFIX ""
 WebServer webserver(PREFIX, 80);
 
 #define RELAY_ON  0
 #define RELAY_OFF 1
 
-const int relays_count = 7;
-const int relays [buttons_count]        = { 40, 42, 43, 44, 45, 46, 47 };
-const int relays_states [buttons_count] = { RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF };
+//const int relays_count = 7;
+const int relays[]        = { 40, 42, 43, 44, 45, 46, 47 };
+int relays_states[] = { RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF, RELAY_OFF };
+
+char* pin_number_as_char = "XX";
 
 /* store the HTML in program memory using the P macro */
-P(On) = "<status>ON</status>";
-P(Off) = "<status>OFF</status>";
+P(On)      = "<status>ON</status>";
+P(Off)     = "<status>OFF</status>";
+P(Hello) = 
+    "<html>"
+    "<head>"
+    "<title>Arduino Web Server</title>"
+    "</head>"
+    "<body>"
+    "<h1>Home Automation Web Server</h1>"
+    "based on <a href=\"https://github.com/sirleech/Webduino\">Webduino library</a><br/>"
+    "Vasily Klenov, 2014<br/>"
+    "<b>version</b>"
+    VERSION_STRING
+    "</body></html>";
 
-/* This command is set as the default command for the server.  It
- * handles both GET and PUT requests.  For a GET, it returns a pin
+void httpNotFound(WebServer &server){
+  P(failMsg) =
+    "HTTP/1.0 404 Bad Request" CRLF
+    "Content-Type: text/html" CRLF
+    CRLF
+    "<h2>File Not Found</h2>";
+  server.printP(failMsg);
+}  
+
+void helloCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
+{
+  server.httpSuccess();
+
+  /* if we're handling a GET or POST, we can output our data here.
+     For a HEAD request, we just stop after outputting headers. */
+  if (type != WebServer::HEAD)
+  {
+    server.printP(Hello);
+  }
+}
+
+/* It handles both GET and PUT requests.  For a GET, it returns a pin
  * value.  For a PUT it set the pin value. */
 void pinsCmd(WebServer &server, WebServer::ConnectionType type, char **url_path, char *url_tail, bool tail_complete)
 {
+
+  if(strncmp(url_path[0],"pins",5)!=0){
+    httpNotFound(server);
+    return;
+  }
+
+  char* pin_param = url_path[1];
+  int relay_index = find_index(pin_param);
+
   if (type == WebServer::PUT)
   {
-    #ifdef DEBUG_SWITCHER
+    #ifdef DEBUG_RELAYS_UNIT
       Serial.println("Processing PUT query...");
     #endif
 
     int new_state = RELAY_OFF;
-    int pin_number = atol(url_path[0]);
-    int relay_index = find_index(relays, relays_count, pin_number);
 
     if ( relay_index >= 0 )
     {
-      /* for url /pins/40/ON url_path[1] will contain 'ON' 
-         and  url_path[0] will contain '40' */
-      if(strcmp(url_path[1], "ON") == 0){
+      server.httpSuccess();
+      /* for url /pins/40/ON url_path[2] will contain 'ON' 
+         and  url_path[1] will contain '40' */
+      if(strcmp(url_path[2], "ON") == 0){ // поменять на strncmp ?????
         new_state = RELAY_ON;
       }
 
       if ( relays_states[relay_index] != new_state )
       {
-        digitalWrite(pin_number, new_state);
+        digitalWrite(relays[relay_index], new_state);
         relays_states[relay_index] = new_state;
       }
 
-      server.httpSuccess();
+      return;
+    } else
+    {
+      httpNotFound(server);
       return;
     }
   }

/* Home Automation Script
   based on Webduino library
   Vasily Klenov, 2014 */

#include "SPI.h"
#include "Ethernet.h"

  webserver.processConnection();
}

int find_index(char *value)
{
   for (int i=0; i<SIZE(relays); i++)
   {
     pin_number_as_char = itoa(relays[i], pin_number_as_char, 10); // itoa reference http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gaa571de9e773dde59b0550a5ca4bd2f00
     if (strcmp(pin_number_as_char, value) == 0)
     {
        return(i); 
     }
