2

I'm not exactly the most knowledgeable on the server-side aspects of how this kind of thing should/would work, but I do tend to pick things up pretty quickly.

I've played around with WebIOPi and a few different scripts running from the pi itself etc, but I was wondering what the steps involved would be to implement a similar kind of thing, but running from an external website.

Essentially, I'd like to be able to control the Pi from any internet connection: run pre-written scripts or just turn pins on/off. I can't seem to find much information on how to do this that starts at a more basic level with some descriptions of WHY you're doing certain things to make stuff work, which is problematic when you're trying to learn.

Just a basic outline of the steps to be implemented would be helpful, but a link or to to descriptions of what they do and why you need them would be amazing.

goldilocks
  • 60,325
  • 17
  • 117
  • 234
Laurengineer
  • 161
  • 10

2 Answers2

2

Following Gerben's comment on the question, one way to make that would be implementing a Message Broker and have a client poll for messages from a server with a known and static address (or a DNS entry that follows a dynamic address).

I suggest taking a look at RabbitMQ. There is also a tutorial on how to install it in the Pi. Consider that the server will be in the remote machine, and you will need a client installed in the Pi with the GPIO.

You can find a list of clients for many programing languages in RabbitMQ's devtools page.

Marco Poli
  • 1,871
  • 10
  • 21
1

Basically you have to do the following steps:

First you have to be able to access your Pi from any network. To do this, go to any Dynamic DNS Host and register. Services like DynDNS or no-ip provide an internet address for your home router.

Than go to your Routers IP-Adress and see if there is already a build in functionallity for Dynamic DNS. You need to configure this, so you router sends his external IP-Adress to you DynDNS Host. This will make your router accessible. If not, than use software on the pi to send the IP-Adress.

Either set your Pi to an static IP or tell the router in his settings to give the pi an static IP and then forward Ports 80 and 443 to this IP-Adress. With this step you tell the router to forward every incomming traffic on the two ports to the raspberry pi.

Set up an webserver like apache on your Pi in order to provide a webserver funtionality.

There are definitely many ways to access the GPIO pins from a website, one way is:

Write a php script for your website. This is the page you will see when you access your pi.

Within it should be buttons and stuff so you can control something end send it via POST method to the script.

Extract the message and call a python script which will do the actual change on the GPIO.

$command = escapeshellcmd(sudo python /home/pi/path/to/script.py led 1); exec("$command > /dev/null &");

The first line would call the python script located in /home/pi/path/to/ and execute it. The > /dev/null will tell it to put all output into an non-existing file and the & will run it in the background. The last 2 are not really necessary but without it your website will load as long as it takes to execute the python script.

At the moment php could not start python because of the sudo command. It is nessesary because we need the privileges to work with the GPIO Pins. So we need to do: sudo nano /etc/sudoers and at the end add www-data ALL=(ALL) NOPASSWD: ALL

The last step is to write your Python script and work with the input variables you put in via php. I assume you already now how to do that based on your question.

hawxs
  • 31
  • 2