BasicAuthGet.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. GET client with HTTP basic authentication for ArduinoHttpClient library
  3. Connects to server once every five seconds, sends a GET request
  4. note: WiFi SSID and password are stored in config.h file.
  5. If it is not present, add a new tab, call it "config.h"
  6. and add the following variables:
  7. char ssid[] = "ssid"; // your network SSID (name)
  8. char pass[] = "password"; // your network password
  9. created 14 Feb 2016
  10. by Tom Igoe
  11. modified 3 Jan 2017 to add HTTP basic authentication
  12. by Sandeep Mistry
  13. this example is in the public domain
  14. */
  15. #include <ArduinoHttpClient.h>
  16. #include <WiFi101.h>
  17. #include "config.h"
  18. char serverAddress[] = "192.168.0.3"; // server address
  19. int port = 8080;
  20. WiFiClient wifi;
  21. HttpClient client = HttpClient(wifi, serverAddress, port);
  22. int status = WL_IDLE_STATUS;
  23. String response;
  24. int statusCode = 0;
  25. void setup() {
  26. Serial.begin(9600);
  27. while ( status != WL_CONNECTED) {
  28. Serial.print("Attempting to connect to Network named: ");
  29. Serial.println(ssid); // print the network name (SSID);
  30. // Connect to WPA/WPA2 network:
  31. status = WiFi.begin(ssid, pass);
  32. }
  33. // print the SSID of the network you're attached to:
  34. Serial.print("SSID: ");
  35. Serial.println(WiFi.SSID());
  36. // print your WiFi shield's IP address:
  37. IPAddress ip = WiFi.localIP();
  38. Serial.print("IP Address: ");
  39. Serial.println(ip);
  40. }
  41. void loop() {
  42. Serial.println("making GET request with HTTP basic authentication");
  43. client.beginRequest();
  44. client.get("/secure");
  45. client.sendBasicAuth("username", "password"); // send the username and password for authentication
  46. client.endRequest();
  47. // read the status code and body of the response
  48. statusCode = client.responseStatusCode();
  49. response = client.responseBody();
  50. Serial.print("Status code: ");
  51. Serial.println(statusCode);
  52. Serial.print("Response: ");
  53. Serial.println(response);
  54. Serial.println("Wait five seconds");
  55. delay(5000);
  56. }