#include <WiFiNINA.h>
#include <Stepper.h>

// Pour arduino Uno wifi rev2
// et moteur 28BYJ-48 et son driver ULN2003A

// Définit le nombre de pas par rotation:
const int stepsPerRevolution = 2048;

// Câblage:
// Broche 8 à IN1 sur le pilote ULN2003
// Broche 9 à IN2 sur le pilote ULN2003
// Broche 10 à IN3 sur le pilote ULN2003
// Broche 11 à IN4 sur le pilote ULN2003
// Vcc driver sur +5 V Arduino
// VM  driver sur 5 V externe
// relier GND driver/Arduino/alim externe

// Créez un objet stepper appelé 'myStepper', notez l'ordre des broches:
Stepper myStepper = Stepper ( stepsPerRevolution, 8, 10, 9, 11 ) ;

// infos de connexion a remplir
char ssid[] = "XXXXXXX";    //  your network SSID (name) between the " "
char pass[] = "YYYYYY";      // your network password between the " "

int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;      //connection status
WiFiServer server(80);            //server socket

WiFiClient client = server.available();

void setup() {
  Serial.begin(9600);
  myStepper.setSpeed(3);
  while (!Serial);
  enable_WiFi();
  connect_WiFi();
  server.begin();
  printWifiStatus();
}

void loop() {
  client = server.available();
  if (client) {
    printWEB();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");

  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void enable_WiFi() {
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }
}

void connect_WiFi() {
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 6 seconds for connection:
    delay(6000);
  }
}

void printWEB() {

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // retirer soulignement liens
            client.println("<style> a{text-decoration:none;}</style> " );

            //create the buttons
            // 1 pas a la fois
             client.print("<font size = '7'>&emsp;&emsp;&emsp;&emsp;&emsp;<a href=\"/AR\"><</a></font>");
             client.print("<font size = '7'>&emsp;</font>");
             client.print("<font size = '7'> <a href=\"/AV\">&nbsp;></a></font>");                          
             client.print("<br><br>");
             
            // mvt rapide 10 pas a la fois
             client.print("<font size = '7'>&emsp;&emsp;&emsp;&emsp;<a href=\"/ARR\"><<<  </a></font>");
             client.print("<font size = '7'>&emsp;</font>");
             client.print("<font size = '7'> <a href=\"/AVR\">>>></a></font>");
             client.print("<br><br>");
                           

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        if (currentLine.endsWith("GET /AV")) {
             myStepper.step(1);
             delay(10);
                     }
        if (currentLine.endsWith("GET /AR")) {
             myStepper.step(-1);
             delay(10);
        }
        if (currentLine.endsWith("GET /AVR")) {
             myStepper.step(10);
             delay(10);
        }
        if (currentLine.endsWith("GET /ARR")) {
             myStepper.step(-10);
             delay(10);     
        }

      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}