Advertisement
Guest User

Jkjskjsjdak827371

a guest
May 23rd, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. // Esta es la librería para utilizar las funciones de red del ESP8266
  2. #include <ESP8266WiFi.h>
  3.  
  4. //Declaraciones
  5. #define LED_BLUE (D2)
  6.  
  7. //Variables
  8. const char* ssid = "AzziNet"; // Rellena con el nombre de tu red WiFi
  9. const char* password = "deliM1306704428"; // Rellena con la contraseña de tu red WiFi
  10.  
  11. const char* host = "api.wunderground.com";
  12. const char* apiKey = "27ecc0d0847394d4";
  13.  
  14.  
  15.  
  16. /*
  17. * Mensajes codigo
  18. * 1 Pulsación: Conectando...
  19. * 2 pulsaciones, conectado correctamente
  20. * 3 pulsaciones, conectado correctamente a la api del clima
  21. * 4 soltando datos
  22. */
  23.  
  24. void messageLed(int re)
  25. {
  26. for(int i; i < re; i++)
  27. {
  28. digitalWrite(LED_BLUE, HIGH);
  29. delay(10);
  30. digitalWrite(LED_BLUE, LOW);
  31. delay(10);
  32. }
  33. }
  34.  
  35. void setup() {
  36. Serial.begin(115200);
  37. delay(10);
  38.  
  39. //Asignación del led
  40. pinMode(LED_BLUE, OUTPUT);
  41.  
  42. // Conectamos a la red WiFi
  43.  
  44. messageLed(1);
  45. Serial.println();
  46. Serial.println();
  47. Serial.print("Connecting to ");
  48. Serial.println(ssid);
  49.  
  50. /* Configuramos el ESP8266 como cliente WiFi. Si no lo hacemos
  51. se configurará como cliente y punto de acceso al mismo tiempo */
  52. WiFi.mode(WIFI_STA); // Modo cliente WiFi
  53. WiFi.begin(ssid, password);
  54.  
  55. // Esperamos a que estemos conectados a la red WiFi
  56. while (WiFi.status() != WL_CONNECTED) {
  57. delay(500);
  58. Serial.print(".");
  59. }
  60.  
  61. Serial.println("");
  62. Serial.println("WiFi connected");
  63. messageLed(2);
  64. Serial.println("IP address: ");
  65. Serial.println(WiFi.localIP()); // Mostramos la IP
  66. }
  67. // 7853 <---------------------- PIN
  68. void loop() {
  69.  
  70. messageLed(5);
  71. /*Serial.print("connecting to ");
  72. Serial.println(host);
  73.  
  74. // Creamos el cliente
  75. WiFiClient client;
  76. const int httpPort = 80; // Puerto HTTP
  77. if (!client.connect(host, httpPort)) {
  78. // ¿hay algún error al conectar?
  79. Serial.println("Ha fallado la conexión");
  80. return;
  81. }
  82.  
  83. messageLed(3);
  84.  
  85. // Creamos la URL para la petición
  86. String url = "/api/";
  87. url += apiKey;
  88. url += "/conditions/lang:SP/q/autoip.json";
  89.  
  90. Serial.print("URL de la petición: http://");
  91. Serial.print(host);
  92. Serial.print(":");
  93. Serial.print(httpPort);
  94. Serial.println(url);
  95.  
  96. // Enviamos la petición
  97. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  98. "Host: " + host + "\r\n" +
  99. "Connection: close\r\n\r\n");
  100. unsigned long timeout = millis();
  101. while (client.available() == 0) {
  102. if (millis() - timeout > 5000) {
  103. Serial.println(">>> Superado el tiempo de espera !");
  104. client.stop();
  105. return;
  106. }
  107. }
  108.  
  109. // Consutar la memoria libre
  110. // Quedan un poco más de 40 kB
  111. Serial.printf("\nMemoria libre en el ESP8266: %d Bytes\n\n",ESP.getFreeHeap());
  112.  
  113. // Leemos la respuesta y la enviamos al monitor serie
  114. while(client.available()){
  115. String line = client.readStringUntil('\r');
  116. Serial.print(line);
  117. }
  118.  
  119. Serial.println();
  120. Serial.println("Cerrando la conexión");*/
  121.  
  122. /*while(1){
  123. delay(0); // Siempre que hay un bucle que pueda durar mucho tiempo
  124. // hay que llamar a la función delay() para atender a los
  125. // procesos de la conexión WiFi. Si no se hace el ESP8266
  126. // generará un error y se reiniciará a los pocos segundos
  127. }*/
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement