1

I have a raspberry pi3 running this project from instructables.com Simple and intuitive web interface for your Raspberry Pi

I need to make the buttons function so that only one user at a time can push a button while a lot of users can view the page. This is to control a pan/tilt web camera where Button Zero pans the camera left, Button One pans the camera right, etc. The raspberry drives relays that drive the motors in the Pelco Camera Pan/tilt mount. I can't have one user trying to pan left while another user on a different http connection tries to pan right. There is no log-in to access this raspberry.

Is there an Apache2 setting to accomplish this? I don't think this can be solved with adding code to the GPIO.php file, or is there? Can I use a semophore or $global flag to limit button actuation with multiple concurrent viewers?

Mojorizing
  • 25
  • 4
  • 2
    This is not a simple problem that can be given a quick answer. You will need to do it on the PHP level and it will require some careful planning. –  Dec 27 '16 at 02:29

1 Answers1

0

You need a mutex lock. Two quick and dirty ways I may suggest.

Lock a local file on the system

Open the file, and lock it for writing.

During this time if another user attempted to control the camera you would first attempt to open this file with and lock it for writing and it would fail with an error.

At that point you know you can't give the second user control.

PHP lock a file for writing

Note: You could use multiple different files for different locks. For example if you had multiple cameras then you could use multiple files to lock, one for each camera.

Use a database like MySQL

Using a database like MySQL you can lock a specific row in a table, effectively doing the same thing as we did with a file in the last example.

If a second user comes along we again attempt to lock that same row and we will fail, at this point we can reject the second user's request.

Lock a single row in MySQL

Note: You can user multiple rows, where each row may represent a different camera as mentioned above.

Other things to consider

I highly recommend providing your users the ability to see if they are they current user or not, and implementing a way to fairly switch between users so that a single user can't hog all the fun. Perhaps something as simple as a 15 or 30 second timer which switches control between the current active users.

Community
  • 1
  • 1
Joshua Briefman
  • 3,783
  • 2
  • 22
  • 33
  • 1
    Please consider not linking to w3schools, it hurts community eyes. Thanks. – sepehr Dec 27 '16 at 07:44
  • Thanks for the reply Joshua. I see that a PHP lock will work for giving access to only one user to the gpio.php file at a time, but not sure how to implement a timer to limit the access time to the php file from "outside" the gpio.php file – Mojorizing Dec 28 '16 at 19:17
  • @Mojorizing That is a question for another post. I'd like to recommend you try to solve before asking for the solution. Attempting to solve something, even if you end up not being able to, helps to further develop your understanding of it. Here are a hint. Cookies, client side JavaScript rest query (on timer) and a small database, time is important but order is to, if they step out of line, then the next turn is for you. – Joshua Briefman Dec 28 '16 at 23:04
  • Thanks for the hints - I will look into the use of cookies, etc. – Mojorizing Jan 02 '17 at 17:50