0

I am trying to build a motion sensor with my Pi that can deliver notification to a program on my computer when it detects motion. I installed apache on the Pi, the server works. I wrote a very simple python script for motion detection which writes the file 'motion' when it detects something, and deletes it otherwise.

My index.php looks something like this:

<?php
        while(!file_exists('motion')) {
                sleep(0.5);
        }

        echo "MOTION DETECTED";
?>

The program on my computer tries to access the page every 60s until it has a response, in which case it delivers a notification.

Everything works well, but it seems to me that writing a file accesses the disk which is slower than accessing the RAM. I was wondering if there is anyway to communicate between the python script and the php script without having to write a file (or using a file related method). This would also prevent errors in file reading/writing permissons.

Thank you.

2 Answers 2

1

I assume you have:

  • on your PC: a program that polls every 60s through HTTP a URL on Raspbery Pi
  • on Raspberry PI: a PHP script that is called to respond to the HTTP request
  • also on Raspberry PI: a python script that monitors for motion in a loop

One option is to connect the PHP and the RaspberryPI through sockets:

  • the python script, in addition to monitoring for motion, would listen to TCP socket connections to some port (e.g. 5000)
  • the PHP script would: open a TCP socket to the python script, get the data, close the socket

Another option would be to:

  • have the python script serve HTTP on some port (e.g. 5000)
  • use nginx to route specific requests to the python script

This would not use apache and PHP script, but would save you from writing the code to connect from PHP to the python script.

Another option would be to:

  • have a HTTP or TCP socket server running on your PC
  • connect from the python script directly to your PC

This would require you to know and be able to access the IP of the PC from the Raspbery Pi. It has the advantage that it eliminates the polling, so you'll get the notifications faster.

Also note, in your example:

 while(!file_exists('motion')) {
            sleep(0.5);
    }

would this not loop for ever if the file does not exist (i.e. if there is no motion)?

1

I have no idea what the best way would be. You'd have to give some criteria to judge against.

My pigpio Python module supports this mode of operation.

You have the pigpio daemon running on the Pi. You have a Python script running on your PC (Windows, Linux, Mac etc.) which connects to the pigpio daemon over the network. The script will see the Pi's GPIO as if they are on the PC.

The following example runs on a PC and connects to a Pi with network name "paul". It prints a line each time GPIO 4 changes state.

# master.py

import time
import pigpio

GPIO=4

def callback_func(gpio, level, tick):
   print("g={} l={} t={}".format(gpio, level, tick))

pi = pigpio.pi("paul") # Connect to paul.

cb = pi.callback(GPIO, pigpio.EITHER_EDGE, callback_func)

time.sleep(30)

cb.cancel()

pi.stop() # Disconnect from paul.

Example output.

python master.py

g=4 l=0 t=3365926169
g=4 l=1 t=3368206735
g=4 l=0 t=3370278655
g=4 l=1 t=3373045107
g=4 l=0 t=3375255898

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.