How to Create a Raspberry Pi Security Camera with Motion Alerts

Raspberry Pi Motion Detecting with IFTTT and Telegram
(Image credit: Linux Format)

It's easy enough to use your Raspberry Pi as a webcam or as a security camera that you look at in real-time, but what if you just want to be notified when your Pi sees motion? You can attach a passive infrared sensor (PIR) to act as a motion detector and then program your Raspberry Pi to send an alert to IFTTT (If This Then That), a web service that gets inputs from devices and acts on them. 

This message to IFTTT will trigger an output and image to be sent to a Telegram account that will send an alert to your mobile device. So, if an intruder walks by the sensor and you're not home, you will be notified.  

Latest Videos FromTom's Hardware
Les Pounder
Associate Editor

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • bit_user
    Thanks for the writeup of IFTTT and Telegram. That's all new to me.
    Reply
  • JanRob
    Many thanks for this article. I've been looking for something similar for a while, so am excited to get this going.

    However, I'm getting an error from the 'r = requests.post' line which reads:
    NameError: name 'new_filelink' is not defined

    The Pi is sensing motion, taking an image, and uploading to Filestack. IFTTT is sending a post to Telegram but the image is not coming through (getting a 'file not found' image).

    If I replace new_filelink.url in the r = requests.post line with "https://cdn.filestackcontent.com/theimageurl" then the image gets posted to Telegram, so the IFTTT to Telegram connection is working.

    It seems that there is an issue with the IFTTT post picking up the new_filelink.url.

    Please can you help!?

    Also the camera seems to be only triggering for the first motion sensor, and doesn't continue. Any ideas?

    Thanks for your help!
    Jan
    Reply
  • bit_user
    JanRob said:
    Thanks for your help!
    I'm not familiar with any of this, but the bottom of the article says it was first published in issue 262 of Linux Format Magazine.
    Maybe they have some forums where the same issue was already discussed (262 appears to be last month's issue).

    Also, that page has a link to the source code, so you can also review it to see if the article missed anything.
    Reply
  • JanRob
    bit_user said:
    I'm not familiar with any of this, but the bottom of the article says it was first published in issue 262 of Linux Format Magazine.
    Maybe they have some forums where the same issue was already discussed (262 appears to be last month's issue).

    Also, that page has a link to the source code, so you can also review it to see if the article missed anything.
    Thanks @bit_user! I did check online versions and it looks the same. Can't find any forums on this unfortunately. :(
    Reply
  • biglesp
    JanRob said:
    Thanks @bit_user! I did check online versions and it looks the same. Can't find any forums on this unfortunately. :(
    Hello!
    I wrote this code for Linux Format, so I hope that we can fix this.

    Firstly can you try out this code on your system, adding your API keys where necessary
    https://github.com/lesp/LXF262If that fixes the issue, then there was a typo, and trust me when I wrote this I made quite a few errors!
    new_filelink.url should return the url of the image on filestack. What does print(new_filelink.url) (Line 13) return in the Python shell?
    IIRC I had an issue like this where the URL was wrong and IFTTT was sending duff URLs to Telegram.

    If you can share your code (No API keys :) ) via a Gist or or Pastebin, then that would help me to diagnose the issue.

    Happy to help and best of luck!
    Les
    Reply
  • nachobiz13
    Hello, Thank you for writing this. I am also getting an error when I run the file. The motion detection is working because every time I move in front of the camera, a web address shows up in terminal and then 'Error' on a new line. Like this:
    https://cdn.filestackcontent.com/qm98PXufQKGbeqi93F09Error
    I am not sure what is going wrong but it seems to be with the send alert part of the code. I had to edit the file because, for some reason, it didn't like the quotes you had in there. I had to go through and replace them with new quotes. It kept giving me invalid character errors. Who knows? The edits fixed that but now I am running into this problem. Please let me know if you have any ideas. Also, if you know of a way to have it take a short video instead of a still upon motion detected, that would be cool to know. Thank you! : )
    Reply