#include <WiFi.h>
const char* ssid = "ASUS SW2";
const char* password = "q1w2e3r4t5";
const char* host = "unicarlos.com";
const int httpPort = 80;
String url =  "http://unicarlos.com/2018/domotica/pdo/Arduino/guardarEntrada.php?escena=Escena1&entrada=";
WiFiClient client;


void iniciarWifi() {
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void conexionTCP(String parametro) {
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  String urlpar = url + parametro;
  // Esto envia la peticion al servidor
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  // lee todas las lineas recibidas del servidor
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print("line=" + line);
    u8g2.firstPage();
    do {
      u8g2.setFont(u8g2_font_ncenB14_tr);
      u8g2.drawStr(0, 24, "cvf");
      char s[30] = " ";
      //line.toCharArray(s, line.length());
      //  u8g2.drawStr(0, 48, s);
      u8g2.setCursor(0, 48);
      u8g2.print(line);
    } while ( u8g2.nextPage() );
  }
   Serial.println();
  Serial.println("closing connection");
}

