Sei sulla pagina 1di 5

ASSIGNMENT 4

I am going to make a home automation system for this assignment


using Arduino UNO and Wi-Fi shield.

I am going to show you how to monitor your home using the Arduino
Wi-Fi Shield along with the Arduino UNO board. You will be able to
monitor some sensors in your home which are installed on your doors
and you are able to know if the door has been opened or not.

Hardware Requirements:

1. Arduino UNO board


2. Arduino Wi-Fi shield
3. Small push button(principle is exactly same)
4. 10kΩ resistor
5. Wireless network

Software Requirement:

1. Arduino IDE

Procedure for Hardware Configuration:

1. First connect the Wi-Fi shield into the Arduino board.


2. Then connect the resistor and the push button to the Arduino
shield using a pull-down configuration.
3. The button will be connected to pin no. 2 because this is an
interrupt pin.
4. We can check the button state by using digital Read() function.
But there is a problem with that function, so we use
attachInterrupt() function.
attachInterrupt(0,setDoorStatus,RISING)

Here the function setDoorStatus will set a variable to indicate that the
door has been opened.

Now we make our board answer with the status of the sensor if
something access the board and for this we have to connect the shield
to the wireless network.

Full Code:
Sign up
  
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourSSID"; // your network SSID (name)
char pass[] = "yourPass"; // your network password
int status = WL_IDLE_STATUS;
boolean door_status = false;
WiFiServer server(80);
void setup() {
// start serial port for debugging purposes
Serial.begin(9600);

// Attach interrupt to pin 2


attachInterrupt(0, setDoorStatus, RISING);
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {

// listen for incoming clients


WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has
ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<meta http-equiv=\"refresh\"
content=\"5\">");

if (door_status == false){
client.print("Everything is ok");
}
else {
client.print("Alert ! The door has been opened");
}
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setDoorStatus() {
door_status = true;
}

Now upload the code into the board and wait until the “link” LED turns
green on the board.
Now just open your browser and type the IP address.

Then you see:

Everything is OK.

Now press the button and return to the page. The message should be
changed.

Alert! The door has been opened.

You can now monitor what is going on in your home directly from your
computer.

Potrebbero piacerti anche