IoTtweet.cpp
8.08 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
library for IoTtweet.com, using via api.iottweet.com
Created by : IoTtweet tech. team
Date : 2016.Sep.22
Updated : 2016.Sep.23, 01:35
Updated : 2016.Sep.23, 16:50
Updated : 2016.Sep.24, 20:40
Updated : 2016.Sep.25, 12:03
>> Release GITHUB : v0.1.0 on 25.Sep, 12:04
Updated : 2016.Oct.3, 23:00
>> Release GITHUB : v0.2.0 on 3.Oct, 23:06
>> Features - Read status of specified switch name
- Read status of specified slider name
Updated : 2016.Dec.17, 17:31
>> Release GITHUB : v0.2.9
Updated : 2017.Jan.15, 22:01
>> Able sending tweet as string type.
>> Able tweet by have whitespace string.
>> Release GITHUB : v0.3.0
Updated : 2019.Mar.14, 17:42
>> Modified code to compatible with ArduinoJson.h version 6+
>> Release GITHUB : v1.0.0
*/
#include "IoTtweet.h"
#define IoTtweet_HOST "api.iottweet.com"
#define IoTtweet_PORT 80
#define IoTtweet_libVersion "v1.0.0"
//Connect WiFi router
bool IoTtweet::begin(const char *ssid, const char *passw)
{
//Transfer variable
_ssid = ssid;
_passw = passw;
int _cnt=0, _retry=30;
bool _conn = false;
//Connect WiFi
WiFi.begin(_ssid,_passw);
Serial.print("IoTweet wifi start connection...");
//Connecting WiFi
while ((WiFi.status() != WL_CONNECTED) && (_cnt <= _retry))
{
delay(200);
_cnt++;
}
if(WiFi.status() == WL_CONNECTED)
{
_conn = true;
Serial.println("\nWiFi for IoTtweet Connected !");
}else{
_conn = false;
}
return _conn;
}
//Connect to api.iottweet.com & send data to dashboard
String IoTtweet::WriteDashboard(const char *userid, const char *key, float slot0, float slot1, float slot2, float slot3, String tw, String twpb)
{
//Transfer variable
_userid = userid;
_key = key;
_slot0 = slot0;
_slot1 = slot1;
_slot2 = slot2;
_slot3 = slot3;
_tw = tw;
_twpb = twpb;
//whitespace string replace to %20
_tw.replace(" ","%20");
_twpb.replace(" ","%20");
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += "&slot0=";
_str += _slot0;
_str += "&slot1=";
_str += _slot1;
_str += "&slot2=";
_str += _slot2;
_str += "&slot3=";
_str += _slot3;
_str += "&tw=";
_str += _tw;
_str += "&twpb=";
_str += _twpb;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
//Check response back
while(client.available()){
_response = client.readStringUntil('\r');
}
return _response;
}
}
//Connect to dashboard and read status of all switch and slide
String IoTtweet::ReadControlPanel(const char *userid, const char *key)
{
//Transfer variable
_userid = userid;
_key = key;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return "deserializeJson() failed with code ";
}
_sw1status = doc["sw1"];
_sw2status = doc["sw2"];
_sw3status = doc["sw3"];
_sw4status = doc["sw4"];
_sw5status = doc["sw5"];
_sl1status = doc["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = doc["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = doc["slide3"];
f_sl3status = String(_sl3status).toFloat();
_allcontrol = "{";
_allcontrol += "\"sw1\":\"";
_allcontrol += _sw1status;
_allcontrol += "\",";
_allcontrol += "\"sw2\":\"";
_allcontrol += _sw2status;
_allcontrol += "\",";
_allcontrol += "\"sw3\":\"";
_allcontrol += _sw3status;
_allcontrol += "\",";
_allcontrol += "\"sw4\":\"";
_allcontrol += _sw4status;
_allcontrol += "\",";
_allcontrol += "\"sw5\":\"";
_allcontrol += _sw5status;
_allcontrol += "\",";
_allcontrol += "\"slider1\":\"";
_allcontrol += f_sl1status;
_allcontrol += "\",";
_allcontrol += "\"slider2\":\"";
_allcontrol += f_sl2status;
_allcontrol += "\",";
_allcontrol += "\"slider3\":\"";
_allcontrol += f_sl3status;
_allcontrol += "\"}";
return _allcontrol;
}
}
//Connect to dashboard and read status of specified number of switch
String IoTtweet::ReadDigitalSwitch(const char *userid, const char *key, uint8_t sw)
{
//Transfer variable
_userid = userid;
_key = key;
_sw = sw;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return "deserializeJson() failed with code ";
}
_sw1status = doc["sw1"];
_sw2status = doc["sw2"];
_sw3status = doc["sw3"];
_sw4status = doc["sw4"];
_sw5status = doc["sw5"];
switch(_sw){
case 1:
return _sw1status;
break;
case 2:
return _sw2status;
break;
case 3:
return _sw3status;
break;
case 4:
return _sw4status;
break;
case 5:
return _sw5status;
break;
}
}
}
//Connect to dashboard and read status each of specified number slider
float IoTtweet::ReadAnalogSlider(const char *userid, const char *key, uint8_t slider)
{
//Transfer variable
_userid = userid;
_key = key;
_slider = slider;
//Create connection TCP to api.iottweet.com
WiFiClient client;
if (client.connect(IoTtweet_HOST, IoTtweet_PORT))
{
//Start sending data package
_str = "GET /read/?userid=";
_str += _userid;
_str += "&key=";
_str += _key;
_str += " HTTP/1.1\r\n";
_str += "Host: api.iottweet.com\r\n";
_str += "Connection: keep-alive\r\n\r\n";
//Push data to api.iottweet.com
client.print(_str);
delay(50);
//Check response back
while(client.available()){
_controlpanelstatus = client.readStringUntil('\r');
}
//--------- Start parse json message ---------------
DynamicJsonDocument doc(1024);
auto error = deserializeJson(doc, _controlpanelstatus);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
}
_sl1status = doc["slide1"];
f_sl1status = String(_sl1status).toFloat();
_sl2status = doc["slide2"];
f_sl2status = String(_sl2status).toFloat();
_sl3status = doc["slide3"];
f_sl3status = String(_sl3status).toFloat();
switch(_slider){
case 1:
return f_sl1status;
break;
case 2:
return f_sl2status;
break;
case 3:
return f_sl3status;
break;
}
}
}
//Get IoTtweet library version information
String IoTtweet::getVersion()
{
return IoTtweet_libVersion;
}