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.

1

1 Answer 1

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);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

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?
This answer should refer to the GET parameter n, not data. And that value should be sanitized; never trust user input.
@daQuincy, about your variable type question, PHP has some interesting ideas about typing. See 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.)
@Chris hey thanks for your explanation, it's informative
UPDATE: it's not working yet, after I run 192.168.x.x/?data=(any number), I still can't light up the LED
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.