Skip to content

Commit a2734f6

Browse files
committed
Updating the example in order to handle intervals of execution
1 parent e0db2a7 commit a2734f6

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

HttpExample/HttpExample.ino

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@
44
HTTP http;
55
Result result;
66

7-
void print(const char *message, int result = -1){
7+
unsigned long lastRunTime = 0;
8+
unsigned long waitForRunTime = 0;
9+
10+
// the setup routine runs once when you press reset:
11+
void setup() {
12+
Serial.begin(9600);
13+
while(!Serial);
14+
Serial.println("Starting!");
15+
}
16+
17+
// the loop routine runs over and over again forever:
18+
void loop() {
19+
if (shouldTrackTimeEntry()) trackTimeEntry();
20+
}
21+
22+
// functions
23+
void print(const char *message, long result = -1){
824
if (result != -1){
925
char buffer[100];
1026
sprintf(buffer, message, result);
@@ -15,46 +31,41 @@ void print(const char *message, int result = -1){
1531
}
1632
}
1733

18-
// the setup routine runs once when you press reset:
19-
void setup() {
20-
Serial.begin(9600);
21-
while(!Serial);
22-
23-
//http.preInit();
34+
bool shouldTrackTimeEntry(){
35+
unsigned long elapsedTime = millis() - lastRunTime;
36+
print("Elapsed time: %ld", elapsedTime);
37+
return elapsedTime >= waitForRunTime;
2438
}
2539

26-
// the loop routine runs over and over again forever:
27-
void loop() {
40+
void trackTimeEntry(){
2841

2942
print("Cofigure bearer: %d", http.configureBearer("movistar.es"));
3043
print("HTTP connect: %d", http.connect());
3144

3245
char response[256];
3346

34-
result = http.get("smartgarden.herokuapp.com", response);
47+
result = http.get("smartgarden.herokuapp.com/api", response);
3548
print("HTTP GET: %d", result);
3649
if (result == SUCCESS) {
3750
Serial.println(response);
3851
StaticJsonBuffer<200> jsonBuffer;
3952
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"]);
53+
lastRunTime = millis();
54+
waitForRunTime = root["waitForRunTime"];
55+
56+
print("Last run time: %ld", lastRunTime);
57+
print("Next post in: %ld", waitForRunTime);
4458
}
45-
46-
result = http.post("smartgarden.herokuapp.com", "{\"name\":\"Antonio\"}", response);
59+
60+
result = http.post("smartgarden.herokuapp.com/api", "{\"name\":\"Antonio\"}", response);
4761
print("HTTP POST: %d", result);
4862
if (result == SUCCESS) {
4963
Serial.println(response);
5064
StaticJsonBuffer<200> jsonBuffer;
5165
JsonObject& root = jsonBuffer.parseObject(response);
52-
Serial.print("date: ");
53-
Serial.println((const char *)root["date"]);
5466
Serial.print("name: ");
5567
Serial.println((const char *)root["name"]);
5668
}
5769

5870
print("HTTP disconnect: %d", http.disconnect());
59-
6071
}

0 commit comments

Comments
 (0)