RPi Cam Web Interface: Turn on and off motion detection usng IFTTT Do Buttons

EDIT – Updated March 2018 to include a more secure raspi_runner.sh script and updated IFTTT instructions.

Would you like the ability to turn on and off motion detection on your Rpi Cam Web Interface from your phone with just the press of an IFTTT Applet button (rather than logging into the camera’s website)?

An extra bonus on the iPhone is the ability to add a button to the home screen widget, so it’s really easy to control your camera. Plus it’s not limited to motion detection – anything the pipe commands can do (http://elinux.org/RPi-Cam-Web-Interface#Pipe), the Applet button can do too!

  1. Install Dropbox-Uploader
  2. cd ~
    git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
    cd Dropbox-Uploader/
    chmod +x dropbox_uploader.sh
    ./dropbox_uploader.sh
    
  3. Follow the Dropbox-Uploader instructions:
  4. - Open the following URL in your Browser, and log in using your account: <a class="postlink" href="https://www.dropbox.com/developers/apps">https://www.dropbox.com/developers/apps</a>
    
    - Click on "Create App", then select "Dropbox API app"
    - Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder
    - Enter the "App Name" that you prefer (e.g. MyPiUploader)
    
    - Now, click on the "Create App" button.
    
    - When your new App is successfully created, please click on the Generate button
    under the 'Generated access token' section, then copy and paste the new access token here:
    
    # Access token:
    
    
  5. Install Raspi-Runner
  6. 
    cd ~
    git clone https://github.com/enkydu/Raspi_Runner
    cd Raspi_Runner/
    chmod +x raspi_runner.sh
    ./raspi_runner.sh
    What is name of Dropbox folder, for Raspi Runner commands? (i.e. Raspi_Commands): scripts
    what is the full path to your Dropbox Uploader? (i.e. /home/pi/Dropbox_Uploader): /home/pi/Dropbox-Uploader
    
    

    Note – ensure the path has Dropbox-Uploader instead of the default Dropbox_Uploader

  7. We now need to modify raspi_runner.sh to be more secure. As things stand, any command posted into dropbox can be executed on the Pi which isn’t good, so the solution is to have pre-defined commands instead.
    Edit /home/pi/Raspi_Runner/raspi_runner.sh and replace text with the following code:
  8. #!/bin/bash
    
    # Check location of Raspi Runner
    script_path="$(readlink -f ${BASH_SOURCE[0]})"
    rr_home="$(dirname $script_path)"
    
    cd $rr_home
    
    rr_dboxstorage=scripts
    rr_storage=/home/pi/Raspi_Runner/scripts
    rr_dbuploader=/home/pi/Dropbox-Uploader
    
    # Download new scripts delivered by mail from Dropbox to Raspberry Pi folder /home/pi/Raspi_Runner/Raspi_Commands
    $rr_dbuploader/dropbox_uploader.sh -q download /$rr_dboxstorage
    
    # Check for new files on Raspberry Pi
    check=`ls $rr_storage | wc -l`
    
    if [[ $check -eq 0 ]];
            then exit 0
    fi
    
    files=`ls $rr_storage`
    OIFS="$IFS"
    IFS=$'\n'
    
    # Run all delivered scripts
    currTime=`date +"%d-%m-%Y %T"`
    for i in $files
    do
      command=`cat ${rr_storage}/$i`
      echo -e "$currTime - $command" >> $rr_home/cmdlog.txt
      case "$command" in
        motionon)
          echo 'md 1' > /var/www/html/FIFO
          ;;
        motionoff)
          echo 'md 0' > /var/www/html/FIFO
          ;;
      esac
    
    done
    
    # Remove all scripts, which were already executed from Dropbox
    for i in $files
    do
            $rr_dbuploader/dropbox_uploader.sh -q remove /$rr_dboxstorage/$i
    done
    
    # Remove all scripts, which were already executed from Raspberry Pi
    rm $rr_storage/*
    
  9. Test the new code by creating a text file in the scripts folder within your dropbox storage area (via the web or your computer if Dropbox is installed) with the following text inside that file (any filename will be ok):
  10. motionon
  11. Run the raspi_runner.sh command as shown below. The script will connect to Dropbox, download any files within the scripts folder, read the contents of that text file, and if there is a match (e.g. ‘motionon’) it will run the associated command defined within our script (in this case, turn on motion detection)
  12. /home/pi/Raspi_Runner/raspi_runner.sh
  13. Now set a schedule in cron so that raspi_runner executes every 2 minutes:
  14. crontab -e
  15. Paste the following line and save
  16. */2 * * * * /home/pi/Raspi_Runner/raspi_runner.sh > /dev/null 2>&1
  17. Create an IFTTT Applet (assumes you’ve created an IFTTT account and linked DropBox to it)
    1. Browse to the IFTTT website https://ifttt.com/discover
    2. Go to the My Applets page (link top of the site)
    3. Press the New Applet button
    4. Press the blue coloured +this within the large ‘If This Then That’ text
    5. Search for Button Widget and press the big red box
    6. Select the Button Press red box
    7. Now select +that
    8. Search for and select Dropbox
    9. Choose Create a text file
    10. Complete the action fields as follows
      1. File Name, e.g.
      2. anyfilename
      3. Content, e.g.
      4. motionon
      5. Dropbox folder path, e.g.
      6. scripts
    11. Click Create Action
    12. Give this action a name such as Turn on Motion
    13. Click Finish

    What’s nice about this method is that you can turn off motion detection in the middle of a recording, whereas the website button to turn motion off is greyed out until no more motion is detected.

    You can now use the iPhone app (and probably Android as well) to create a Widget for easy access to initiate the Applet.

    Advanced Extras
    If you have followed my guide “Full Reverse Proxy Instructions with Dynamic IP and HTTPS Encryption“, you could install Raspi Runner on the proxy pi and then issue commands to other cameras or Linux devices on your network using ssh.
    For example, after configuring SSH Public Key Authentication on your proxy, you can use the following command in raspi-runner.sh instead:

          cameraon)
          echo "md 1" | ssh picam 'cat > /var/www/html/FIFO'
          ;;
    

One thought on “RPi Cam Web Interface: Turn on and off motion detection usng IFTTT Do Buttons

Leave a comment