Posts: 3
Joined: Tue Mar 21, 2017 1:45 pm

display message when button pressed in php?

Wed Mar 22, 2017 11:20 am

How to display a message on a webpage when my button on pin 18 from my raspberry pi 3 is been pressed in php ?

Posts: 16544
Joined: Tue Jul 17, 2012 3:02 pm

Re: display message when button pressed in php?

Wed Mar 22, 2017 1:49 pm

Not easy.

As a far as I know the only way to do this is have some Javascript in your page that makes an AJAX request to some URL on the server every second or so. On receiving that request the PHP can look at the GPIO and return something to indicate it's state. On getting the response the Javascript can update whatever on the page to indicate the GPIO state.

This "polling" technique is slow and clunky.

Better is to have Javascript in your page open a web socket to a web socket server on the Pi. That web socket server can be made to detect GPIO changes and send them immediately to the page for display update.

Easiest way to make such a web socket server is to use node.js.

Of course if you are going to do that it starts to become easier to throw away Apache and PHP and do the whole webserver in Javascript with node.js.

See example of GPIO to page and page to GPIO using node and websockets here:
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”