0

Working on a simple project where I can type 192.168.x.xx/?$n into the address bar of the browser, and then the Raspberry Pi will send a pwm signal (using WiringPi library) that is equivalent to the variable $n to a LED. I am just starting with PHP, that's why I need help here. What I have in mind now:

<?php
exec("gpio mode 1 pwm");
//declare $n as a variable (integer maybe)
//$n = the user input value from browser (I only know $_GET, pretty sure it's not this right?)
exec("gpio pwm 1 "+$n);
?>

Will appreciate any help and pointers.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
daQuincy
  • 13
  • 5

1 Answers1

2

you have to enter data through URL like this...

192.168.x.xx/?data=n                //n is of type int


//php script
<?php
$n=$_GET['data'];
exec("gpio mode 1 pwm");
//declare $n as a variable (integer maybe)
//$n = the user input value from browser (I only know $_GET, pretty sure it's not this right?)
exec("gpio pwm 1 ".$n);
?>
user5756014
  • 301
  • 4
  • 16
  • thanks for your input, using the code you suggested, the variable $n is a type string am I correct? Can I convert it to an integer? – daQuincy Nov 05 '16 at 12:03
  • This answer should refer to the GET parameter `n`, not `data`. And that value should be sanitized; never trust user input. – ChrisGPT was on strike Nov 05 '16 at 12:12
  • @daQuincy, about your variable type question, PHP has some interesting ideas about typing. See https://secure.php.net/manual/en/language.types.type-juggling.php, which begins with "PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used." (In any case, if you intend to use it with `exec()` it _should_ be a string.) – ChrisGPT was on strike Nov 05 '16 at 12:14
  • @Chris hey thanks for your explanation, it's informative – daQuincy Nov 05 '16 at 12:18
  • UPDATE: it's not working yet, after I run 192.168.x.x/?data=(any number), I still can't light up the LED – daQuincy Nov 05 '16 at 12:20
  • @daQuincy, I just had another look at the code in this answer. It has another error: PHP [uses `.` for string concatenation](https://secure.php.net/manual/en/language.operators.string.php), not `+`. Please make sure you have error reporting enabled. See https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – ChrisGPT was on strike Nov 05 '16 at 13:54