Electgpl

ESP - Servidor Web

Feb 17th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. //HTTP WebClient
  2. #include <ESP8266WiFi.h>
  3. const char* ssid     = "yourssid";
  4. const char* password = "yourpassword";
  5. const char* host = "www.adafruit.com";
  6. void setup() {
  7.   Serial.begin(115200);
  8.   delay(100);
  9. //Empezamos mediante la conexión a una red Wi-Fi
  10.   Serial.println();
  11.   Serial.println();
  12.   Serial.print("Connecting to ");
  13.   Serial.println(ssid);
  14.   WiFi.begin(ssid, password);
  15.   while (WiFi.status() != WL_CONNECTED) {
  16.     delay(500);
  17.     Serial.print(".");
  18.   }
  19.   Serial.println("");
  20.   Serial.println("WiFi connected");  
  21.   Serial.println("IP address: ");
  22.   Serial.println(WiFi.localIP());
  23. }
  24. int value = 0;
  25. void loop() {
  26.   delay(5000);
  27.   ++value;
  28.   Serial.print("connecting to ");
  29.   Serial.println(host);
  30. // Utiliza la clase de cliente WiFi para crear conexiones TCP
  31.   WiFiClient client;
  32.   const int httpPort = 80;
  33.   if (!client.connect(host, httpPort)) {
  34.     Serial.println("connection failed");
  35.     return;
  36.   }
  37. // Ahora se crea la URL de solicitud
  38.   String url = "/testwifi/index.html";
  39.   Serial.print("Requesting URL: ");
  40.   Serial.println(url);
  41. // Envia la solicitud al servidor
  42.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  43.                      "Host: " + host + "\r\n" +
  44.                      "Connection: close\r\n\r\n");
  45.   delay(500);  
  46. // Lee todas las líneas de la respuesta del servidor y los imprime en UART
  47.   while(client.available()){
  48.     String line = client.readStringUntil('\r');
  49.     Serial.print(line);
  50.   }
  51.   Serial.println();
  52.   Serial.println("closing connection");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment