@@ -16,68 +16,17 @@ const int inputPinForDoor = 2;
16
16
17
17
SimpleTimer timer;
18
18
19
- void setup () {
20
- Serial.begin (115200 ); // for debugging
21
-
22
- Serial.print (" Connecting to wifi" );
23
- WiFi.begin (wifiCreds[0 ], wifiCreds[1 ]);
24
- while (WiFi.status () != WL_CONNECTED) {
25
- delay (500 );
26
- Serial.print (" ." );
27
- }
28
- Serial.println (" \r\n WiFi connected." );
29
- Serial.println (" access point:" );
30
- Serial.println (WiFi.SSID ());
31
- Serial.println (" ip address:" );
32
- Serial.println (WiFi.localIP ());
33
-
34
- Serial.println (" \r\n Ready for interwebs action!\r\n " );
35
-
36
- // using GPIO2 for input. door is normally closed.
37
- // can not be active LOW on reset or weird stuff happens
38
- // needs to be open/HIGH
39
- // and then closed/LOW -after- startup
40
- pinMode (inputPinForDoor, INPUT_PULLUP);
41
-
42
- // check to see if the door has been left open for too long every 10s
43
- timer.setInterval (5000 , checkOpen);
44
- }
45
-
46
- void loop () {
47
- timer.run ();
48
- }
49
-
50
- void checkOpen () {
51
- if ( digitalRead (inputPinForDoor) == doorOpen ) {
52
- Serial.println (" ncInputPinForDoor is HIGH" );
53
- Serial.println (" door is open" );
54
- doorOpenDurationInSeconds += 5 ;
55
- Serial.println (" doorOpenDurationInSeconds:" );
56
- Serial.println (doorOpenDurationInSeconds);
57
- }
58
- if ( digitalRead (inputPinForDoor) == doorClosed ) {
59
- Serial.println (" ncInputPinForDoor is LOW" );
60
- Serial.println (" door is closed." );
61
- resetDoorOpenCounter ();
62
- Serial.println (" doorOpenDurationInSeconds:" );
63
- Serial.println (doorOpenDurationInSeconds);
64
- }
65
-
66
- if ( messageSentInThisOpening == false &&
67
- doorOpenDurationInSeconds > openForTooLongInMins * 60 ) {
68
- String messageToSend = (String)" WARNING: your garage door has been open for more than " + openForTooLongInMins + " mins!" ;
69
- sendSms (messageToSend);
70
- // todo: this does not know if it sent successfully. needs work
71
- Serial.println (" Sent SMS: " + messageToSend);
72
- messageSentInThisOpening = true ;
73
- }
74
- }
75
-
76
19
void resetDoorOpenCounter () {
77
20
doorOpenDurationInSeconds = 0 ;
78
21
messageSentInThisOpening = false ;
79
22
}
80
23
24
+ String urlEncode (String input){
25
+ input.replace (" +" ," %2B" );
26
+ input.replace (" " ," %20" );
27
+ return input;
28
+ }
29
+
81
30
void sendSms (String message) {
82
31
Serial.println (" making POST request to Twilio for sending sms.." );
83
32
@@ -121,10 +70,61 @@ void sendSms(String message) {
121
70
Serial.println (" closing connection" );
122
71
}
123
72
124
- String urlEncode (String input){
125
- input.replace (" +" ," %2B" );
126
- input.replace (" " ," %20" );
127
- return input;
73
+ void checkOpen () {
74
+ if ( digitalRead (inputPinForDoor) == doorOpen ) {
75
+ Serial.println (" ncInputPinForDoor is HIGH" );
76
+ Serial.println (" door is open" );
77
+ doorOpenDurationInSeconds += 5 ;
78
+ Serial.println (" doorOpenDurationInSeconds:" );
79
+ Serial.println (doorOpenDurationInSeconds);
80
+ }
81
+ if ( digitalRead (inputPinForDoor) == doorClosed ) {
82
+ Serial.println (" ncInputPinForDoor is LOW" );
83
+ Serial.println (" door is closed." );
84
+ resetDoorOpenCounter ();
85
+ Serial.println (" doorOpenDurationInSeconds:" );
86
+ Serial.println (doorOpenDurationInSeconds);
87
+ }
88
+
89
+ if ( messageSentInThisOpening == false &&
90
+ doorOpenDurationInSeconds > openForTooLongInMins * 60 ) {
91
+ String messageToSend = (String)" WARNING: your garage door has been open for more than " + openForTooLongInMins + " mins!" ;
92
+ sendSms (messageToSend);
93
+ // todo: this does not know if it sent successfully. needs work
94
+ Serial.println (" Sent SMS: " + messageToSend);
95
+ messageSentInThisOpening = true ;
96
+ }
97
+ }
98
+
99
+ void setup () {
100
+ Serial.begin (115200 ); // for debugging
101
+
102
+ Serial.print (" Connecting to wifi" );
103
+ WiFi.begin (wifiCreds[0 ], wifiCreds[1 ]);
104
+ while (WiFi.status () != WL_CONNECTED) {
105
+ delay (500 );
106
+ Serial.print (" ." );
107
+ }
108
+ Serial.println (" \r\n WiFi connected." );
109
+ Serial.println (" access point:" );
110
+ Serial.println (WiFi.SSID ());
111
+ Serial.println (" ip address:" );
112
+ Serial.println (WiFi.localIP ());
113
+
114
+ Serial.println (" \r\n Ready for interwebs action!\r\n " );
115
+
116
+ // using GPIO2 for input. door is normally closed.
117
+ // can not be active LOW on reset or weird stuff happens
118
+ // needs to be open/HIGH
119
+ // and then closed/LOW -after- startup
120
+ pinMode (inputPinForDoor, INPUT_PULLUP);
121
+
122
+ // check to see if the door has been left open for too long every 10s
123
+ timer.setInterval (5000 , checkOpen);
124
+ }
125
+
126
+ void loop () {
127
+ timer.run ();
128
128
}
129
129
130
130
0 commit comments