sketch_sep18a.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Simple HTTP get webclient test
  3. */
  4. #include <ESP8266WiFi.h>
  5. const char* ssid = "hazard18";
  6. const char* password = "pu1f0xylu1";
  7. const char* host = "wifitest.adafruit.com";
  8. void setup() {
  9. pinMode(0, OUTPUT);
  10. Serial.begin(115200);
  11. delay(100);
  12. // We start by connecting to a WiFi network
  13. Serial.println();
  14. Serial.println();
  15. Serial.print("Connecting to ");
  16. Serial.println(ssid);
  17. WiFi.begin(ssid, password);
  18. while (WiFi.status() != WL_CONNECTED) {
  19. delay(500);
  20. Serial.print(".");
  21. }
  22. Serial.println("");
  23. Serial.println("WiFi connected");
  24. Serial.println("IP address: ");
  25. Serial.println(WiFi.localIP());
  26. }
  27. int value = 0;
  28. void loop() {
  29. digitalWrite(0, HIGH);
  30. delay(5000);
  31. ++value;
  32. digitalWrite(0, LOW);
  33. Serial.print("connecting to ");
  34. Serial.println(host);
  35. // Use WiFiClient class to create TCP connections
  36. WiFiClient client;
  37. const int httpPort = 80;
  38. if (!client.connect(host, httpPort)) {
  39. Serial.println("connection failed");
  40. return;
  41. }
  42. // We now create a URI for the request
  43. String url = "/testwifi/index.html";
  44. Serial.print("Requesting URL (" + String(value) + ")" + " : ");
  45. Serial.println(url);
  46. // This will send the request to the server
  47. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  48. "Host: " + host + "\r\n" +
  49. "Connection: close\r\n\r\n");
  50. delay(500);
  51. // Read all the lines of the reply from server and print them to Serial
  52. while(client.available()){
  53. String line = client.readStringUntil('\r');
  54. Serial.print(line);
  55. }
  56. Serial.println();
  57. Serial.println("closing connection");
  58. }