Introduction

This article improves the design earlier shown in "Motion detector with ESP8266 and HC-SR501". The purpose of this device is to allow full usage of the activate and deactivate signal provided by the HC-SR501. This meant that when the status of the HC-SR501 changes (either from active → inactive or inactive → active) the ESP8266 needs to be woken up and transmits the status of the HC-SR501.

 

Step 1: Designing the trigger electronics

  • Repeat Trigger (as long as there is motion, the output will be kept high)
  • Time delay variable
  • Wakeup needs to occur when output changes
  • On wakeup state of output needs to be transmitted

 

 

The circuit is simulated using: http://www.falstad.com/circuit/circuitjs.html

Code used:

$ 1 0.000001 19.867427341514983 69 5 50
r 336 352 336 448 0 47000
g 336 448 336 464 0
w 528 272 336 272 0
d 176 272 336 272 3 default
r 336 352 432 352 0 47000
w 528 272 528 352 0
I 176 352 272 352 0 0.5 3.3
I 432 352 528 352 0 0.5 3.3
I 96 352 176 352 0 0.5 3.3
w 176 352 176 272 0
c 272 352 336 352 0 1e-7 0.006907472761369231
x -144 332 50 335 4 16 Output\sPIR\sto\sinput\sInverter
x 781 425 921 428 4 16 ESP8266\s\s(RESET)
x 781 446 1001 449 4 12 Do\snot\suse\sGPIO16\s(else\sit\swill\snot\swork)
L 96 352 48 352 0 0 false 3.3 0
x 121 399 167 402 4 12 74HC14
x 573 491 619 494 4 12 74HC08
c 272 592 336 592 0 1e-7 3.293092345850044
w 176 592 176 512 0
I 432 592 528 592 0 0.5 3.3
I 176 592 272 592 0 0.5 3.3
w 528 512 528 592 0
r 336 592 432 592 0 47000
d 176 512 336 512 3 default
w 528 512 336 512 0
g 336 688 336 704 0
r 336 592 336 688 0 47000
w 96 352 96 592 0
w 96 592 176 592 0
150 528 432 672 432 0 2 3.3 3.3
w 528 352 528 416 0
w 528 512 528 448 0
w 96 352 96 224 0
w 96 224 672 224 0
w 672 224 672 320 0
x 776 318 904 321 4 16 ESP8266\sGPIOxx
207 672 432 752 432 0 Reset
207 672 320 752 320 0 Data
x 776 338 883 341 4 12 For\sexample\sGPIO4
o 37 64 0 4099 5 0.00009765625 0 2 37 3
o 36 64 0 4099 5 0.00009765625 0 2 36 3 

 

Step 2: Setting up Domoticz

Same as in previous article.

 

Step 3: The ESP8266 code

/* PIR Advanced motion Detection with deep-sleep mode */

ADC_MODE(ADC_VCC); //vcc read

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// WiFi credentials & Domoticz API IDX.
const char* WIFI_SSID = "<YOUR_WIFI_SSID>";
const char* WIFI_PASS = "<YOUR_WIFI_PASS>";
const char* DOMOTICZ_SRV = "http://192.168.123.100:8080";
const char* DOMOTICZ_SWITCH_IDX = "45";
const char* DOMOTICZ_VOLT_IDX = "22";
const int PIRSignalPin = 14; //Using GPIO14 for PIR Data

void setup() {
  HTTPClient http;      // HTTP object
  String api;           // Holds full API string for Domoticz
  int httpCode;         // Return HTTP code
  String response;      // Response from HTTP GET request
  float vdd;            // Internal voltage
  String s_vdd;         // Internal voltage (formatted as string)
  
  Serial.begin(115200);
  Serial.setTimeout(2000);

  while (!Serial) { }   // Wait for serial to initialize.
  
  Serial.println("");
  Serial.println("-------------------------------------");
  Serial.println("PIR Motion Detection Firmware!");
  Serial.println("-------------------------------------");

  // Connect to Wifi.
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    // Check to see if
    if (WiFi.status() == WL_CONNECT_FAILED) {
      Serial.println("");
      Serial.println("Failed to connect to WiFi. Please verify credentials!");
      delay(10000);
    } else {
      delay(500);
      Serial.print(".");
    } 
  }

  Serial.println("");
  Serial.print("WiFi connected with IP address: ");
  Serial.println(WiFi.localIP());

  // Send post request to Domoticz
  Serial.println("");
  
  Serial.println("Sending On signal to Domoticz.");
  //Build API command
  api = DOMOTICZ_SRV;
  api += "/json.htm?type=command&param=switchlight&idx=";
  api += DOMOTICZ_SWITCH_IDX;
  if (digitalRead(PIRSignalPin)==HIGH) {
    api += "&switchcmd=On";
  } else {
    DEBUGPRINTLN0("Sending 'Off' signal to Domoticz.");
    api += "&switchcmd=Off";
  }    
  http.begin(api);                        //Specify request destination
  httpCode = http.GET();                  //Send the request and set return code
  Serial.print("Return code: ");
  Serial.println(httpCode);
  if (httpCode > 0) {                     //Check the returning code  
    response = http.getString();          //Get the request response
    Serial.println("Response:");
    Serial.println(response);
  } 
  http.end();                             //Close connection

  delay(200);

  Serial.print("Sending voltage to Domoticz: ");
  vdd = ESP.getVcc() / 1024.0f;
  s_vdd = String(vdd, 3);
  Serial.println(s_vdd);
 
  api = DOMOTICZ_SRV;
  api += "/json.htm?type=command&param=udevice&idx=";
  api += DOMOTICZ_VOLT_IDX;
  api += "&nvalue=0&svalue=";
  api += s_vdd;
  http.begin(api);                        //Specify request destination
  httpCode = http.GET();                  //Send the request and set return code
  Serial.print("Return code: ");
  Serial.println(httpCode);
  if (httpCode > 0) {                     //Check the returning code  
    response = http.getString();          //Get the request response
    Serial.println("Response:");
    Serial.println(response);
  } 
  http.end();                             //Close connection
  
  Serial.println("");
  Serial.println("Going into deep sleep!");
  ESP.deepSleep(0); // forever

}

void loop() {
  // Never reached!
}

When there is motion detected, the ESP8266 is awoken. It switch on/off command and the voltage is send to the Domoticz server.

 

Working device: