   pinMode(STATUS_LED, OUTPUT);
 
   WiFi.hostByName(DESTINATION_HOST, serverIP); // resolve name
+
+  // init header
+  init_header();
 }
 
 void send() {
   // Serial.println("sending UDP packet...");
+  // build package
+  byte *msg = message + BUFFER_HEADER_SIZE;
+  for( int i=0; i<buttonsAllocated; i++ ) {
+    *msg = buttonLastState[i]?2:1;
+    msg ++;
+    *msg = buttonCode[i];
+    msg ++;
+  }   // TODO eventually check overflow
+
   udp.beginPacket(serverIP, DESTINATION_PORT);
-  udp.write(message, sizeof(message));
+  udp.write(message, BUFFER_HEADER_SIZE + buttonsAllocated*2);
   udp.endPacket();
 }
 

  }
}

void setButton(int nr, boolean isPressed) {
    int byteNr = nr / 8;
    int bitNr = nr % 8;
    
    if( isPressed ) {
        message[byteNr] |= 1 << bitNr;
    } else {
        message[byteNr] &= 255 - (1<<bitNr);
    }
}

bool getButton( int nr ) {
    int byteNr = nr / 8;
    int bitNr = nr % 8;

    return (message[byteNr] & (1<<bitNr))>0;
}
    
bool checkButton( int buttonIndex ) {
    if(digitalRead( buttonPin[buttonIndex] ) == (buttonPullup[buttonIndex]?LOW:HIGH)) {
        if(buttonThresCounter[buttonIndex] < BUTTON_THRES)

#include <ESP8266WiFi.h>
#include <lwip/ip.h> // for endian conversion htons, htonl, ...
#include <WiFiUdp.h>

//const char* ssid     = "tulix-mobile";
const char* ssid     = "tulix-upstairs";
const char* password = "vivalasalsa07";

const char * DESTINATION_HOST = "ulno-work";
//const char* DESTINATION_HOST = "192.168.43.1";
//const char* DESTINATION_HOST = "192.168.15.167"; // asus phone
//const char* DESTINATION_HOST = "192.168.15.168"; // note 3 phone
//const char* DESTINATION_HOST = "192.168.15.198"; // fire stick
//const char* DESTINATION_HOST = "localhost";
//const char* DESTINATION_HOST = "192.168.15.194";
int DESTINATION_PORT = 19877;

const char *MAGIC = "GNCT"; // magic identifier for Game Network Controller
const int PROTOCOL_VERSION = 1;
const int NUMBER_OF_BUTTONS = 256;
const int NUMBER_OF_BUTTON_BYTES = (NUMBER_OF_BUTTONS + 7)/8;
const int NUMBER_OF_AXIS = 16;
const int NUMBER_OF_AXIS_BYTES = NUMBER_OF_AXIS * 2;
const int MAX_BUFFER_SIZE = 128;
const int BUFFER_HEADER_SIZE = 16;
byte message[MAX_BUFFER_SIZE];
const int MAX_NUMBER_OF_BUTTONS = 16;
const int BUTTON_THRES = 16; // how many times has an on be counted to assume this button is on
//const int STATUS_LED = 2;
