1

Basically, I'm building an IoT oriented app. I created a couple binary files that turns on or off the pins of a Raspberry Pi. I have used PHP (running on the Raspberry Pi) to run those programs by executing a bash command in the past, which worked perfectly, like so:

exec('sudo /home/pi/Projects/calelec/rpi/on');

But what I need to do now, is to constantly read (long poll) an API service that will tell my Raspberry pi any instruction, and then execute something like the command above.

I know a cronjob will be ideal for this task (so I don't need to use the PHP part), but I need this to have a latency of .3 seconds.

I have read before that a daemon could work, but on that I know almost nothing. So I just need to be pointed to the right/better direction.

Multitut
  • 1,937
  • 4
  • 36
  • 53
  • You could indeed write a daemon: http://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux#17955149 this looks more complicated than it is, but I think it would be a good solution. A quicker way could be writing a bash file, that starts an endless loop, sleeping for 0.3 sec and then does your API calls, then end and restart loop. If you want to try that, I can try to provide an example as answer – Bolli Jan 12 '17 at 15:12
  • Thanks +Bolli, an example would be awesome – Multitut Jan 12 '17 at 15:13
  • Just to be sure I understand what you want, before doing the example. You want to run a command or API call every 0.3 sec - in the background contentiously? – Bolli Jan 12 '17 at 15:15
  • Yes, it will be a call to an API using HTTP GET. – Multitut Jan 12 '17 at 15:18
  • Have you tried using `forever` – Karan Shah Jan 12 '17 at 15:30

1 Answers1

0

Here is an example using a endless loop with bash:

#!/bin/bash
while :
do
    # Write your code below, you can interact with API's using curl
    curl  http://google.com

    sleep 0.3 # You might need to adjust this, if the request takes more time to complete
done

Save it as test.sh and give it executable rights chmod +x test.sh and run with ./test.sh

You can stop it from running by hitting CTRL+C

This script will do a GET request to google.com every 0.3 sec. You can write your own curl requests and commands withing this loop, that you want to run every 0.3 sec.

You can read the curl man page here

Bolli
  • 4,714
  • 5
  • 29
  • 45