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