DweetGet.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Dweet.io GET client for ArduinoHttpClient library
  3. Connects to dweet.io once every ten seconds,
  4. sends a GET request and a request body. Uses SSL
  5. Shows how to use Strings to assemble path and parse content
  6. from response. dweet.io expects:
  7. https://dweet.io/get/latest/dweet/for/thingName
  8. For more on dweet.io, see https://dweet.io/play/
  9. note: WiFi SSID and password are stored in config.h file.
  10. If it is not present, add a new tab, call it "config.h"
  11. and add the following variables:
  12. char ssid[] = "ssid"; // your network SSID (name)
  13. char pass[] = "password"; // your network password
  14. created 15 Feb 2016
  15. updated 16 Feb 2016
  16. by Tom Igoe
  17. this example is in the public domain
  18. */
  19. #include <ArduinoHttpClient.h>
  20. #include <WiFi101.h>
  21. #include "config.h"
  22. const char serverAddress[] = "dweet.io"; // server address
  23. int port = 80;
  24. String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here
  25. WiFiClient wifi;
  26. HttpClient client = HttpClient(wifi, serverAddress, port);
  27. int status = WL_IDLE_STATUS;
  28. int statusCode = 0;
  29. String response;
  30. void setup() {
  31. Serial.begin(9600);
  32. while (!Serial);
  33. while ( status != WL_CONNECTED) {
  34. Serial.print("Attempting to connect to Network named: ");
  35. Serial.println(ssid); // print the network name (SSID);
  36. // Connect to WPA/WPA2 network:
  37. status = WiFi.begin(ssid, pass);
  38. }
  39. // print the SSID of the network you're attached to:
  40. Serial.print("SSID: ");
  41. Serial.println(WiFi.SSID());
  42. // print your WiFi shield's IP address:
  43. IPAddress ip = WiFi.localIP();
  44. Serial.print("IP Address: ");
  45. Serial.println(ip);
  46. }
  47. void loop() {
  48. // assemble the path for the GET message:
  49. String path = "/get/latest/dweet/for/" + dweetName;
  50. // send the GET request
  51. Serial.println("making GET request");
  52. client.get(path);
  53. // read the status code and body of the response
  54. statusCode = client.responseStatusCode();
  55. response = client.responseBody();
  56. Serial.print("Status code: ");
  57. Serial.println(statusCode);
  58. Serial.print("Response: ");
  59. Serial.println(response);
  60. /*
  61. Typical response is:
  62. {"this":"succeeded",
  63. "by":"getting",
  64. "the":"dweets",
  65. "with":[{"thing":"my-thing-name",
  66. "created":"2016-02-16T05:10:36.589Z",
  67. "content":{"sensorValue":456}}]}
  68. You want "content": numberValue
  69. */
  70. // now parse the response looking for "content":
  71. int labelStart = response.indexOf("content\":");
  72. // find the first { after "content":
  73. int contentStart = response.indexOf("{", labelStart);
  74. // find the following } and get what's between the braces:
  75. int contentEnd = response.indexOf("}", labelStart);
  76. String content = response.substring(contentStart + 1, contentEnd);
  77. Serial.println(content);
  78. // now get the value after the colon, and convert to an int:
  79. int valueStart = content.indexOf(":");
  80. String valueString = content.substring(valueStart + 1);
  81. int number = valueString.toInt();
  82. Serial.print("Value string: ");
  83. Serial.println(valueString);
  84. Serial.print("Actual value: ");
  85. Serial.println(number);
  86. Serial.println("Wait ten seconds\n");
  87. delay(10000);
  88. }