Question

I have a script that runs in the terminal that polls a server every 2 seconds for a 0 or a 1 from a PHP script, and then executes a command based on the result.

Unimportant note: it's running on a raspberry pi.

So my question is: Is there a better way to communicate with the server? What would be ideal is something like a socket.io or html5 websocket type of connection, but thos specifically aren't doable from the terminal.

Here is what I have so that you get the gist

while :
 do

gpio mode 0 out
gpio mode 7 out

var1=$(curl http://url.com/index.php)
L1=$(echo $var1 | cut -c1-1)
L7=$(echo $var1 | cut -c7-7)
if [ $L1 == 0 ]; then
gpio write 0 0
elif [ $L1 == 1 ]; then
gpio write 0 1
fi

if [ $L7 == 0 ]; then
gpio write 7 0
elif [ $L7 == 1 ]; then
gpio write 7 1
fi

gpio readall
sleep 2

done

EDIT: I need to run around 1000 raspberry pi units using this on the same server, and i'm just looking for the most efficient method.

Was it helpful?

Solution

There are many ways of doing this. HTTP isn't the best suited protocoll for this (since it doesn't support sessions), but since you're already using it. Let's look at a solution for HTTP.

Instead of polling (which is expensive but simple to implement) you can push data from the server to the clients.

For doing this with HTTP, use COMET that is an umbrella term for a few different techiniques for doing this. See http://en.wikipedia.org/wiki/Comet_%28programming%29

Doing this with nginx you need an extra module, for example: https://github.com/wandenberg/nginx-push-stream-module

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top