//////////////////////////////     Import des librairies     \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  

#include <ESP8266WiFi.h>
#include <EasyButton.h>

//////////////////////////////     Config du  reseau     \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

WiFiClient client;                                    // Association du mode client
IPAddress local_IP(192, 168, 0, 1);                   // IP fixe
IPAddress gateway(192, 168, 0, 254);                  // Passerelle
IPAddress subnet(255, 255, 255, 0);                   // Masque

const char* ssid = "RubanLedWIFI";                    // SSID
const char* password = "123456789";                   // Mdp SSID

IPAddress ruban1(192,168,0,11);                       // l'adresse ip du ruban1
//IPAddress ruban2(192,168,0,22);                       // l'adresse ip du ruban2 

const uint16_t port = 80;                             // port de communication

//////////////////////////////     Config du bouton et variable    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

int commande = 0;                                     // Variable pour stocker la commande à envoyer 
const int BUTTON_PIN = 0;                             // Bouton sur gpio0 pin D3 
String nom = "";                                      // Variable pour stocker le nom du client
EasyButton button(BUTTON_PIN);                        // On construit le bouton

//////////////////////////////     La fonction onPressed    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

void onPressed() {
  commande++;                           // On incrémante commande a chaque appuye sur le bouton
  if (commande > 11) commande = 1;      // Si commande depasse 11, commande vaut 1 
  Serial.print("commande = ");          // On affiche la commande
  Serial.println(commande);
  rubans(1);                            // On lance la commande au ruban1
  //rubans(2);                            // On lance la commande au ruban2
}

//////////////////////////////     La fonction buttonLongPress    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

void buttonLongPress() { 
  commande = 0;                       // La commande vaut 100
  Serial.print("commande = ");          // On affiche la commande
  Serial.println(commande);
  rubans(1);                            // On lance la commande au ruban1
  //rubans(2);                            // On lance la commande au ruban2
}

//////////////////////////////     La fonction rubans    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

void rubans(int id){  
  if (id == 1) client.connect(ruban1, port);    // Si l'argument vaut 1 on se connect au ruban1
  //if (id == 2) client.connect(ruban2, port);    // Si l'argument vaut 2 on se connect au ruban2
  
  if (client.connected()) {                     // Si on est connecté
    client.println(commande);                   // On envoie la commande
    client.flush();                             // On vide le buffer
  }
  
}

//////////////////////////////     Le setup     \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

void setup() {
  delay(3000);                                                                     // Delay pour démarage
  Serial.begin(115200);                                                                  
  Serial.println();
  Serial.print("Configuration du réseau ");
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "OK" : "KO!");     // On contruit l'ip fixe
  Serial.print("Configuration du ssid et mdp ... ");
  Serial.println(WiFi.softAP(ssid, password) ? "OK" : "KO!");                      // On contruit le reseau
  Serial.print("Addresse IP du softAP : ");
  Serial.println(WiFi.softAPIP());
  Serial.println();
  button.begin();                                                                  // On initialise le bouton
  button.onPressed(onPressed);                                                     // On associe la fonction onPressed au bouton
  button.onPressedFor(2000, buttonLongPress);                                      // On associe la fonction buttonLongPress au bouton
  Serial.println("setup(): OK");                                                   // Fin du setup
}

//////////////////////////////     Le loop     \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

void loop () {
  button.read();         // On écoute le bouton en boucle
}