0

As a newbie to the exciting world of raspberry pi i have to following situation.

On the one hand i have an raspi with an button connected to the gpio and a bash script that registers button presses. This works wonderful.

Also i have a php-script which reads the actual ip- and mac-adress of the raspi - this script works also wonderful when called on the build-in-browser on the raspi.

On the other hand i have an windows 2012r2 based iis with an asp.net webpage which awaits the call of this page by the raspi-php-script with the params ip and mac.

When i call the php-script on the raspi-browser everything works perfect. But the call by the bash-script is not even going out? What do i have to change?

Here is the bash-script:

#!/bin/sh

gpio mode 2 in
while true
do
result="$( gpio read 2 )"
if [ "$result" = "0" ]; then
php /var/www/html/buttom.php
echo "go"
fi

sleep 0.5
done

Here is the php-script "buttom.php"

#!/usr/bin/php 
<?php

$ip = trim(shell_exec("/sbin/ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"));
$mac = trim(shell_exec("/sbin/ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:digit:]]{1,2}'"));

$search = array (0 => ':',);
$replace = array (0 => '',);
$subject = $mac;
$mac = (str_replace( $search, $replace, $subject) );

$vars = array('ip' => $ip, 'mac' => $mac);
$querystring = http_build_query($vars);

$url = "http://www.mydomain.de/inbound/buttom.aspx?".$querystring;

header("Location: $url");

?>

I would be so grateful for any advice :D

3
  • can you add "set -x" on your script. This will give you shell script debug logs. Also add that to the question. type which php and get the result something like "/usr/bin/php". Try changing the shell script line "php /var/www/html/buttom.php" to <path to php(/usr/bin/php) /var/www/html/buttom.php>. Don't forget to update the question.
    – Varad A G
    Sep 6, 2016 at 7:35
  • Thank you for the quick reply - where do i add set -x? i also changed the path to php as you recommended but nothing happened... Sep 6, 2016 at 7:54
  • i found out where to put it - the result is something like this: + /usr/bin/php /var/www/html/buttom.php + echo go Sep 6, 2016 at 7:59

1 Answer 1

0
  1. Provide full path to both PHP file and PHP binary, eg. /usr/bin/php /var/www/myscript.php. You might need root privileges as well.
  2. Safer option might be calling your local server via curl, like this: curl -s http://localhost/buttom.php

EDIT. Ah, I see your problem. So your script launches. You run bash script, that executes PHP. And you are returning what? A header. I don't think bash console understands that ;)

Try calling your site from inside PHP.

Instead of

header("Location: $url");

Go for

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
curl_close($ch);

Try this and let us know did it work :)

More info on PHP curl function is available on manual page http://php.net/manual/en/book.curl.php

16
  • + // /usr/bin/php /var/www/html/buttom.php /var/www/buttom.sh: Zeile 10: //: Ist ein Verzeichnis + curl -s http://localhost/buttom.php #!/usr/bin/php + echo go go This is the result of changing to curl - but still no call of aspx-page noticed on iis... Sep 6, 2016 at 8:06
  • 1
    @Mark since he has "/usr/bin/php " inside his actual php script I don't think there is a need to give full path while invoking the script. Now that your script is getting invoked can you add some debug point inside your php script ("print statement ") and provide the output. I think we need to debug where exactly if comes out.
    – Varad A G
    Sep 6, 2016 at 8:20
  • sorry again - i am really completely new to the linux bash/shell/php-world. Where do i insert the "print statement"? Sep 6, 2016 at 8:24
  • Put some echo "name of your point so you know we went this far"; inside your php script. You can also do some php.net/manual/en/function.var-dump.php and check what is inside your php variables
    – Mark
    Sep 6, 2016 at 8:25
  • 1
    @MichaelHanning install the dependencies for curl - sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
    – Varad A G
    Sep 6, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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