0

I'm connecting my raspberry pi with PIR sensor... I wrote Python script to get the sensor's input... Now I want the output from the python script in PHP for further actions in my website... I tried the following code: This is the PHP code:

<?php    
    $output=shell_exec('python/sense1.py');
    echo $output;
?>

This is the Python code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
PIR_PIN = 26
GPIO.setup(PIR_PIN, GPIO.IN)
while True:
    time.sleep(1)
    if GPIO.input(PIR_PIN):
        print ("Human Moving")
    else:
        print ("No human moving")

I tried single print statement in python and got the output in PHP... But now the problem is Python script is returning output for every 1 second infinitely... So I'm unable to get the output from the python script(becoz of infinite loop)... But I want the output for every one second to check the PIR sensors status... How can I do that??? Please Help me out...

  • 1
    Possible duplicate of [Run process with realtime output in PHP](http://stackoverflow.com/questions/1281140/run-process-with-realtime-output-in-php) – insertusernamehere Feb 06 '17 at 16:13
  • Hey you can save your each and every log of sensor activity to a local sqlite Db or any other DB and run a script to update to your central DB where your PHP app connected with. – BetaDev Feb 06 '17 at 16:16
  • Another possible way is, create a REST API endpoint using python flask and put this code inside a rest endpoint code in flask. flask is light weight and just to write a small file including this sensor code. Call this rest endpoint from you php easily using GET or POST method. – BetaDev Feb 06 '17 at 16:18

2 Answers2

0

I am doing something similar with Temperature sensor with Raspberry PI. I am getting temperature level of a room contentiously by calling a REST API that i have create using python flask.

Here What I am doing (you just need to replace the Temperature Sensor code with your sensor code)

MY python flask code:

@app.route('/room-temp') //This is my get rest endpoint, i call from php
@requires_auth  //You dont need authentication, so you should remove this
def room_temp():
    pi = pigpio.pi()
    pigpio.exceptions = False
    c, files = pi.file_list("/sys/bus/w1/devices/28-00*/w1_slave")
    pigpio.exceptions = True
    if c >= 0:
       for sensor in files[:-1].split("\n"):
          devid = sensor.split("/")[5] # Fifth field is the device Id.

          h = pi.file_open(sensor, pigpio.FILE_READ)
          c, data = pi.file_read(h, 1000) # 1000 is plenty to read full file.
          pi.file_close(h)

          if "YES" in data:
             #gc.collect()
             (discard, sep, reading) = data.partition(' t=')
             t = float(reading) / 1000.0
             #print("{} {:.1f}".format(devid, t))
             data5 = {'temp':"{} {:.1f}".format(devid, t)}
             pi.stop()
             return jsonify(data5)
          else:
             #print("999.9")
             #gc.collect()
             data6 = {'temp':"999.9"}
             return jsonify(data6)

I am running this flask api in my Raspberry and I make ajax call from my PHP app. I make AJAX call to http://raspberryip:port/room-temp from my PHP.

Note: Just you need to see REST, REST with Python Flask (Link), and AJAX from PHP.

This is very simple way to get result from sensors, but the problem is that will take some milliseconds to place a AJAX request to your REST endpoint written in python flask. So you need to store you each and every sensor activity to you local DB by running you python script continuously and you can get values from that light weighted DB. After fetching from Raspberry you can delete records from Raspberry to ignore overhead.
For All activity on sensor.
My Recommendation: Please store you sensor result to a local Database and then write a REST endpoint to get that records from DB. Place your AJAX call from you PHP app to that REST endpoint to get data from local DB of that Raspberry.

BetaDev
  • 4,516
  • 3
  • 21
  • 47
-2

In your python Script, you should write the sensor value on a text file (Instead of displaying it) and read it with php in a loop.

Or just remove the loop from your python script. Then, when you'll launch your python script, it will output just one value, and you'll do your script in php. The problem is that the PHP will generate your page once and not reload the value. Then you can just reload the page every second to actualize the value.

saperlipopette
  • 177
  • 1
  • 12
  • The sensor must read activity continuously, the sensor must log each activity and report to PHP. – BetaDev Feb 06 '17 at 16:20
  • I don't understand why you underscore my answer. This is a way of doing I use at home with my equipment and it's perfectly working. The sensor reads data continously, it's just that instead of a loop you re-launch your script. – saperlipopette Feb 06 '17 at 17:35
  • I did not underscore buddy, I just commented. – BetaDev Feb 06 '17 at 18:05