SimpleHttpExample.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // (c) Copyright 2010-2012 MCQN Ltd.
  2. // Released under Apache License, version 2.0
  3. //
  4. // Simple example to show how to use the HttpClient library
  5. // Get's the web page given at http://<kHostname><kPath> and
  6. // outputs the content to the serial port
  7. #include <SPI.h>
  8. #include <WiFi101.h>
  9. #include <ArduinoHttpClient.h>
  10. // This example downloads the URL "http://arduino.cc/"
  11. char ssid[] = "yourNetwork"; // your network SSID (name)
  12. char pass[] = "secretPassword"; // your network password
  13. // Name of the server we want to connect to
  14. const char kHostname[] = "arduino.cc";
  15. // Path to download (this is the bit after the hostname in the URL
  16. // that you want to download
  17. const char kPath[] = "/";
  18. // Number of milliseconds to wait without receiving any data before we give up
  19. const int kNetworkTimeout = 30*1000;
  20. // Number of milliseconds to wait if no data is available before trying again
  21. const int kNetworkDelay = 1000;
  22. WiFiClient c;
  23. HttpClient http(c, kHostname);
  24. void setup()
  25. {
  26. //Initialize serial and wait for port to open:
  27. Serial.begin(9600);
  28. while (!Serial) {
  29. ; // wait for serial port to connect. Needed for native USB port only
  30. }
  31. // attempt to connect to Wifi network:
  32. Serial.print("Attempting to connect to WPA SSID: ");
  33. Serial.println(ssid);
  34. while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
  35. // unsuccessful, retry in 4 seconds
  36. Serial.print("failed ... ");
  37. delay(4000);
  38. Serial.print("retrying ... ");
  39. }
  40. Serial.println("connected");
  41. }
  42. void loop()
  43. {
  44. int err =0;
  45. err = http.get(kPath);
  46. if (err == 0)
  47. {
  48. Serial.println("startedRequest ok");
  49. err = http.responseStatusCode();
  50. if (err >= 0)
  51. {
  52. Serial.print("Got status code: ");
  53. Serial.println(err);
  54. // Usually you'd check that the response code is 200 or a
  55. // similar "success" code (200-299) before carrying on,
  56. // but we'll print out whatever response we get
  57. // If you are interesting in the response headers, you
  58. // can read them here:
  59. //while(http.headerAvailable())
  60. //{
  61. // String headerName = http.readHeaderName();
  62. // String headerValue = http.readHeaderValue();
  63. //}
  64. int bodyLen = http.contentLength();
  65. Serial.print("Content length is: ");
  66. Serial.println(bodyLen);
  67. Serial.println();
  68. Serial.println("Body returned follows:");
  69. // Now we've got to the body, so we can print it out
  70. unsigned long timeoutStart = millis();
  71. char c;
  72. // Whilst we haven't timed out & haven't reached the end of the body
  73. while ( (http.connected() || http.available()) &&
  74. (!http.endOfBodyReached()) &&
  75. ((millis() - timeoutStart) < kNetworkTimeout) )
  76. {
  77. if (http.available())
  78. {
  79. c = http.read();
  80. // Print out this character
  81. Serial.print(c);
  82. // We read something, reset the timeout counter
  83. timeoutStart = millis();
  84. }
  85. else
  86. {
  87. // We haven't got any data, so let's pause to allow some to
  88. // arrive
  89. delay(kNetworkDelay);
  90. }
  91. }
  92. }
  93. else
  94. {
  95. Serial.print("Getting response failed: ");
  96. Serial.println(err);
  97. }
  98. }
  99. else
  100. {
  101. Serial.print("Connect failed: ");
  102. Serial.println(err);
  103. }
  104. http.stop();
  105. // And just stop, now that we've tried a download
  106. while(1);
  107. }