-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino Program
365 lines (322 loc) · 10.6 KB
/
Arduino Program
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
356
357
358
359
360
361
362
363
364
365
/*
TCP_WIFI program for Arduino and Arduino WIFI shield.
This program is made to connect Smart Home server and does several tasks.
The circuit:
- LED from pin 9
- Magnetic sensor pins 2,3
created 10-04-2020
by Mohammed Hazem Kudaimi and Mohammed Amayi
*/
#include <WiFi.h>
#include <Servo.h>
#include <SimpleDHT.h>
Servo servoo;
SimpleDHT11 temp_sensor;
WiFiClient client; //Use WiFiClient class to create TCP connection.
const int sensor_up = 2; //The Upper magnetic sensor.
const int sensor_down = 5; //The lower magnatic sensor.
const int temp_sensor_Pin = 4;
const int SPEED = 3;
const int Counterclockwise = 6;
const int Clockwise = 8;
const int sound_sensor = A0;
const int microswitch_OPEN = A1;
const int microswitch_CLOSE = A2;
const int pirPin = A3;
const int servo_Pin = A4;
const int lamp_1 = A5; //The lamp 1.
boolean magnetic_Sensor_Up = true;
boolean magnetic_Sensor_Down = true;
boolean stoop = false;
boolean openSensor = false;
boolean closeSensor = false;
boolean motion_sensor_state = true;
//String str;
//String strFromServer;
#ifndef localRouterName //Tests if this word is not defined.
#define localRouterName "TP-Link Extender" //Network name.
#define localRouterPassword "3gtfhA7T" //Network key.
#endif //End if statement.
const char* LRNANE = localRouterName;
const char* LRPassword = localRouterPassword;
const char* serverIP = "192.168.1.9";
const uint16_t serverPort = 9000;
void setup() {
pinMode(lamp_1, OUTPUT);
pinMode(sound_sensor, INPUT);
pinMode(sensor_down, INPUT);
pinMode(sensor_up, INPUT);
servoo.attach(servo_Pin);
pinMode(temp_sensor_Pin, INPUT);
pinMode (Clockwise, OUTPUT);
pinMode (Counterclockwise, OUTPUT);
pinMode (SPEED, OUTPUT);
pinMode (microswitch_OPEN, INPUT);
pinMode (microswitch_CLOSE, INPUT);
pinMode(pirPin, INPUT);
delay(200);
Serial.begin(115200); //Open serial port and set data rate to 115200 bps.
delay(200);
Serial.print("");
Serial.print("Wait for WiFi...");
WiFi.begin(LRNANE, LRPassword); //Initialize the WiFi network settings and provide the current status.
connectToWiFi(); //Connect to the WIFI.
writeIPAddress(); //Write the WIFI IP address.
connectToServer(); //Connect to the server
writeConnectionInfo(); //Write the connection information with the server
sendMessage("Cnnected"); //Send a message to the server to make sure that everything is well.
}
void loop() {
sendAndReceiveData(); //Send and receice data between WIFI and Arduino.
if (!client.connected()) { //Check the connection with the server.
Serial.println("disconnected with the server.");
client.stop(); //Disconnect the client from the server.
for (;;);
}
}
void connectToWiFi() {
while (WiFi.status() != WL_CONNECTED) { //Attempt to connect to Wifi network.
Serial.print(".");
delay(1000); //Wait 1 seconds for connection.
}
}
void writeIPAddress() {
Serial.println("");
Serial.print("WiFi is connected. The IP address is: ");
Serial.println(WiFi.localIP()); //Get the Arduino WiFi shield IP address.
delay(500);
}
void connectToServer() {
while (!client.connect(serverIP, serverPort)) { //Check the connection to the server with this IP address and port number.
Serial.println("connection with server is failed.");
Serial.println("wait 5 sec... and try again");
delay(2000);
}
}
void writeConnectionInfo() {
Serial.print("connecting to server: "); //Write the connection information with the server.
Serial.print(serverIP);
Serial.print(" . Server port number: ");
Serial.println(serverPort);
}
void sendMessage(String str) { // Send Message to the server.
Serial.println("Arduino is connected is send to the server.");
client.println(str);
}
void sendAndReceiveData() {
while (client.connected()) {
readSoundSensor();
motionSensor();
if (client.available()) {
String strFromServer = client.readStringUntil('\r'); //Reading the data received from the Server.
//Serial.println();
Serial.print(strFromServer); //Write the message in the serial monitor.
order(strFromServer); //Send the message to sort.
delay(10);
}
}
}
void order(String str) { //Sort the coming message.
if (str == "lamp1_on") {
digitalWrite(lamp_1, HIGH);
client.println(str);
Serial.println();
Serial.print("State is send to the server: " + str);
}
else if (str == "lamp1_off") {
digitalWrite(lamp_1, LOW);
client.println(str);
Serial.println();
Serial.print("State is send to the server: " + str);
}
else if (str == "up") {
client.println(str);
Serial.println();
Serial.print("State is send to the server: " + str);
magnetic_Sensor_Up = true;
stoop = false;
while (magnetic_Sensor_Up_Detected()) {
servoo.write(100);
String strFromServer = client.readStringUntil('\r');
//Serial.println();
Serial.print(strFromServer);
order(strFromServer);
if (stoop) {
break;
}
}
servoo.write(90);
if (!magnetic_Sensor_Up_Detected()) {
client.println("top");
Serial.println();
Serial.print("State is send to the server: top");
}
}
else if (str == "down") {
client.println(str);
Serial.println();
Serial.print("State is send to the server: " + str);
magnetic_Sensor_Down = true;
stoop = false;
while (magnetic_Sensor_Down_Detected()) {
servoo.write(80);
String strFromServer = client.readStringUntil('\r');
//Serial.println();
Serial.print(strFromServer);
order(strFromServer);
if (stoop) {
break;
}
servoo.write(90);
if (!magnetic_Sensor_Down_Detected()) {
client.println("bottom");
Serial.println();
Serial.print("State is send to the server: bottom");
}
}
}
else if (str == "stop") {
stoop = true;
servoo.write(90);
client.println("stoop");
Serial.println();
Serial.print("State is send to the server: " + str);
}
else if (str == "temp") {
// client.println(str);
Serial.println();
Serial.print("State is send to the server: " + str);
get_temperature();
}
else if (str == "initiate") {
if (!magnetic_Sensor_Up_Detected()) {
client.println("top");
Serial.println();
Serial.print("State is send to the server: top");
}
else if (!magnetic_Sensor_Down_Detected()) {
client.println("bottom");
Serial.println();
Serial.print("State is send to the server: bottom");
}
}
else if (str == "open") {
Serial.println();
Serial.print("The door is opening now");
while (!open_Senseor_Detected()) {
analogWrite(SPEED, 60);
digitalWrite(Clockwise, LOW);
digitalWrite(Counterclockwise, HIGH);
//strFromServer = (String)Serial.readStringUntil('\r');
//Serial.print(strFromServer);
//order(str);
}
if (open_Senseor_Detected()) {
Serial.println();
Serial.print("Open sensor is detected");
analogWrite(SPEED, 0);
Serial.println();
Serial.print("The door is opened!");
client.println("open");
}
}
else if (str == "close") {
Serial.println();
Serial.print("The door closing now");
while (!close_Senseor_Detected()) {
analogWrite(SPEED, 60);
digitalWrite(Clockwise, HIGH);
digitalWrite(Counterclockwise, LOW);
//strFromServer = (String)Serial.readStringUntil('\r');
//Serial.print(strFromServer);
//order(str);
}
if (close_Senseor_Detected()) {
Serial.println();
Serial.print("Close sensor is detected");
analogWrite(SPEED, 0);
Serial.println();
Serial.print("The door is closed!");
client.println("close");
}
}
delay(10);
}
void readSoundSensor() {
if (analogRead(sound_sensor) >= 210) {
if (digitalRead(lamp_1) == LOW) {
digitalWrite(lamp_1, HIGH);
client.println("lamp1_on");
Serial.println();
Serial.print("State is send to the server: lamp1_on");
delay(20);
}
else {
digitalWrite(lamp_1, LOW);
client.println("lamp1_off");
Serial.println();
Serial.print("State is send to the server: lamp1_off");
delay(20);
}
}
}
boolean magnetic_Sensor_Up_Detected() {
if (digitalRead(sensor_up) == LOW) {
magnetic_Sensor_Up == true;
} else {
magnetic_Sensor_Up = false;
}
return magnetic_Sensor_Up;
}
boolean magnetic_Sensor_Down_Detected() {
if (digitalRead(sensor_down) == HIGH) {
magnetic_Sensor_Down == true;
} else {
magnetic_Sensor_Down = false;
}
return magnetic_Sensor_Down;
}
void get_temperature() {
byte temp = 0;
byte humidity = 0;
temp_sensor.read(temp_sensor_Pin, &temp, &humidity, NULL);
Serial.println();
Serial.print("The temperature is ");
Serial.print((String)temp);
//Serial.print(" *C, and The humidity is ");
//Serial.println(humidity);
client.println("temp:" + (String)temp);
}
boolean close_Senseor_Detected() {
if (digitalRead(microswitch_CLOSE) == LOW) {
closeSensor = true;
} else {
closeSensor = false;
}
return closeSensor;
}
boolean open_Senseor_Detected() {
if (digitalRead(microswitch_OPEN) == LOW) {
openSensor = true;
} else {
openSensor = false;
}
return openSensor;
}
void motionSensor() {
int sensor_value = digitalRead(pirPin);
if (sensor_value == HIGH) {
if (motion_sensor_state) {
motion_sensor_state = false;
client.println("guest");
Serial.println();
Serial.print("Motion sensor detected");
}
} else {
if (!motion_sensor_state) {
motion_sensor_state = true;
Serial.println();
Serial.print("Motion ended");
}
}
}