Antitheft


Description

In this tutorial is shown how to create a simple “antitheft ”using a PIR sensor and to manage it via a Smartphone/PC using an Arduino UNO WiFi.

Warning:
Please, it's important to upgrade to the latest ESP firmware, using the Arduino UNO firmware updater tool, before to continue this tutorial.

 

Hardware 


You can use also a Tinkerkit  LED and 3 Wires Female/Male.
Thinkerkit Led
For more details visit our Store.

Circuit

The PIR Motion Sensor has three pins: VCC, OUTPUT and Ground.
Connect the Ground pin at GND, the Vcc at 5V and the OUTPUT at the digital pin 3 of the board.
Instead the Led has two legs called anode and cathode, as shown in the below image:
LED 
Connect the anode (usually the longer leg) in series to a resistor of 220Ω and connect it to the board on pin 13
Finally connect the cathode to GND. The complete circuit is shown in the below picture.

pir sensor circuit

If you want to use a Tinkerkit Led then follow these indication:
Normally the Tinkerkit devices have three pins Vcc(+), the Signal and the Ground (-). 
Connect the Vcc pin (+) to 5V, the Ground (-) to GND and the other one to the pin 13 of the board, you can use the wires female/male for the connections.


Code

Now connect the board to PC, using the USB cable, and upload the code below, using the Arduino IDE (click here to download the sketch):

/*
This sketch, connected to an PIR sensor, reveal a motion near the sensor
Upload the code and open your borwser on http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
for more information:
http://www.arduino.org/learning/tutorials/boards-tutorials/antitheft

created in November 2016
by sebba[at]arduino[dot]org @ a.ferrante[at]arduino[dot]org
*/

#include <Wire.h>
#include <ArduinoWiFi.h>

volatile byte state = LOW;
//On the Arduino Uno the Interrupt availables are pin 2 and 3
//https://www.arduino.cc/en/Reference/AttachInterrupt
const byte interruptPin = 3;
const byte ledPin = 13;

void setup() {
Wifi.begin();
Wifi.println("WebServer Server is up");

pinMode(interruptPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
//When the high level is present on the interruptPin turn to state to HIGH.
attachInterrupt(digitalPinToInterrupt(interruptPin), allarm, HIGH);
}
void loop() {

while(Wifi.available()){
process(Wifi);
}
delay(50);
}
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');

if (command == "webserver") {
WebServer(client);
}
}
void WebServer(WifiData client) {
//ledPin reveal a motion
digitalWrite(ledPin, state);

//The header of the web page
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println(F("Refresh: 0.5")); // refresh the page automatically every sec
client.println();
client.println(F("<html>"));
client.println(F("<head> <title>UNO WIFI Tutorial - Antitheft</title> </head>"));
client.print(F("<body>"));

String alertBox=F("<div style='color: white; font-size:48px; background:green; width:400px; height:200px; border: 1px solid black; text-align:center; vertical-align: middle;' >SAFE</div>");

//When the PIR reveal a motion the square on the browser turn to yellow
if(state==HIGH){
alertBox.replace("background:green", "background:yellow");
alertBox.replace("SAFE", "WARNING!");
alertBox.replace("color: white", "color: black");
state=LOW;
}

//Print the alert box
client.println(alertBox);

client.print(F("</body>"));
client.println(F("</html>"));
client.print(DELIMITER); // very important to end the communication !!!
}

//The interrupt function
void allarm() {
state = HIGH;
}



OUTPUT

save status
 
  • If the PIR reveals some presence then it activates the warning signal:
warning status

Note: 
If you haven't configured your board then click here.