 }
 
 void callback() {
-  
-      if ( buttonState ) {
-       Serial.println("Sending heartbeat (on)");               
+    if ( buttonState ) {
+       Serial.println("Sending heartbeat (on)");    
+       send_packet( "on" );       
        sentPacket = 1;
-       
-       char buffer[12];
-       strcpy(packetBuffer,(char*)pgm_read_word(&loopPacket1) );
-       strcat(packetBuffer, itoa( requestID, buffer, 10) );
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket2) );
-       for (byte thisByte = 0; thisByte < 6; thisByte++) {
-         sprintf(buffer, "%x", mac[thisByte] );
-         strcat(packetBuffer, buffer); 
-       }   
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket3) );
-       for (byte thisByte = 0; thisByte < 6; thisByte++) {
-         sprintf(buffer, "%x", mac[thisByte] );
-         strcat(packetBuffer, buffer); 
-       }   
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket4) );
-       strcat(packetBuffer, "on");
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket5) );
-       strcat(packetBuffer, itoa( millis(), buffer, 10) );
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket6) );
-
-       Serial.println(packetBuffer); 
-       udp.beginPacket(udp.remoteIP(), udp.remotePort());
-       udp.write(packetBuffer);
-       udp.endPacket();       
-       requestID = requestID + 1;
-      
     } else {
        Serial.println("Sending heartbeat (off)");               
+       send_packet( "off" );       
        sentPacket = 1;     
- 
-       char buffer[12];
-       strcpy(packetBuffer,(char*)pgm_read_word(&loopPacket1) );
-       strcat(packetBuffer, itoa( requestID, buffer, 10) );
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket2) );
-       for (byte thisByte = 0; thisByte < 6; thisByte++) {
-         sprintf(buffer, "%x", mac[thisByte] );
-         strcat(packetBuffer, buffer); 
-       }   
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket3) );
-       for (byte thisByte = 0; thisByte < 6; thisByte++) {
-         sprintf(buffer, "%x", mac[thisByte] );
-         strcat(packetBuffer, buffer); 
-       }   
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket4) );
-       strcat(packetBuffer, "off");
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket5) );
-       strcat(packetBuffer, itoa( millis(), buffer, 10) );
-       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket6) );
-
-       Serial.println(packetBuffer); 
-       udp.beginPacket(udp.remoteIP(), udp.remotePort());
-       udp.write(packetBuffer);
-       udp.endPacket();      
-       requestID = requestID + 1;
-       
    }
 }
   
+void send_packet( char * state ) {
+  
+   char buffer[12];
+   strcpy(packetBuffer,(char*)pgm_read_word(&loopPacket1) );
+   strcat(packetBuffer, itoa( requestID, buffer, 10) );
+   strcat(packetBuffer,(char*)pgm_read_word(&loopPacket2) );
+   for (byte thisByte = 0; thisByte < 6; thisByte++) {
+     sprintf(buffer, "%x", mac[thisByte] );
+     strcat(packetBuffer, buffer); 
+   }   
+   strcat(packetBuffer,(char*)pgm_read_word(&loopPacket3) );
+   for (byte thisByte = 0; thisByte < 6; thisByte++) {
+     sprintf(buffer, "%x", mac[thisByte] );
+     strcat(packetBuffer, buffer); 
+   }   
+   strcat(packetBuffer,(char*)pgm_read_word(&loopPacket4) );
+   strcat(packetBuffer, state);
+   strcat(packetBuffer,(char*)pgm_read_word(&loopPacket5) );
+   strcat(packetBuffer, itoa( millis()/1000, buffer, 10) );
+   strcat(packetBuffer,(char*)pgm_read_word(&loopPacket6) );
+
+   Serial.println(packetBuffer); 
+   udp.beginPacket(udp.remoteIP(), udp.remotePort());
+   udp.write(packetBuffer);
+   udp.endPacket();       
+   requestID = requestID + 1;
+       
+}
+
   
 

    //Serial.print( "Button state = " );
    //Serial.println( buttonState );
    if ( buttonState && !sentPacket ) {
       Serial.println("Sending contact = on");               
       sentPacket = 1;
       
       char buffer[12];
       strcpy(packetBuffer,(char*)pgm_read_word(&loopPacket1) );
       strcat(packetBuffer, itoa( requestID, buffer, 10) );
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket2) );
       for (byte thisByte = 0; thisByte < 6; thisByte++) {
         sprintf(buffer, "%x", mac[thisByte] );
         strcat(packetBuffer, buffer); 
       }   
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket3) );
       for (byte thisByte = 0; thisByte < 6; thisByte++) {
         sprintf(buffer, "%x", mac[thisByte] );
         strcat(packetBuffer, buffer); 
       }   
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket4) );
       strcat(packetBuffer, "on");
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket5) );
       strcat(packetBuffer, itoa( millis(), buffer, 10) );
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket6) );

       Serial.println(packetBuffer); 
       udp.beginPacket(udp.remoteIP(), udp.remotePort());
       udp.write(packetBuffer);
       udp.endPacket();       
       requestID = requestID + 1;
      
    } else if ( !buttonState && !sentPacket ) {
       Serial.println("Sending contact = off");               
       sentPacket = 1;     
 
       char buffer[12];
       strcpy(packetBuffer,(char*)pgm_read_word(&loopPacket1) );
       strcat(packetBuffer, itoa( requestID, buffer, 10) );
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket2) );
       for (byte thisByte = 0; thisByte < 6; thisByte++) {
         sprintf(buffer, "%x", mac[thisByte] );
         strcat(packetBuffer, buffer); 
       }   
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket3) );
       for (byte thisByte = 0; thisByte < 6; thisByte++) {
         sprintf(buffer, "%x", mac[thisByte] );
         strcat(packetBuffer, buffer); 
       }   
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket4) );
       strcat(packetBuffer, "off");
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket5) );
       strcat(packetBuffer, itoa( millis(), buffer, 10) );
       strcat(packetBuffer,(char*)pgm_read_word(&loopPacket6) );

       Serial.println(packetBuffer); 
       udp.beginPacket(udp.remoteIP(), udp.remotePort());
       udp.write(packetBuffer);
       udp.endPacket();      
       requestID = requestID + 1;
       
   }
    
  }
  
  if ( lastButtonState != reading ) {

 
  udp.beginMulti(ip,port);

}

void loop() {
