SimpleGet.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Simple GET client 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. this example is in the public domain
  12. */
  13. #include <ArduinoHttpClient.h>
  14. #include <WiFi101.h>
  15. #include "config.h"
  16. char serverAddress[] = "192.168.0.3"; // server address
  17. int port = 8080;
  18. WiFiClient wifi;
  19. HttpClient client = HttpClient(wifi, serverAddress, port);
  20. int status = WL_IDLE_STATUS;
  21. String response;
  22. int statusCode = 0;
  23. void setup() {
  24. Serial.begin(9600);
  25. while ( status != WL_CONNECTED) {
  26. Serial.print("Attempting to connect to Network named: ");
  27. Serial.println(ssid); // print the network name (SSID);
  28. // Connect to WPA/WPA2 network:
  29. status = WiFi.begin(ssid, pass);
  30. }
  31. // print the SSID of the network you're attached to:
  32. Serial.print("SSID: ");
  33. Serial.println(WiFi.SSID());
  34. // print your WiFi shield's IP address:
  35. IPAddress ip = WiFi.localIP();
  36. Serial.print("IP Address: ");
  37. Serial.println(ip);
  38. }
  39. void loop() {
  40. Serial.println("making GET request");
  41. client.get("/");
  42. // read the status code and body of the response
  43. statusCode = client.responseStatusCode();
  44. response = client.responseBody();
  45. Serial.print("Status code: ");
  46. Serial.println(statusCode);
  47. Serial.print("Response: ");
  48. Serial.println(response);
  49. Serial.println("Wait five seconds");
  50. delay(5000);
  51. }