-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blink.ino
373 lines (328 loc) · 8.6 KB
/
Blink.ino
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "FS.h"
#include <CapacitiveSensor.h>
#include <aJSON.h>
//#include "HTTP_Async/ESP8266.h"
const String URL = "http://aws.blink.care";
const String APP_ID = "APPLICATION_ID";
bool confed = false;
CapacitiveSensor cs_4_2 = CapacitiveSensor(14, 12);
const int resetPin = 16;
int threshold = 100;
int long_press = 350;
int short_press = 90;
int new_char = 1200;
int new_word = 3000;
int reset = 6000;
String queue = "";
String prev_sent = "";
bool started = false;
String id = "";
String sessionToken = "";
String userId = "";
String deviceName = "";
void setup() {
// DEBUGGING, REMOVE WHEN DONE!
//WiFi.disconnect();
Serial.begin(115200);
SPIFFS.begin();
pinMode(resetPin, INPUT);
String def;
Serial.println(digitalRead(resetPin));
if ( digitalRead(resetPin) == HIGH) {
def = get_name();
def.trim();
runWifi(def, false);
} else {
def = get_name();
def.trim();
runWifi(def, true);
}
deviceName = get_device();
sessionToken = def;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
HTTPClient http;
http.begin(URL + "/parse/users/me");
http.addHeader("X-Parse-Application-Id", APP_ID);
http.addHeader("X-Parse-Session-Token", sessionToken);
if (http.GET()) {
String out = http.getString();
Serial.println(out);
if (strstr(out.c_str(), "invalid session token") != NULL) {
runWifi(def, false);
} else {
aJsonObject* jsonObject = aJson.parse(const_cast<char*> (out.c_str()));
aJsonObject* objId = aJson.getObjectItem(jsonObject, "objectId");
userId = objId->valuestring;
}
}
http.end();
id = get_id();
Serial.println(id);
}
int getVal() {
long total1 = cs_4_2.capacitiveSensor(30);
delay(100);
return total1;
}
String writeData(String queue, String i) {
Serial.println("Starting sending");
HTTPClient http;
if (queue == prev_sent && i != "") {
return i;
} else {
prev_sent = queue;
}
if (i == "") {
http.begin(URL + "/parse/classes/Queue");
} else {
http.begin(URL + "/parse/classes/Queue/" + i);
}
if (sessionToken == "")
return i;
http.addHeader("X-Parse-Application-Id", APP_ID);
http.addHeader("Content-Type", "application/json");
http.addHeader("X-Parse-Session-Token", sessionToken);
String data = "{\"deviceName\": \"" + deviceName + "\", \"queue\": \"" + queue + "\", \"started\": " + (started ? "true" : "false") + ", \"ACL\": {\"" + userId + "\": {\"read\": true, \"write\": true}, \"*\": {}}}";
if (i == "") {
http.POST(data);
String out = http.getString();
http.end();
aJsonObject* jsonObject = aJson.parse(const_cast<char*> (out.c_str()));
aJsonObject* objId = aJson.getObjectItem(jsonObject, "objectId");
id = objId->valuestring;
return objId->valuestring;
} else {
http.sendRequest("PUT", data);
Serial.println("Sending");
String out = http.getString();
http.end();
if (strstr(out.c_str(), "\"error\"") != NULL) {
Serial.println(out);
id = get_id();
return id;
}
return i;
}
}
String get_id() {
HTTPClient http;
http.begin(URL + "/parse/classes/Queue");
http.addHeader("X-Parse-Application-Id", APP_ID);
http.addHeader("X-Parse-Session-Token", sessionToken);
http.sendRequest("GET", "where={\"deviceName\": \"Blinky\"}");
String out = http.getString();
http.end();
// if (!out) {
// return "";
// }
aJsonObject* jsonObject = aJson.parse(const_cast<char*> (out.c_str()));
aJsonObject* objId = aJson.getObjectItem(jsonObject, "results");
Serial.println(out);
if (aJson.getArraySize(objId) != 0) {
aJsonObject* result = aJson.getArrayItem(objId, 0);
return aJson.getObjectItem(result, "objectId")->valuestring;
} else {
return "";
}
}
void loop()
{
if ( digitalRead(resetPin) == HIGH) {
String def = get_name();
def.trim();
runWifi(def, false);
}
int value;
long change, start_millis;
if (strstr(queue.c_str(), "..--") != NULL) {
queue = "";
started = true;
} else if (strstr(queue.c_str(), "......") != NULL) {
started = false;
queue = "";
}
value = getVal();
Serial.println(value);
start_millis = millis();
while (value < threshold) {
value = getVal();
Serial.println(value);
change = millis() - start_millis;
if (new_char < change) {
break;
}
}
if (new_char < change) {
if (started == true) {
if (!queue.endsWith("|") && !queue.endsWith(" ") && queue.length() != 0) {
queue += "|";
writeData(queue, id);
}
}
}
if (value > threshold) {
start_millis = millis();
while (value > threshold) {
value = getVal();
}
change = millis() - start_millis;
if (change > short_press && change < long_press) {
queue += ".";
} else if (change > long_press && change < reset) {
queue += "-";
} else if (change > reset) {
queue = "";
}
writeData(queue, id);
}
Serial.println(queue);
}
///////////////////////////////////////////////
String get_name() {
File f = SPIFFS.open("/session.txt", "r");
if (!f) {
return "";
} else {
return f.readString();
}
}
void write_name(String hostname) {
File f = SPIFFS.open("/session.txt", "w");
f.println(hostname);
}
String get_device() {
File f = SPIFFS.open("/device.txt", "r");
if (!f) {
return "Blinky";
} else {
String out = f.readString();
out.trim();
return out;
}
}
void write_device(String hostname) {
File f = SPIFFS.open("/device.txt", "w");
f.println(hostname);
}
String authenticate(String username, String password) {
HTTPClient http;
http.begin(URL + "/parse/login?username=" + urlencode(username) + "&password=" + urlencode(password));
http.addHeader("X-Parse-Application-Id", APP_ID);
http.addHeader("Content-Type", "application/json");
int code = http.GET();
String out = http.getString();
http.end();
Serial.println(out);
if (code == 200) {
aJsonObject* jsonObject = aJson.parse(const_cast<char*> (out.c_str()));
aJsonObject* sessionToken = aJson.getObjectItem(jsonObject, "sessionToken");
return sessionToken->valuestring;
} else {
return "";
}
}
void setconfed(WiFiManager *myWiFiManager) {
confed = true;
}
void runWifi(String def, bool aut) {
char defa[24];
def.toCharArray(defa, 24);
WiFiManagerParameter username("username", "Email (if no account, create one in the app)", "", 24);
WiFiManagerParameter password("password", "Password", "", 24);
WiFiManagerParameter device("device", "Device Name", "Blinky", 24);
WiFiManager wifiManager;
wifiManager.addParameter(&username);
wifiManager.addParameter(&password);
wifiManager.addParameter(&device);
wifiManager.setAPCallback(setconfed);
if (aut) {
wifiManager.autoConnect("Blink Device");
} else {
wifiManager.startConfigPortal("Blink Device");
}
if (confed) {
sessionToken = authenticate(username.getValue(), password.getValue());
write_name(sessionToken);
write_device(device.getValue());
ESP.restart();
}
}
// URL ENCODE ///////////////////////////////////////////////////
String urldecode(String str)
{
String encodedString = "";
char c;
char code0;
char code1;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == '+') {
encodedString += ' ';
} else if (c == '%') {
i++;
code0 = str.charAt(i);
i++;
code1 = str.charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString += c;
} else {
encodedString += c;
}
yield();
}
return encodedString;
}
String urlencode(String str)
{
String encodedString = "";
char c;
char code0;
char code1;
char code2;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
} else if (isalnum(c)) {
encodedString += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
code2 = '\0';
encodedString += '%';
encodedString += code0;
encodedString += code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
unsigned char h2int(char c)
{
if (c >= '0' && c <= '9') {
return ((unsigned char)c - '0');
}
if (c >= 'a' && c <= 'f') {
return ((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <= 'F') {
return ((unsigned char)c - 'A' + 10);
}
return (0);
}