Skip to content

Commit fc8beb4

Browse files
committed
Updated DweetGet to parse results
1 parent 0ce0ce0 commit fc8beb4

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
examples/*/config.h
1+
examples/*/config.h
2+
examples/DweetGet/config.h

examples/DweetGet/DweetGet.ino

+34-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
Connects to dweet.io once every ten seconds,
44
sends a GET request and a request body. Uses SSL
55
6-
Shows how to use Strings to assemble path. dweet.io expects:
6+
Shows how to use Strings to assemble path and parse content
7+
from response. dweet.io expects:
78
https://dweet.io/get/latest/dweet/for/thingName
89
9-
For more on dweet.io, see https://dweet.io/play/
10+
For more on dweet.io, see https://dweet.io/play/
1011
1112
note: WiFi SSID and password are stored in config.h file.
1213
If it is not present, add a new tab, call it "config.h"
@@ -15,6 +16,7 @@ For more on dweet.io, see https://dweet.io/play/
1516
char pass[] = "password"; // your network password
1617
1718
created 15 Feb 2016
19+
updated 16 Feb 2016
1820
by Tom Igoe
1921
2022
this example is in the public domain
@@ -25,7 +27,7 @@ For more on dweet.io, see https://dweet.io/play/
2527

2628
char serverAddress[] = "dweet.io"; // server address
2729
int port = 80;
28-
String dweetName = "my-thing-name"; // use your own thing name here
30+
String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here
2931

3032
WiFiClient wifi;
3133
RestClient client = RestClient(wifi, serverAddress, port);
@@ -70,6 +72,35 @@ void loop() {
7072
Serial.print("Response: ");
7173
Serial.println(response);
7274

75+
/*
76+
Typical response is:
77+
{"this":"succeeded",
78+
"by":"getting",
79+
"the":"dweets",
80+
"with":[{"thing":"my-thing-name",
81+
"created":"2016-02-16T05:10:36.589Z",
82+
"content":{"sensorValue":456}}]}
83+
84+
You want "content": numberValue
85+
*/
86+
// now parse the response looking for "content":
87+
int labelStart = response.indexOf("content\":");
88+
// find the first { after "content":
89+
int contentStart = response.indexOf("{", labelStart);
90+
// find the following } and get what's between the braces:
91+
int contentEnd = response.indexOf("}", labelStart);
92+
String content = response.substring(contentStart + 1, contentEnd);
93+
Serial.println(content);
94+
95+
// now get the value after the colon, and convert to an int:
96+
int valueStart = content.indexOf(":");
97+
String valueString = content.substring(valueStart + 1);
98+
int number = valueString.toInt();
99+
Serial.print("Value string: ");
100+
Serial.println(valueString);
101+
Serial.print("Actual value: ");
102+
Serial.println(number);
103+
73104
Serial.println("Wait ten seconds\n");
74105
delay(10000);
75106
}

examples/DweetGet/config.h

-3
This file was deleted.

0 commit comments

Comments
 (0)