forked from gamo256/dweet-esp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdweetESP8266.cpp
138 lines (131 loc) · 3.83 KB
/
dweetESP8266.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "dweetESP8266.h"
#include <ArduinoJson.h>
/**
* Constructor.
*/
dweet::dweet(){
// _thing_name = thing_name;
maxValues = 5;
currentValue = 0;
val = (Value *)malloc(maxValues*sizeof(Value));
}
/**
* This function is to get value from the dweet.io API
* @arg key the key where you will get the data
* @return value the data that you get from the dweet.io API
*/
String dweet::getDweet(char* thing_name, char* key){
String value;
String response;
uint8_t bodyPosinit;
uint8_t bodyPosend;
String dweetKey ="\"";
dweetKey += key;
dweetKey += "\":";
int length = dweetKey.length();
if (_client.connect(SERVER, PORT)){
Serial.println(F("Geting your variable"));
// Make a HTTP request:
_client.print(F("GET /get/latest/dweet/for/"));
_client.print(thing_name);
_client.println(F(" HTTP/1.1"));
_client.println(F("Host: dweet.io"));
_client.println(F("User-Agent: ESP8266-WiFi/1.0"));
_client.println(F("Connection: close"));
_client.println();
}
while (!_client.available());
while (_client.available()){
response = _client.readString();
}
//Serial.write(c);
Serial.println(response);
bodyPosinit =4+ response.indexOf("\r\n\r\n");
response = response.substring(bodyPosinit);
Serial.println(response);
bodyPosinit =10+ response.indexOf("\"content\"");
bodyPosend = response.indexOf("}]}");
response = response.substring(bodyPosinit,bodyPosend);
Serial.println(response);
if (response.indexOf(dweetKey) == -1) {
value = "Key not found";
} else {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
if (!root.success()){
Serial.println("parseObject() failed");
value = "parse error.";
} else {
const char* val = root[key];
value = String(val);
}
}
_client.flush();
_client.stop();
return value;
}
/**
* Add a value of variable to save
* @arg key variable key to save in a struct
* @arg value variable value to save in a struct
*/
void dweet::add(char *key, String value){
if(currentValue == maxValues){
Serial.println(F("You are sending more than 5 consecutives variables, you just could send 5 variables. Then other variables will be deleted!"));
//currentValue = maxValues;
} else {
(val+currentValue)->id = key;
(val+currentValue)->value_id = value;
currentValue++;
}
}
/**
* Send all data of all variables that you saved
* @reutrn true upon success, false upon error.
*/
bool dweet::sendAll(char* thing_name){
int i;
String all;
String str;
for(i=0; i<currentValue;){
str = (val+i)->value_id;
all += String((val + i)->id);
all += "=";
all += str;
i++;
if(i<currentValue){
all += "&";
}
}
//i = all.length();
if (_client.connect(SERVER, PORT)){
Serial.println(F("Posting your variables"));
_client.print(F("POST /dweet/for/"));
_client.print(thing_name);
_client.print(F("?"));
_client.print(all);
_client.println(F(" HTTP/1.1"));
_client.println(F("Host: dweet.io"));
_client.println(F("User-Agent: ESP8266-WiFi/1.0"));
_client.println(F("Connection: close"));
_client.println();
}
while(!_client.available());
while (_client.available()){
char c = _client.read();
Serial.write(c);
}
currentValue = 0;
_client.stop();
return true;
}
bool dweet::wifiConnection(char* ssid, char* pass){
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}