rybo
Posts: 6
Joined: Tue Apr 19, 2016 9:59 pm

Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:10 pm

Is there a way to detect fallback from a sensor using only php? I see you can use shell_exec to activate a pin but how can I read the output? I would like to have a php file detect the fallback in real-time using ajax..

Heater
Posts: 16091
Joined: Tue Jul 17, 2012 3:02 pm

Re: Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:35 pm

rybo,

I have no idea what you mean by "fallback".

What vibration sensor?

In general web servers running PHP scripts only run them when a browser requests a URL from the server.

That means that in order to get updates the browser has to poll the server, often using some Javascript in the web page to make an AJAX request.

This is slow and crude.

A better approach is to:

1) Throw away Apache and PHP

2) Create a little HTTP server in Javascript using node.js.

3) Have that server read your GPIO or whatever.

4) Have the server "push" updates from the GPIO to the web page using websockets.

Now you have a system where whatever data updates you have on the server, GPIO chages etc, can be seen in the web page as they happen.

I have tried to create the smallest, simplest example I can think of that does what you want here https://bitbucket.org/zicog/pigpio2html

Basically it relays GPIO input changes to "LEDs" on the web page. And it relays clicks on the web page to outputs on the GPIO.
Last edited by Heater on Wed May 11, 2016 5:40 pm, edited 1 time in total.
Memory in C++ is a leaky abstraction .

rybo
Posts: 6
Joined: Tue Apr 19, 2016 9:59 pm

Re: Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:40 pm

I am currently doing this using python that has a function to detect "fallback" from a vibration sensor I have setup on the pi. It works great but I am limited using python. I have a sqlite DB that stores each time a sensor is vibrated and then pull that data using php to display in a web browser. Like I said it all works great now I would just like to do away with python and be able to detect the sensor using php.

User avatar
joan
Posts: 14959
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:45 pm

I guess by fallback you mean callback, i.e. your Python is called when a GPIO changes state.

You will have to write a PHP module to duplicate the actions of the Python module.

rybo
Posts: 6
Joined: Tue Apr 19, 2016 9:59 pm

Re: Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:48 pm

Yes it uses a callback to reference a function but what Im reffering to as fallback is GPIO.FALLING

GPIO.add_event_detect( vib_sensor, GPIO.FALLING, callback=vib, bouncetime=2000 )

Heater
Posts: 16091
Joined: Tue Jul 17, 2012 3:02 pm

Re: Use PHP to detect fallback from vibration sensor

Wed May 11, 2016 5:56 pm

Just do it in node.js. Much easier.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”