-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySmartHome.ino
253 lines (214 loc) · 6.06 KB
/
MySmartHome.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
#include <ESP8266WiFi.h>
#define LED_GREEN_LIVING_ROOM D0
#define LED_RED_DINING_ROOM D1
#define LED_YELLOW_BATH_ROOM D2
#define LED_RED_EXIT D3
#define delayLed 150
int LED_GREEN_LIVING_ROOM_VALUE = LOW;
int LED_RED_DINING_ROOM_VALUE = LOW;
int LED_YELLOW_BATH_ROOM_VALUE = LOW;
int LED_RED_EXIT_VALUE = LOW;
int PARTY_MODE_VALUE = LOW;
String LIVING_ROOM_LED = "LIVING_ROOM_LED";
String DINING_ROOM_LED = "DINING_ROOM_LED";
String BATHROOM_LED = "BATHROOM_LED";
String EXIT_LED = "EXIT_LED";
String PARTY_MODE = "PARTY_MODE";
String ON = "ON";
String OFF = "OFF";
const char* ssid = "****YOUR_WIFI_SSID*****";
const char* password = "*****YOUR_WIFI_PASSWORD*****";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
connectToWiFi();
setPinModes();
}
void loop()
{
if (PARTY_MODE_VALUE == HIGH)
letsParty();
WiFiClient client = server.available();
if (!client)
return;
if (client.available())
{
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
processRequest(request);
sendWebSiteHtml(client);
}
delay(2);
}
void connectToWiFi()
{
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
}
void sendWebSiteHtml(WiFiClient client)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Green - Living Room is now: ");
if (LED_GREEN_LIVING_ROOM_VALUE == LOW)
client.print("Off");
else
client.print("On");
client.print("<br>Red - Dining Room is now: ");
if (LED_GREEN_LIVING_ROOM_VALUE == LOW)
client.print("Off");
else
client.print("On");
client.print("<br>Yellow - Bath Room is now: ");
if (LED_GREEN_LIVING_ROOM_VALUE == LOW)
client.print("Off");
else
client.print("On");
client.print("<br>Red - Exit is now: ");
if (LED_GREEN_LIVING_ROOM_VALUE == LOW)
client.print("Off");
else
client.print("On");
client.print("<br>Party Mode is now: ");
if (PARTY_MODE_VALUE == LOW)
client.print("Off");
else
client.print("On");
client.println("<br><br>");
client.println("<a href=\"/LIVING_ROOM_LED=ON\"\"><button>Green - Living Room Turn On </button></a>");
client.println("<a href=\"/LIVING_ROOM_LED=OFF\"\"><button>Green - Living Room Turn Off </button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/DINING_ROOM_LED=ON\"\"><button>Red - Dining Room Turn On </button></a>");
client.println("<a href=\"/DINING_ROOM_LED=OFF\"\"><button>Red - Dining Room Turn Off </button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/BATHROOM_LED=ON\"\"><button>Yellow - Bathroom Turn On </button></a>");
client.println("<a href=\"/BATHROOM_LED=OFF\"\"><button>Yellow - Bathroom Turn Off </button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/EXIT_LED=ON\"\"><button>Red - Exit Turn On </button></a>");
client.println("<a href=\"/EXIT_LED=OFF\"\"><button>Red - Exit Turn Off </button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/PARTY_MODE=ON\"\"><button>Party Mode Turn On </button></a>");
client.println("<a href=\"/PARTY_MODE=OFF\"\"><button>Party Mode Turn Off </button></a>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
}
void processRequest(String command)
{
//LIVING ROOM
if (command.indexOf(LIVING_ROOM_LED + ON) != -1)
{
turnOnLed(LED_GREEN_LIVING_ROOM);
LED_GREEN_LIVING_ROOM_VALUE = HIGH;
}
else if (command.indexOf(LIVING_ROOM_LED + OFF) != -1)
{
turnOnLed(LED_GREEN_LIVING_ROOM);
LED_GREEN_LIVING_ROOM_VALUE = LOW;
}
//DINING ROOM
else if (command.indexOf(DINING_ROOM_LED + ON) != -1)
{
turnOnLed(LED_RED_DINING_ROOM);
LED_RED_DINING_ROOM_VALUE = HIGH;
}
else if (command.indexOf(DINING_ROOM_LED + OFF) != -1)
{
turnOnLed(LED_GREEN_LIVING_ROOM);
LED_RED_DINING_ROOM_VALUE = LOW;
}
//BATHROOM
else if (command.indexOf(BATHROOM_LED + ON) != -1)
{
turnOnLed(LED_GREEN_LIVING_ROOM);
LED_YELLOW_BATH_ROOM_VALUE = HIGH;
}
else if (command.indexOf(BATHROOM_LED + OFF) != -1)
{
turnOnLed(LED_GREEN_LIVING_ROOM);
LED_YELLOW_BATH_ROOM_VALUE = LOW;
}
//EXIT
else if (command.indexOf(EXIT_LED + ON) != -1)
{
turnOnLed(LED_RED_EXIT);
LED_RED_EXIT_VALUE = HIGH;
}
else if (command.indexOf(EXIT_LED + OFF) != -1)
{
turnOnLed(LED_RED_EXIT);
LED_RED_EXIT_VALUE = HIGH;
}
//PARTY MODE
else if (command.indexOf(PARTY_MODE + ON) != -1)
{
PARTY_MODE_VALUE = HIGH;
}
else if (command.indexOf(PARTY_MODE + OFF) != -1)
{
PARTY_MODE_VALUE = LOW;
}
}
void turnOnLed(int pin)
{
digitalWrite(pin, HIGH);
}
void turnOffLed(int pin)
{
digitalWrite(pin, LOW);
}
void setPinModes()
{
pinMode(LED_GREEN_LIVING_ROOM, OUTPUT);
pinMode(LED_RED_DINING_ROOM, OUTPUT);
pinMode(LED_YELLOW_BATH_ROOM, OUTPUT);
pinMode(LED_RED_EXIT, OUTPUT);
}
void letsParty()
{
turnOffAll();
digitalWrite(LED_GREEN_LIVING_ROOM, HIGH);
delay(delayLed);
digitalWrite(LED_RED_DINING_ROOM, HIGH);
delay(delayLed);
digitalWrite(LED_YELLOW_BATH_ROOM, HIGH);
delay(delayLed);
digitalWrite(LED_RED_EXIT, HIGH);
delay(delayLed);
turnOffAll();
delay(delayLed);
digitalWrite(LED_RED_EXIT, HIGH);
delay(delayLed);
digitalWrite(LED_YELLOW_BATH_ROOM, HIGH);
delay(delayLed);
digitalWrite(LED_RED_DINING_ROOM, HIGH);
delay(delayLed);
digitalWrite(LED_GREEN_LIVING_ROOM, HIGH);
delay(delayLed);
turnOffAll();
delay(delayLed);
}
void turnOffAll()
{
digitalWrite(LED_GREEN_LIVING_ROOM, LOW);
digitalWrite(LED_RED_DINING_ROOM, LOW);
digitalWrite(LED_YELLOW_BATH_ROOM, LOW);
digitalWrite(LED_RED_EXIT, LOW);
}