DweetPost.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Dweet.io POST client for ArduinoHttpClient library
  3. Connects to dweet.io once every ten seconds,
  4. sends a POST request and a request body.
  5. Shows how to use Strings to assemble path and body
  6. note: WiFi SSID and password are stored in config.h file.
  7. If it is not present, add a new tab, call it "config.h"
  8. and add the following variables:
  9. char ssid[] = "ssid"; // your network SSID (name)
  10. char pass[] = "password"; // your network password
  11. created 15 Feb 2016
  12. by Tom Igoe
  13. this example is in the public domain
  14. */
  15. #include <ArduinoHttpClient.h>
  16. #include <WiFi101.h>
  17. #include "config.h"
  18. const char serverAddress[] = "dweet.io"; // server address
  19. int port = 80;
  20. WiFiClient wifi;
  21. HttpClient client = HttpClient(wifi, serverAddress, port);
  22. int status = WL_IDLE_STATUS;
  23. int statusCode = 0;
  24. String response;
  25. void setup() {
  26. Serial.begin(9600);
  27. while(!Serial);
  28. while ( status != WL_CONNECTED) {
  29. Serial.print("Attempting to connect to Network named: ");
  30. Serial.println(ssid); // print the network name (SSID);
  31. // Connect to WPA/WPA2 network:
  32. status = WiFi.begin(ssid, pass);
  33. }
  34. // print the SSID of the network you're attached to:
  35. Serial.print("SSID: ");
  36. Serial.println(WiFi.SSID());
  37. // print your WiFi shield's IP address:
  38. IPAddress ip = WiFi.localIP();
  39. Serial.print("IP Address: ");
  40. Serial.println(ip);
  41. }
  42. void loop() {
  43. // assemble the path for the POST message:
  44. String dweetName = "scandalous-cheese-hoarder";
  45. String path = "/dweet/for/" + dweetName;
  46. String contentType = "application/json";
  47. // assemble the body of the POST message:
  48. int sensorValue = analogRead(A0);
  49. String postData = "{\"sensorValue\":\"";
  50. postData += sensorValue;
  51. postData += "\"}";
  52. Serial.println("making POST request");
  53. // send the POST request
  54. client.post(path, contentType, postData);
  55. // read the status code and body of the response
  56. statusCode = client.responseStatusCode();
  57. response = client.responseBody();
  58. Serial.print("Status code: ");
  59. Serial.println(statusCode);
  60. Serial.print("Response: ");
  61. Serial.println(response);
  62. Serial.println("Wait ten seconds\n");
  63. delay(10000);
  64. }