Home Security System using ESP8266 WeMos D1 and PIR Motion Sensor

I cant believe this can be so difficult. I have a WeMos D1 board with builtin ESP8266 WiFi module and a PIR motion sensor. My intention is to make a home security alarm that interfaces with my Android mobile phone . So when there is an intruder detected the phone sends out an SMS and also takes photos via its android application which i still need to write.

With a basic sketch i can pick up the triggers when the PIR sensor picks up motion. This works fine. What i am really struggling with is posting a simple text message to a webserver page that changes the security status if an intruder is detected. All i need to have displayed on the webpage is a simple one line sentence like “Intruder Detected”. My android app will monitor the webpage content and take action accordingly.

Another problem that ive encountered is the Wifi board breaks communication with the Wifi router. This only seems to be the case when i include the code for the sensor detection within the void loop(). When the sensor code is omitted then the webpage displays.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>

//SSID and Password of your Wifi router on mobile phone access point
const char* ssid = "........";//Enter your router ssid
const char* password = "........";//Enter your router password 

ESP8266WebServer server(80);//Server on port 80

int sensorVal;//Our sensor value as analogue input to pin A0
int sensorPin = A0;//Analog input channel for output from PIR sensor

String securityStatus ="Secure";


//This routine runs when you open its IP in browser
void handleRoot(){

 char html[1000];

 //Build html page 
 snprintf(html, 1000,
"<html>\
   <head>\   
   <title>MyWebpage</title>\
   </head>\
   <body>\
    <center>\
     <h1>Security PIR Motion Detection Alarm System</h1>\       
    </center>\    
   </body>\
 </html>"
 );
 server.send(200,"text/html",html);//Send web page
  
}



void setup() {
  // put your setup code here, to run once:
 
 Serial.begin(115200);
 WiFi.begin(ssid,password); //Connect to Wifi router

 //Wait for connection
 while (WiFi.status() !=WL_CONNECTED){
  delay(1000);
  Serial.print(".");
 }

//If connection successful show IP Address in serial monitor on Arduino
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP Address:");
Serial.println(WiFi.localIP());//eg IP address assigned to is 192.168.43.52, Webpage can be 
                                        //viewed on PC or android phone using this IP
 
server.on("/",handleRoot);
server.begin();//start server
Serial.println("HTTP server started");

 
}


void loop() {
  // put your main code here, to run repeatedly:
 

/*
//Monitor input from PIR sensor. If this code is included the webpage wouldn't display
//because of the communication break with WeMos board

 sensorVal = analogRead(sensorPin);
  if (sensorVal >=1024) {
    //change security state
    securityStatus="Intruder detected";
    Serial.println("Intruder detected");
  }
*/


 server.handleClient();//Handle client requests
}

sensorVal = analogRead(sensorPin);
Why are you using an analog pin for a digital sensor? There is motion, or there isn’t. THAT is a digital sensor.

There is nothing in the handleRoot() function that includes anything about the presence/absence of motion.

The handleRoot() method is what gets called when a client requests the page. What client is requesting the page?

Are you certain that a cat or dog won’t set off the PIR sensor?

Hi

Thanks for your input.

I wasn't getting any reliable input on the digital pins so i tried out the analog and it worked fine. At least that's my understanding of this sensor.

On my mobile device or pc ive used my browser to access the IP address which displays the webpage. However this only will display if the sensor monitoring code is either commented out or omited. This is what i cannot understand. If i can resolve this then i can proceed to the next step outputing the security status to the webpage. Any suggestions how you would accomplish this with the least amount of effort.

A lizard maybe but no pets.

I wasn't getting any reliable input on the digital pins so i tried out the analog and it worked fine. At least that's my understanding of this sensor.

Maybe you should post a link to the device.

