Skip to content

Commit e0db2a7

Browse files
committed
Example of use of POST and GET requests using the Http class and ArduinoJSON
1 parent 90d5677 commit e0db2a7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

HttpExample/HttpExample.ino

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <ArduinoJson.h>
2+
#include <Http.h>
3+
4+
HTTP http;
5+
Result result;
6+
7+
void print(const char *message, int result = -1){
8+
if (result != -1){
9+
char buffer[100];
10+
sprintf(buffer, message, result);
11+
Serial.println(buffer);
12+
}
13+
else {
14+
Serial.println(message);
15+
}
16+
}
17+
18+
// the setup routine runs once when you press reset:
19+
void setup() {
20+
Serial.begin(9600);
21+
while(!Serial);
22+
23+
//http.preInit();
24+
}
25+
26+
// the loop routine runs over and over again forever:
27+
void loop() {
28+
29+
print("Cofigure bearer: %d", http.configureBearer("movistar.es"));
30+
print("HTTP connect: %d", http.connect());
31+
32+
char response[256];
33+
34+
result = http.get("smartgarden.herokuapp.com", response);
35+
print("HTTP GET: %d", result);
36+
if (result == SUCCESS) {
37+
Serial.println(response);
38+
StaticJsonBuffer<200> jsonBuffer;
39+
JsonObject& root = jsonBuffer.parseObject(response);
40+
Serial.print("date: ");
41+
Serial.println((const char *)root["date"]);
42+
Serial.print("name: ");
43+
Serial.println((const char *)root["name"]);
44+
}
45+
46+
result = http.post("smartgarden.herokuapp.com", "{\"name\":\"Antonio\"}", response);
47+
print("HTTP POST: %d", result);
48+
if (result == SUCCESS) {
49+
Serial.println(response);
50+
StaticJsonBuffer<200> jsonBuffer;
51+
JsonObject& root = jsonBuffer.parseObject(response);
52+
Serial.print("date: ");
53+
Serial.println((const char *)root["date"]);
54+
Serial.print("name: ");
55+
Serial.println((const char *)root["name"]);
56+
}
57+
58+
print("HTTP disconnect: %d", http.disconnect());
59+
60+
}

0 commit comments

Comments
 (0)