3
3
Connects to dweet.io once every ten seconds,
4
4
sends a GET request and a request body. Uses SSL
5
5
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:
7
8
https://dweet.io/get/latest/dweet/for/thingName
8
9
9
- For more on dweet.io, see https://dweet.io/play/
10
+ For more on dweet.io, see https://dweet.io/play/
10
11
11
12
note: WiFi SSID and password are stored in config.h file.
12
13
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/
15
16
char pass[] = "password"; // your network password
16
17
17
18
created 15 Feb 2016
19
+ updated 16 Feb 2016
18
20
by Tom Igoe
19
21
20
22
this example is in the public domain
@@ -25,7 +27,7 @@ For more on dweet.io, see https://dweet.io/play/
25
27
26
28
char serverAddress[] = " dweet.io" ; // server address
27
29
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
29
31
30
32
WiFiClient wifi;
31
33
RestClient client = RestClient(wifi, serverAddress, port);
@@ -70,6 +72,35 @@ void loop() {
70
72
Serial.print (" Response: " );
71
73
Serial.println (response);
72
74
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
+
73
104
Serial.println (" Wait ten seconds\n " );
74
105
delay (10000 );
75
106
}
0 commit comments