-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarageDoor.ino
237 lines (201 loc) · 5.3 KB
/
GarageDoor.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
#include <Ticker.h>
#include <ESP8266WiFi.h>
#define DEBUG false // flag to turn on/off debugging
#define Serial if(DEBUG)Serial
#define CLOSED LOW // Open/Closed depends on if the sensor is NO or NC
#define OPEN HIGH
const int statusPin = 13;
const int relayPin = 12;
const int ledPin = 0;
Ticker ticker;
WiFiServer server(80);
WiFiClient client;
const IPAddress updateserver(192,168,1,100); //IP of domoticz server
const int updateport = 8084; //Port of domoticz server
const char* updateurl = "GET /json.htm?type=command¶m=udevice&idx=12&nvalue="; //Update URL for domoticz
const char* SSID = "<YourWifiSSID>";
const char* PASS = "<YourWifiPassword>";
int Status = -1;
bool SendUpdate = false;
void setup() {
Serial.begin(115200);
pinMode(statusPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(50);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
delay(1000);
//Start the server
server.begin();
Serial.println("Server started");
//Print the IP Address
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void loop()
{
CheckDoorStatus();
if (SendUpdate)
{
SendStatusUpdate();
SendUpdate = false;
}
client = server.available();
if (client) // if you get a client
{
char getLine[128];
int i = 0;
bool getLineFound = false;
bool currentLineIsBlank = true;
Serial.println("");
Serial.println("new client");
while (client.connected()) // loop while the client's connected
{
if (client.available()) // if there's bytes to read from the client
{
char c = client.read(); // read 1 byte from client
Serial.print(c);
if (!getLineFound && i < sizeof(getLine))
{
//save the char to getLine
getLine[i] = c;
i++;
}
if (c == '\n' && currentLineIsBlank) // respond to client only after last line is received, last line is blank and ends with \n
{
ProcessRequest(getLine);
SendResponse(client);
break;
}
if (c == '\n') // end of the line, next char read will be a new line
{
if (!getLineFound) //the GET line is the first line from the client, save it for later
{
getLineFound = true;
//strip off the HTTP/1.1 from the end of the getLine
const char *ptr = strstr(getLine, "HTTP/1.1");
if (ptr)
getLine[ptr - getLine - 1] = '\0';
}
currentLineIsBlank = true;
}
else if (c != '\r') //text char received
{
currentLineIsBlank = false;
}
} //end if (client.available())
} //end while (client.connected())
// close the connection
delay(1); //allow client to receive the data
client.stop();
Serial.println("client disconnected");
} //end if (client)
}
void CheckDoorStatus()
{
int newStatus = digitalRead(statusPin);
if (newStatus == Status)
{
//new status is the same as the current status, return
return;
}
else
{
Status = newStatus;
Serial.print("Status has changed to: ");
if (Status == CLOSED)
{
Serial.println("CLOSED");
}
else
{
Serial.println("OPEN");
}
SendUpdate = true;
}
}
void SendResponse(WiFiClient& client)
{
client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
client.println("<!DOCTYPE HTML>\r\n<html>\r\n<head>\r\n<title>Garage Door Monitor</title><link rel=\"shortcut icon\" type=\"image / x - icon\" href=\"http://arduino.cc/en/favicon.png\" /></head>");
client.println("<body>\r\n<center>\r\n<h1>Garage Door Status</h1>\r\n<hr>\r\n<br>\r\nGarage Door is now: ");
if (Status == CLOSED) {
client.print("<span style='background-color:#00FF00; font-size:18pt'>Closed</span>\r\n<br>");
}
else {
client.print("<span style='background-color:#FF0000; font-size:18pt'>Open</span>\r\n<br>");
}
client.println("</center>\r\n</html>");
}
void ProcessRequest(char* getLine)
{
if (strstr(getLine, "GET /ACTIVATE") != NULL)
{
Serial.println("Activating garage door");
SendUpdate = true; //refresh the client
ActivateDoor();
}
}
void SendStatusUpdate()
{
if (client.connect(updateserver, updateport))
{
digitalWrite(ledPin, LOW);
Serial.println("connected to update server");
// Make your API request:
client.print(updateurl);
if (Status == CLOSED)
{
client.print("0");
}
else
{
client.print("1");
}
client.println(" HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
client.println();
while (client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop();
digitalWrite(ledPin, HIGH);
}
else
{
Serial.println("connection to update server failed");
Serial.println();
}
}
void ActivateDoor()
{
Serial.println("Turning relay on");
digitalWrite(relayPin, HIGH);
ticker.attach_ms(500, ReleaseDoorButton);
}
void ReleaseDoorButton() {
Serial.println("Turning relay off");
digitalWrite(relayPin, LOW);
ticker.detach();
}