SimplePut.ino 1.8 KB

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