A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By eqsOne
#46970 Here's my attempt on receiving a push message on detected motion back home.

It uses an ESP8266(V1) connected to my WiFi router, Pushover App & service (iOS/Android) for the messaging part and an HC-SR501 passive infrared sensor.
Arduino IDE is used for programming.

It's running for 5 days now indoors without any false positives within the shown setup. Seems promising..


Wiring

Standalone:
PIR sensor board.jpg


For programming - in case you don't already have a solution:
ESP Porgramming Board.jpg



PIR Sensor (HC-SR501)

- Jumper set to L mode (No reset)*
- Sensivity set to around 4-5m range (approx. 11 o'clock)
- Triggering time set to about a minute (approx. 9 o'clock)

*Note on the version without jumper: Check for a small trace on your circuit board between the middle pad and H-pad and cut it in case it's there when joining middle- to L-pad.

PIR sensor settings.jpg



Housing

Sensor and ESP are housed inside this 50mm (~2 Inch) plastic case. The PIR's lens is jammed into a drilled hole at the frontside (slightly bigger than lens diameter) with a flat rubber band between, keeping it tight (cosists of TPU, kind of tear- and frost resistant elastic band used for kitchen stuff). ESP and PIR are kept approx. 35mm away from each other, facing backside to backside.

Housing.jpg


Housing inside.jpg



Notes

To keep RF noise low in order to prevent false positives, I left the power supply outside the housing using a USB charger and reduced the ESP’s WiFi transmittig power in code. You may play around with this value as you’ll have different conditions and environment than I do. With the same setup at full transmit power, I had about 6 false positives occuring per 24 hours.

A licensed version of Pushover is required (after a 7 day trial) as you'll have to create a so-called App within your Pushover account, which is actually kind of a virtual sender, defined by a name and unique token that will be part of the sketch. A license currently costs €4,99 as in-app purchase for iOS/Android.

Generally it takes about 5 seconds from detected motion to actually receiving the push on my devices, biggest lag was about 20 seconds (iOS) so far.
If you’d prefer using some other push-, mail- or data service, just change to it within the sketch according to its related API.

Since the triggering time is set to a minute on the PIR sensor, it will send max. one message per minute on constant movement. You may change that by altering the trigger time on the PIR to your likes. For testing and adjustment I’d recommend to comment the pushover line in code and just work with the serial output instead.

Additionaly I use a common WiFi plug switch (TP-Link HS100 in particular) to have it capable of being activated or deactivated from anywhere outside my network just in case.. There might be cheaper solutions but you may at least consider some option on enabling such feature.

Happy tinkering.


Code: Select all//////////////////////////////////////////////////
// ESP8266 Pushover message on motion detection //
//////////////////////////////////////////////////

#include <ESP8266WiFi.h>
#include <ESP8266WiFiGeneric.h>

// Sensor
const int Sensor = 2; //PIR out at GPIO 2
bool lastButtonState = 1;
int buttonState = LOW;
int calTime = 15; //Period of time (in sec.) to let the PIR settle in setup

// Pushover
String Token  = "your_Pushover_App_token";
String User   = "your_Pushover_user_ID";
int length;

//  WiFi
WiFiClient client;
const char* ssid     = "your_WiFi_name";
const char* password = "your_WiFi_password";

// Setup
void setup() {
  Serial.begin(115200);

  // WiFi setup
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA); //Client mode only
  WiFi.setOutputPower(2.5); //Limiting output power (0 - 20.5)
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("_");
  }
  Serial.println("");
  Serial.print(ssid);
  Serial.println(" successfully connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  delay(50);

  //Sensor setup
  pinMode(Sensor, INPUT);
  digitalWrite (Sensor, LOW);
  Serial.println("");
  Serial.println("Sensor calibrating...");
  for (int i = 0; i < calTime; i++) {
      Serial.print("_");
      delay(1000);
      }
  Serial.println("");
  Serial.println("Motion detection activated");
  pushover("Motion detection activated");
  Serial.println("");
}

void loop() {
  int buttonState = digitalRead(Sensor);
  if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
    Serial.println("Motion detected");
    pushover("Motion detected");
  }
  lastButtonState = buttonState;
  delay(300);
  }
}

byte pushover(char *pushovermessage) {
 
  String Msg = pushovermessage;
  length = 81 + Msg.length();
    if (client.connect("api.pushover.net", 80)) {
      Serial.println("Sending message…");
      client.println("POST /1/messages.json HTTP/1.1");
      client.println("Host: api.pushover.net");
      client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.print(length);
      client.println("\r\n");
      client.print("token="+Token+"&user="+User+"&message="+Msg);
      /* Uncomment this to receive a reply from Pushover server:
      while(client.connected()) {
        while(client.available()) {
          char ch = client.read();
          Serial.write(ch);
        }
      }
      */
      client.stop();
      Serial.println("Done");
      Serial.println("");
      delay(100);
    }
}
You do not have the required permissions to view the files attached to this post.