-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriggerAlarm.ino
355 lines (228 loc) · 7.03 KB
/
TriggerAlarm.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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <EEPROM.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
extern "C" {
#include <user_interface.h>
}
//We can keep 100 bytes per field in the EEPROM
//ESP-01 has a 4096 bytes long EEPROM
#define IFTTT_TOKEN_START_ADDRESS 100
#define IFTTT_EVENT_START_ADDRESS 200
#define ALLOWED_CHARACTER_LOWER_VALUE 48
#define ALLOWED_CHARACTER_UPPER_VALUE 122
#define AP_SSID "TriggerAlarm"
#define AP_PASSWORD "TriggerAlarm"
#define MAC_LENGTH 6
//define your default values here, if there are different values in config.json, they are overwritten.
String s_ifttt_event;
String s_ifttt_token;
const char* c_ifttt_event;
const char* c_ifttt_token;
//flag for saving data
bool shouldSaveConfig = false;
bool sensorTrigger = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
bool saveDataInEEPROM(int fieldstartaddress, const char* stringdata) {
// The event name is a string whose length varies
//The eeprom location at the start address holds the length of the data
int data_length = strlen(stringdata);
//Serial.print("Data Length: ");
//Serial.println(data_length);
EEPROM.write(fieldstartaddress, data_length);
EEPROM.commit();
int dataStartAddress = fieldstartaddress + 1;
int dataEndAddress = dataStartAddress + data_length;
int totalValidCharacters = 0;
for (int addr = 0; addr < data_length; addr++)
{
if (allowedCharacter(stringdata[addr]))
{
EEPROM.write(addr + dataStartAddress, stringdata[addr]);
EEPROM.commit();
totalValidCharacters = totalValidCharacters + 1;
}
}
//Update the length of the valid characters
EEPROM.write(fieldstartaddress, totalValidCharacters);
EEPROM.commit();
return true;
}
String getEEPROMData(int fieldstartaddress) {
// The event name is a string whose length varies
//The eeprom location at the start address holds the length of the data
int data_length = EEPROM.read(fieldstartaddress);
//Serial.println(data_length);
int dataStartAddress = fieldstartaddress + 1;
int dataEndAddress = dataStartAddress + data_length;
String dataString;
for (int addr = 0; addr < data_length; addr++)
{
char characterInEEPROM = EEPROM.read(addr + dataStartAddress);
if (allowedCharacter(characterInEEPROM))
{
dataString = dataString + characterInEEPROM;
}
}
return dataString;
}
// We wish to limit the characters to be within the ascii table
bool allowedCharacter(char questionableCharacter)
{
if (questionableCharacter > ALLOWED_CHARACTER_UPPER_VALUE)
{
return false;
}
if (questionableCharacter < ALLOWED_CHARACTER_LOWER_VALUE)
{
return false;
}
return true;
}
char getHEXValue(uint8_t value)
{
if (value < 10)
{
return 48 + value;
}
else
{
return 55 + value;
}
}
boolean SetupMode = false;
boolean HTTPActionToBeInitiated=false;
const byte ACTION_TRIGGER_PIN = 2;
const byte SETUP_MODE_PIN = 0;
WiFiManager wifiManager;
void ICACHE_RAM_ATTR handleInterruptRising();
void ISRHTTPActionTrigger()
{
detachInterrupt(digitalPinToInterrupt(ACTION_TRIGGER_PIN));
HTTPActionToBeInitiated=true;
Serial.println("Interrupted");
}
void ICACHE_RAM_ATTR ISRSetupMode();
void ISRSetupMode()
{
detachInterrupt(digitalPinToInterrupt(SETUP_MODE_PIN));
uint32_t startTime=millis();
while(millis()-startTime<3000)
{
}
if(digitalRead(SETUP_MODE_PIN)==LOW)
{
SetupMode=true;
Serial.println("Going into Setup Mode");
}
}
void setup() {
/* We wish to evaluate how the reset happened
/* It is possible we could reach here either by a loss of power or actual reset */
/* Loss of Power should not lead to a notification being sent */
pinMode(ACTION_TRIGGER_PIN, INPUT);
pinMode(SETUP_MODE_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(ACTION_TRIGGER_PIN), ISRHTTPActionTrigger, RISING);
attachInterrupt(digitalPinToInterrupt(SETUP_MODE_PIN), ISRSetupMode, FALLING);
rst_info *resetInfo;
resetInfo = ESP.getResetInfoPtr();
int reasonCode = (*resetInfo).reason;
Serial.println(reasonCode);
Serial.begin(115200);
Serial.println();
EEPROM.begin(512);
// Lets look at the data that exists in the EEPROM
s_ifttt_event = String(getEEPROMData(IFTTT_EVENT_START_ADDRESS));
s_ifttt_token = String(getEEPROMData(IFTTT_TOKEN_START_ADDRESS));
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.setConfigPortalTimeout(60);
}
void ConnectToWifi()
{
WiFiManagerParameter IFTTTEventName("ifttt_event", "ifttt_event", s_ifttt_event.c_str(), 100);
WiFiManagerParameter IFTTTToken("ifttt_token", "ifttt_token", s_ifttt_token.c_str(), 100);
//add all your parameters here
wifiManager.addParameter(&IFTTTEventName);
wifiManager.addParameter(&IFTTTToken);
if (!wifiManager.autoConnect(AP_SSID, AP_PASSWORD)) {
Serial.println("failed to connect and hit timeout");
}
//if you get here you have connected to the WiFi
Serial.println("Connected to AP");
if (shouldSaveConfig)
{
//read updated parameters
c_ifttt_event = IFTTTEventName.getValue();
c_ifttt_token = IFTTTToken.getValue();
//We save the Data we recieved in EEPROM.
saveDataInEEPROM(IFTTT_TOKEN_START_ADDRESS, c_ifttt_token);
saveDataInEEPROM(IFTTT_EVENT_START_ADDRESS, c_ifttt_event);
}
}
bool sendIFTTTRequest()
{
uint8_t mac[MAC_LENGTH];
WiFi.macAddress(mac);
String MACString = "";
uint8_t i;
for (i = 0; i < MAC_LENGTH; i++)
{
uint8_t MSN = (mac[i] & 0xF0) >> 4;
uint8_t LSN = (mac[i] & 0x0F);
uint8_t current_index = i << 1; //Multiply by 2
//Serial.println(current_index);
MACString = MACString + getHEXValue(MSN);
MACString = MACString + getHEXValue(LSN);
}
HTTPClient http;
String URL = "http://maker.ifttt.com/trigger/IFTTT_EVENT/with/key/IFTTT_TOKEN?value1=VALUE_1";
//String IFTTT_EVENT(ifttt_event);
//String IFTTT_TOKEN(ifttt_token);
//String MACString(MACResult);
URL.replace("IFTTT_EVENT", s_ifttt_event);
URL.replace("IFTTT_TOKEN", s_ifttt_token);
MD5Builder md5;
md5.begin();
md5.add(MACString);
md5.calculate();
URL.replace("VALUE_1", md5.toString());
Serial.println(URL);
http.begin(URL);
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode == 200)// The IFTTT Request which went through
{
return true;
}
else
{
return false;
}
}
void loop() {
if (HTTPActionToBeInitiated)
{
delay(5000);// Wait for the Input to settle
ConnectToWifi();
while(sendIFTTTRequest()) //Make the HTTP Request. repeat until you get a success
{
ESP.restart();// Restart the ESP
}
}
if (SetupMode)
{
delay(1000);
wifiManager.resetSettings();
ConnectToWifi();
ESP.restart();// Restart the ESP
}
}