   digitalWrite(RED_LED, red_state);
 }
 
-///////////////////////////// IPAddress to string
-String ip2string(IPAddress& ip)
-{
-  String addr="";
-  for(int i=0;i<4;i++)
-  {
-    //addr+=ip[0]/100;
-  }
-  return addr;
-}
-
-void send(unsigned char b, unsigned char v) {
-  if(client.connected()) {
-    unsigned char data[12] = {'/','S','T','A','R','T',b,v, '/','E','N','D'};
-    client.write(data, sizeof(data));
-  }
-}
+
+void update(unsigned char button, boolean value) 
+{
+  if(button==PUSH1) {
+    aJsonObject* button1 = aJson.getObjectItem(frame, "button1");
+    button1->valuebool = value;
+  }
+  
+  if(button==PUSH2) {
+    aJsonObject* button2 = aJson.getObjectItem(frame, "button2");
+    button2->valuebool = value;
+  }
+}

volatile int green_state = LOW;

//access point should only work for b and g
char ssid[] = "nsynapse";
char password[] = "elec6887";
WiFiClient client;
WiFiServer server(8000);
WiFiUDP Udp;


    //////////////////////// service announce
    case ANNOUNCE: 
    {
      digitalWrite(YELLOW_LED, HIGH);
      if(announce)
      {
        Udp.beginPacket(DEFAULT_UDP_MEMBERSHIP, DEFAULT_UDP_PORT);
        Udp.write(aJson.print(announce));
        Udp.endPacket();
        process_state = SERVICE;
      }
    }
    break;
    
    
    //////////////////////// service run
    case SERVICE:
    { 
      WiFiClient client = server.available();
      if(client) {
        Serial.println("new client");
        while(client.connected()) {
          digitalWrite(RED_LED, HIGH);
          
          push1_state = digitalRead(PUSH1);
          push2_state = digitalRead(PUSH2);
          
          if(push1_state!=prev_push1_state) {
            if(push1_state==HIGH) {
              Serial.println("Push1 High");
              update(PUSH1, true);
            }
            else if(push1_state==LOW) {
              Serial.println("Push1 Low");
              update(PUSH1, false);
            }
            client.print(aJson.print(frame));
            delay(50);
          }
          
          if(push2_state!=prev_push2_state) 
          {
            if(push2_state==HIGH) {
              Serial.println("Push2 High");
              update(PUSH2, true);
            }
            else if(push2_state==LOW) {
              Serial.println("Push2 Low");
              update(PUSH2, false);
            }
            client.print(aJson.print(frame));
            delay(50);
          }
         
          
          
          prev_push1_state = push1_state;
          prev_push2_state = push2_state;
        }
        Serial.println("connection closed");
        digitalWrite(RED_LED, LOW);
        client.stop();
        
        if(WiFi.status()!=WL_CONNECTED) {
          digitalWrite(YELLOW_LED, LOW);
          process_state = IDLE;
        }
      }
    }
    break;
  }
}


