0

My idea is to have a Pi running and controlling some LED lights.

I have a Python script that controls the LED lights.

How does another computer (Windows based) send a signal to the Pi to switch the appropriate lights off and on?

I was thinking that a web-based approach using PHP would work best. How do I interface PHP and Python?

(clarified 6.12.2014)

closed as unclear what you're asking by lenik, Milliways, kolin, RPiAwesomeness, Impulss Jun 11 '14 at 21:43

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

(assuming you run raspbian)

You can very easily install a LAMP (Linux Apache MySQL PHP) server on your raspberry pi, there are numerous tutorials online, but it basically boils down to this:

On your Raspbian Pi, execute the following commands (you will need internet access):

sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5

For MySQL, also install

sudo apt-get install mysql-server mysql-client php5-mysql

And then reboot:

sudo reboot

You will find a file called index.html in you /var/www/ directory. Here you can start your first test: On a remote computer, open a web browser and point it to your RPi's IP Address (you can get that by using ifconfig). You should see a message like "It works." (This is the content of the index.html!).

Now for your case of accessing a python script: Of course, you should be able to call a python script directly as a CGI-Script. Anyhow, I'd suggest (as this has worked well for me) to use a php script to call your python script. This could be something like this:

File: /var/www/5_on.php

<?php
echo shell_exec('sudo /path/to/your/python/script.py 5 on');
?>

As you can see, this calls a python script on your pi, giving along command line arguments you can retrieve via (python):

import sys
print sys.argv[1] + sys.argv[2] #sys.argv[0] will habe the name of the script

Of course, instead of having a seperate php file for every on/off/gpio combination, you can use $_GET and call the php script with

http://192.168.*.*/gpio.php?number=5&on=on

and using (as /var/www/gpio.php):

<?php
echo shell_exec('sudo /path/to/your/python/script.py '.$_GET['number'].' '.$_GET['on']);
?>

Be careful, tough, as this may pose a security risk!

Now, for executing the python script, you will need (to be able to access gpio) sudo rights. If you don't want your www-data-user to have full root permissions (also not suggested), you can allow a single file to be executed as root, by editing the /etc/sudoers file with the command visudo: Add at the bottom:

%www-data ALL=(ALL) NOPASSWD: /path/to/your/python/script.py

Also, don't forget to add the executable bit (sudo chmod +x) and the shebang (first line of your script should be #!/usr/bin/env python) to your python script! It might also be necessary for your script to belong to www-data:

sudo chown www-data:www-data /path/to/your/python/script.py

Now you can call http://ip.of.your.rpi/gpio.php?number=5&on=on from your windows server (I don't know the exact commands for that, but on linux I'd use curl or wget).

I hope I didn't forget anything, if I did, feel free to add more information!

  • security risk isn't an issue. The application/pi's will be running internally. Thanks for the detail. – John M Jun 10 '14 at 16:19
1

Yes the internet was invented on Unix. The r-pi runs Gnu and Gnu is Unix (Gnu = Gnu's not UNIX = Gnu is not UNIX, but it is Unix).

You don't need a web server, here is a simple solution in python. There where no security considerations (this is not secure, unless your network is).

This runs a server that you connect to from the other system, I assumed that the other end is active and will want to connect to a listening (passive) server.

#!/usr/bin/python2
import socket
import sys

port = 2003 # choose a better port
listener=socket.socket() 
listener.bind(('',port)) 
listener.listen(1)
while True:
    socket, info = listener.accept() 
    f=socket.makefile()
    for line in f: 
        #interperate the lines and do something
        print line
        sys.stdout.flush()
    f.close
    socket.close()

Here is a minimum at the other end. It connects and sends a message.

#!/usr/bin/python2
import socket
s = socket.create_connection(('raspbery-pi',port))
s.send("hello\n")
s.close()

Not the answer you're looking for? Browse other questions tagged or ask your own question.