Skip to content

Commit 4da63d6

Browse files
author
matt kocaj
committed
re-ordering code cos esp8266/Arduino#1194
1 parent d528b75 commit 4da63d6

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

iforgottocloseit/iforgottocloseit.ino

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,17 @@ const int inputPinForDoor = 2;
1616

1717
SimpleTimer timer;
1818

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\nWiFi 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\nReady 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-
7619
void resetDoorOpenCounter() {
7720
doorOpenDurationInSeconds = 0;
7821
messageSentInThisOpening = false;
7922
}
8023

24+
String urlEncode(String input){
25+
input.replace("+","%2B");
26+
input.replace(" ","%20");
27+
return input;
28+
}
29+
8130
void sendSms(String message) {
8231
Serial.println("making POST request to Twilio for sending sms..");
8332

@@ -121,10 +70,61 @@ void sendSms(String message) {
12170
Serial.println("closing connection");
12271
}
12372

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\nWiFi 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\nReady 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();
128128
}
129129

130130

0 commit comments

Comments
 (0)