-
Notifications
You must be signed in to change notification settings - Fork 317
/
Extra_Utilities.ino
412 lines (324 loc) · 10.3 KB
/
Extra_Utilities.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//**********************************************************************************************************************
//
// Gets the TaskIndex from the Task Name - returns 255 if not found
//
byte getTaskIndex(String TaskName)
// This routine returns the Task Index of the task called TaskName - note that TaskName is not case sensitive
// If TaskName is not found, then it returns 255
{
for (byte y = 0; y < TASKS_MAX; y++)
{
LoadTaskSettings(y);
String DName = ExtraTaskSettings.TaskDeviceName;
if (( ExtraTaskSettings.TaskDeviceName[0] != 0 ) && ( DName.equalsIgnoreCase(TaskName) ))
{
return y;
}
}
return 255;
}
// Extracts the Task Name and ValueName from the Ident
boolean getTaskandValueName(String IdentIn,String &TaskName,String &ValueName)
{
String Ident;
Ident = IdentIn;
// Get rid of any []
Ident.replace("[", " ");
Ident.replace("]", " ");
Ident.trim();
// Find the location of # - it must exist and must not be at the beginning or the end of the ident
int loc = Ident.indexOf("#");
if ((loc == -1) || (loc == 0) || (loc == Ident.length()))
{
return false;
}
// Seperate out the taskname and the valuename
TaskName = Ident.substring(0, loc);
ValueName = Ident.substring(loc + 1);
return true;
}
// ************************************************************************************************************************
//
// Gets the ValuenameIndex from the TaskIndex and the ValueName - returns 255 if not found
//
byte getValueNameIndex(int TaskIndex, String ValueName)
{
// This routine checks to see if ValueName is valid for a given taskindex.
// If the Valuename is found then we return its number - else 255
LoadTaskSettings(TaskIndex);
for (byte y = 0; y < VARS_PER_TASK; y++)
{
if (ValueName.equalsIgnoreCase(ExtraTaskSettings.TaskDeviceValueNames[y]))
{
return y;
}
}
return 255;
}
// ******************************************************************************************************************
//
// Standard routine for checking parameters
//
boolean CheckParam(String Name, int Val, int Min, int Max)
//
{
if (Val < Min or Val > Max)
{
String log = F("ERR : '");
log += Name;
log += "' has value ";
log += Val;
log += " but should be between ";
log += Min;
log += " and ";
log += Max;
addLog(LOG_LEVEL_ERROR, log);
return false;
}
return true;
}
// **************************************************************************
//
// Convert String to Integer
//
int string2Integer(String myString) {
int i, value, len;
len = myString.length();
char tmp[(len+1)]; // one extra for the zero termination
byte start=0;
if ( myString.substring(0,1) == "-"){
tmp[0]='-';
start=1; //allow a minus in front of string
}
for(i=start; i<len; i++)
{
tmp[i] = myString.charAt(i);
if ( ! isdigit(tmp[i]) ) return -999;
}
tmp[i]=0;
value = atoi(tmp);
return value;
}
// ***************************************************************************
//
// Convert String to float
//
float string2float(String myString) {
int i, len;
float value;
len = myString.length();
char tmp[(len+1)]; // one extra for the zero termination
byte start=0;
// Look for decimal point - they can be anywhere but no more than one of them!
int dotIndex=myString.indexOf(".");
//Serial.println(dotIndex);
if (dotIndex != -1)
{
int dotIndex2=(myString.substring(dotIndex+1)).indexOf(".");
//Serial.println(dotIndex2);
if (dotIndex2 != -1 )return -999.00; // Give error if there is more than one dot
}
if ( myString.substring(0,1) == "-"){
start=1; //allow a minus in front of string
tmp[i] = '-';
}
for(i=start; i<len; i++)
{
tmp[i] = myString.charAt(i);
if ( ! isdigit(tmp[i]) )
{
if (tmp[i] != '.' )return -999;
}
}
tmp[i]=0;
value = atof(tmp);
return value;
}
// *****************************************************************************
//
// Log Updates
//
void logUpdates(byte ModNum,byte TaskNum, byte ValueNum, float NewValue)
{
LoadTaskSettings(TaskNum);
String log=F("");
log+=ModNum;
log += " : [";
log+=ExtraTaskSettings.TaskDeviceName;
log+="#";
log += ExtraTaskSettings.TaskDeviceValueNames[ValueNum];
log += "] set to ";
log += NewValue;
addLog(LOG_LEVEL_INFO,log);
}
//***************************************************************************
// Send a pushbullet note
boolean pushbulletSend(String PushBulletAPIKEY, String Title, String Body) {
String log;
const char* host = "api.pushbullet.com";
const int Port = 443;
// Use WiFiClientSecure class to create TLS connection - this is too heavy on resources!!
WiFiClientSecure client;
if (!client.connect(host, Port)) {
log = F("ERR : Connection to ");
log += host;
log += " failed";
addLog(LOG_LEVEL_ERROR, log);
return false;
}
log = F("CON : Connected to ");
log += host;
addLog(LOG_LEVEL_INFO, log);
// Construct the POST Data String
String df1 = F("{\"type\": \"note\", \"title\":");
String df2 = F("\"body\":");
String PushBullet_Data = df1 + "\"" + Title + "\"" + "," + df2 + "\"" + Body + "\"" + "}";
// Determine the Data size
String PushBullet_Data_Size = String(PushBullet_Data.length());
// Now send stuff to pushbullet
String url = "/v2/pushes";
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + PushBulletAPIKEY + "\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + PushBullet_Data_Size + "\r\n\r\n" + PushBullet_Data);
//print the response
// If all is OK, he first line should contain the string "200 OK"
// If this is the case then don't print anything and return true
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\r');
if (line.lastIndexOf("200 OK") >= 0) {
log = F("IFTT : Success - PushBullet Note Sent");
addLog(LOG_LEVEL_INFO, log);
client.stop();
return true;
}
log = F("ERR : ");
log += line;
addLog(LOG_LEVEL_ERROR, log);
}
else {
// No data yet, wait a while
delay(50);
};
}
// All done
client.stop();
addLog(LOG_LEVEL_ERROR, "ERR : Error sending PushBullet Note");
return false;
}
//****************************************************************
// Get the most recent value of parameter IdentIn
// Return -999 in case of error
float getLatestValue(String IdentIn) {
String TaskName;
String ValueName;
String Ident;
Ident = IdentIn;
// Get rid of any []
Ident.replace("[", " ");
Ident.replace("]", " ");
Ident.trim();
// Find the location of # - it must exist and must not be at the beginning or the end of the ident
int loc = Ident.indexOf("#");
if ((loc == -1) || (loc == 0) || (loc == Ident.length()))
{
String log = F("Err : Illegal Identifier Syntax - ");
log += IdentIn;
addLog(LOG_LEVEL_ERROR, log);
return -999;
}
// Seperate out the taskname and the valuename
TaskName = Ident.substring(0, loc);
ValueName = Ident.substring(loc + 1);
// Get the indices from the names
int TaskIndex = getTaskIndex(TaskName);
if (TaskIndex == 255) {
String log = F("Err : Unknown Task - ");
log += TaskName;
addLog(LOG_LEVEL_ERROR, log);
return -999;
}
int ValueIndex = getValueNameIndex(TaskIndex, ValueName);
if (ValueIndex == 255) {
String log = F("Err : Unknown ValueName - ");
log += ValueName;
addLog(LOG_LEVEL_ERROR, log);
return -999;
}
// And get the last reading from Uservar
return UserVar[TaskIndex*VARS_PER_TASK + ValueIndex];
}
//****************************************************************************************************
// Send a trigger to IFTTT with user specified key, Event and Data fields
boolean IFTTT_Trigger(String IFTTT_APIKey, String IFTTT_Event, String IFTTT_Value1, String IFTTT_Value2, String IFTTT_Value3)
{
const char *host = "maker.ifttt.com";
String log;
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int Port = 80;
if (!client.connect(host, Port)) {
log = F("ERR : Connection to ");
log += host;
log += " failed";
addLog(LOG_LEVEL_ERROR, log);
return false;
}
log = F("CON : Connected to ");
log += host;
addLog(LOG_LEVEL_INFO, log);
// Construct the POST Data String
String df1 = F("{\"value1\":");
String df2 = F("\"value2\":");
String df3 = F("\"value3\":");
String IFTTT_Data = df1 + "\"" + IFTTT_Value1 + "\"" + "," + df2 + "\"" + IFTTT_Value2 + "\"" + "," + df3 + "\"" + IFTTT_Value3 + "\"" + "}";
// Determine the Data size
String IFTTT_Data_Size = String(IFTTT_Data.length());
// We now create a URL for the request as per the IFTTT API instructions
String url = F("/trigger/");
url += IFTTT_Event;
url += "/with/key/";
url += IFTTT_APIKey;
// This will send the request to the server
client.print(String("POST ") + url + " HTTP/1.1\r\n"
+ "Host: " + host + "\r\n"
+ "Connection: close\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Length: " + IFTTT_Data_Size + "\r\n"
+ "\r\n"
+ IFTTT_Data + "\r\n");
// Read all the lines of the reply from server and print them to Serial,
// the connection will close when the server has sent all the data.
// If all is OK, he first line should contain the string "200 OK"
// If this is the case then don't print anything and return true
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\r');
if (line.lastIndexOf("200 OK") >= 0) {
log = F("IFTT : Success - Trigger '");
log += IFTTT_Event;
log += "' Sent to IFTTT";
addLog(LOG_LEVEL_INFO, log);
client.stop();
return true;
}
log = F("ERR : ");
log += line;
addLog(LOG_LEVEL_ERROR, log);
}
else {
// No data yet, wait a bit
delay(50);
};
}
// All done
client.stop();
addLog(LOG_LEVEL_ERROR, "IFTT : Error sending trigger to IFTTT");
return false;
}