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.