if (sensorVal >=1024) {

On an Arduino, the value returned by analogRead() will be in the range 0 to 1023. The body of the if statement will never be executed.

The Wemos is NOT a standard Arduino. You need to determine exactly which pins you can use for GPIO, and use them appropriately.

This is the link to the PIR sensor http://www.communica.co.za/Catalog/Details/P3250917876

I was getting around 3 V output and the if statement was executing as i could see the printout in the Arduino Monitor. This was for the simpler code without the html webpage.

Anyway ive changed the code again to digitalRead on pin 2 using pinMode(sensorPin, INPUT_PULLUP);.

While we talking its still compiling. Extremely slow compared to my standard Arduino Uno board.

Ok just completed and retested. Now the webpage loads but in the Arduino monitor its registering false triggers in succession.

http://www.communica.co.za/Catalog/Details/P3250917876

SME DIGITAL PIR MOTION SENSOR

Where did you get the idea that connecting that to an analog pin was a good idea? Where did you get the idea that testing for an impossible value was useful?

I don't mind taking some criticism. Please go on if it helps me find a solution.

You’ve changed your code, but not posted it again. I don’t like guessing how you’ve changed it.

You appear, though I could be wrong, to be just randomly trying pins, to see if they can be used. The pins, and how they are used on the ESP boards, are fairly well documented.

Instead of just randomly grabbing a pin to use, I think that a more systematic approach, involving reading some documentation, might be more useful.

Here are the pins on the WeMos board which one do you recommend i use with my PIR sensor?

Pin Function ESP-8266 Pin TX TXD TXD RX RXD RXD A0 Analog Input,max 3.3 V A0 D0 IO GPIO16 D1 IO, SCL GPIO5 D2 IO, SDA GPIO4 D3 IO, 10k Pull-up GPIO0 D4 IO, 10k Pull-up,BUILTIN_LED GPIO2 D5 IO, SCK GPIO14 D6 IO, MISO GPIO12 D7 IO,MOSI GPIO13 D8 IO, 10k Pull-down, SS GPIO15 G Ground GND 5V 5V - 3V3 3.3V 3.3V RST Reset RST

which one do you recommend i use with my PIR sensor?

D0 IO GPIO16

Ive relooked at the pins on my board after reading an article at

https://www.esp8266.com/viewtopic.php?f=32&t=7344&start=8

I had tried using pin 4 revised my code attached below. Seem to be going somewhere at least ive accomplished what ive been struggling with. Will need to relook at revising the code and check the sensitivity of the PIR.

Thanks for all your help much appreciated.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>

//SSID and Password of your Wifi router on mobile phone access point
const char* ssid = ".........";
const char* password = "........";

ESP8266WebServer server(80);//Server on port 80

int sensorVal;//Our sensor value as digital input
int sensorPin = 4;//D14/SDA/D4 or D14/SDA


char* securityStatus ="Secure";
int numhits=0;

//This routine runs when you open its IP in browser
void handleRoot(){

 char html[1000];

 //Build html page refresh every 10 secs
 snprintf(html, 1000,
"<html>\
   <head>\
   <meta http-equiv='refresh' content='10'/>\   
   <title>MyWebpage</title>\
   </head>\
   <body>\
    <center>\
     <h1>Security PIR Motion Detection Alarm System</h1>\
     <p>Security Status: %s</p>\
     <p>No of hits: %d</p>\       
    </center>\    
   </body>\
 </html>",securityStatus,numhits
 );
 server.send(200,"text/html",html);//Send web page
  
}



void setup() {
  // put your setup code here, to run once:
 pinMode(sensorPin, INPUT);//configure pin 4 as an input 
 Serial.begin(115200);
 WiFi.begin(ssid,password); //Connect to Wifi router

 //Wait for connection
 while (WiFi.status() !=WL_CONNECTED){
  delay(1000);
  Serial.print(".");
 }

//If connection successful show IP Address in serial monitor on Arduino
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP Address:");
Serial.println(WiFi.localIP());

server.on("/",handleRoot);
server.begin();//start server
Serial.println("HTTP server started");

 
}


void loop() {
  // put your main code here, to run repeatedly:
 
//Monitor input from PIR sensor
 sensorVal = digitalRead(sensorPin);
 
 if(sensorVal==HIGH){  
    securityStatus="Intruder detected";
    Serial.println("Intruder detected");
    numhits++;
 }
 else{
    securityStatus="Secure";
    Serial.println("Secure");
 }
  
 server.handleClient();//Handle client requests
}