-
Notifications
You must be signed in to change notification settings - Fork 0
/
communicaition.h
88 lines (73 loc) · 1.89 KB
/
communicaition.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef communcation
#define comunication
#include <HTTPClient.h>
#include <math.h>
#include <stdio.h>
String get_data(String API_URL) {
// busca dados
HTTPClient http;
String url = "https://script.google.com/macros/s/" + API_URL + "/exec?read";
http.begin(url.c_str()); //Specify the URL and certificate
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
http.end();
return payload;
}
else {
Serial.println("Error on HTTP request");
http.end();
return "";
}
}
String send_data(String API_URL, String message) {
// envia dados
HTTPClient http;
String url = "https://script.google.com/macros/s/" + API_URL + "/exec?post=" + message;
http.begin(url.c_str()); //Specify the URL and certificate
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
http.end();
return "";
}
else {
Serial.println("Error on HTTP request");
http.end();
return "";
}
}
int split(String str, long arr[]){
// tratamento da lista recebida por get_data
char helper[10];
int val = 0;
int j = 0;
int k = 0;
int u = 0;
for(int i = 0; str[i] != '\0'; i++){
if(str[i] == ','){
if(j != 0){
val = 0;
u = 0;
for(; j > 0; j--){
val += (helper[j-1] - 48)*pow(10,u);
u++;
}
arr[k] = val;
k++;
}
}
else{
helper[j] = str[i];
j++;
}
}
return 0;
}
void getAndProcessData(String API_URL, long out[]){
String raw = get_data(API_URL);
split(raw,out);
}
#endif