I am following : http://www.instructables.com/id/Simple- ... ur-Raspbe/
The objective is to switch ON and OFF Pins as guided in tutorial But
here i am not able to use : system(),exec() as it does not execute
commands in raspberry pi. Page is getting loaded accurately but when i
press buttons only image changes(ON/OFF) but no execution of commands.
I have well connected relays for my desired GPIO pin numbers
I am using Tools : PHP,Javascript,Lighttpd webserver and my device is Pi 3 model B with raspian Jessie installed.
So i have decided to use " shell_exec " to execute commands but after implementing it into
program there is no effect in my prgram and it displays nothing. Need help where i am doing wrong.
gpio.php
<?php
//This page is requested by the JavaScript, it updates the pin's status and then print it
//Getting and using values
$pin = array(4,17,18,27,22,23,24,25);
if (isset ( $_GET["pic"] )) {
$pic = strip_tags ($_GET["pic"]);
//test if value is a number
if ( (is_numeric($pic)) && ($pic <= 7) && ($pic >= 0) ) {
//set the gpio's mode to output
shell_exec("/usr/local/bin/gpio -g mode $pin[$pic] out");
//reading pin's status
shell_exec("/usr/local/bin/gpio -g read $pin[$pic] $status $return ");
//set the gpio to high/low
if ($status[0] == "0" ) { $status[0] = "1"; }
else if ($status[0] == "1" ) { $status[0] = "0"; }
shell_exec("/usr/local/bin/gpio -g write $pin[$pic] $status[0]");
//reading pin's status
shell_exec ("/usr/local/bin/gpio -g read $pin[$pic] $status $return");
//print it to the client on the response
echo($status[0]);
}
else { echo ("fail"); }
} //print fail if cannot use values
else { echo ("fail"); }
?>
index.php
<!DOCTYPE html>
<!--TheFreeElectron 2015, http://www.instructables.com/member/TheFreeElectron/ -->
<html>
<head>
<meta charset="utf-8" />
<title>Raspberry Pi Gpio</title>
</head>
<body style="background-color: black;">
<!-- On/Off button's picture -->
<?php
$val_array = array(0,0,0,0,0,0,0,0);
$pin = array(4,17,18,27,22,23,24,25);
//this php script generate the first page in function of the file
for ( $i= 0; $i<8; $i++) {
//set the pin's mode to output and read them
shell_exec("/usr/local/bin/gpio -g mode $pin[$pic] out");
shell_exec("/usr/local/bin/gpio -g read $i, $val_array[$i],$status $return ");
}
//for loop to read the value
$i =0;
for ($i = 0; $i < 8; $i++) {
//if off
if ($val_array[$i][0] == 0 ) {
echo ("<img id='button_".$i."' src='data/img/red/red_".$i.".jpg' onclick='change_pin (".$i.");'/>");
}
//if on
if ($val_array[$i][0] == 1 ) {
echo ("<img id='button_".$i."' src='data/img/green/green_".$i.".jpg' onclick='change_pin (".$i.");'/>");
}
}
?>
<!-- javascript -->
<script src="script.js"></script>
</body>
</html>
Thanks
Atila
Related
So i have this code that whenever an IP is ping able or up it'll choose the green line to appear on my screen and in reverse the red line. So what I am trying to do instead if the Round Trip Time of that IP is < 200 then it's green and when it's > 250 it's red . How can i do that?
Anyone help me. Thank you.
<?php
$page = $_SERVER['PHP_SELF'];
$sec = 5;
function pingAddress($TEST) {
$pingresult = exec("ping -c 1 $TEST", $output, $result);
if ($result == 0) {
echo "Ping successful!";
echo "<pre>Your ping: $TEST</pre>";
echo "<hr color = \"green\" width = 40%> GOOD";
} else {
echo "Ping unsuccessful!";
echo "<pre>Your ping: $TEST</pre>";
echo "<hr color = \"red\" width = 40%> BAD";
}
}
pingAddress("66.147.244.228");
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body>
</body>
</html>
The exec function is ok to use, but you should parse the contents of the output argument, after declaring it first as an array.
Even if you added -c 1 to only issue one ping, this is the recommended way of using exec.
define('RETRIES', 1);
define('PING_PATH', '/usr/bin/ping');
function pingAddress($IP)
{
$output = array();
exec(PING_PATH . " -c " . RETRIES . " $IP", $output);
// generic way, even for one line. You can also do -c 4,
// and preg_match will pick the first meaningful result.
$output_string = implode("; ", $output);
/// adapt the regular expression to the actual format of your implementation of ping
if (preg_match('/ time=\s+(\d+)ms/', $output_string, $bits)) {
$rt_time = (int)$bits[1];
if ($rt_time < 200) {
// green business
}
else if ($rt_time > 250) {
// red business
}
else {
// default handler business (or not...)
}
}
else {
echo "Hum, I didn't manage to parse the output of the ping command.", PHP_EOL;
}
}
I have a csgo betting site, when atleast 2 players deposit their skins into the site it the game starts and it takes 2 minutes until the the bot picks a winner.
Everything works fine, the game starts, a winner is picked 2 minutes after the game started, but the countdown text that are supposed to display the seconds left is not working.
this is my code Time left: <h4 id="countdown-timer"><span id="timeleft">0</span></h4>
Accepted trade offer #1211760373 by XXXXXXX (XXXXXXXXXXXXXX)
Current Players: 1
Accepted trade offer #1211760308 by XXXXXXXXX (XXXXXXXXXXXXXXX)
Current Players: 2
Found 2 Players
and that is what the bot says
and this is the timeleft.php http://prnt.sc/b03ute
PHP Code
<?php
#include_once ("set.php");
$game = fetchinfo("value", "info", "name", "current_game");
$r = fetchinfo("starttime", "games", "id", $game);
$somebodywon = fetchinfo("winner", "games", "id", $game);
if ($r == 2147483647)
die("120");
$r += 120 - time();
if ($r < 0) {
$r = 0; /* if(empty($somebodywon)) include_once('getwinner34634f.php'); */
} echo $r;
?>
Found this one aswell, called ssetimeleft.php
<
?php
#include_once ("set.php");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
/**
* Constructs the SSE data format and flushes that data to the client.
*
* #param string $id Timestamp/id of this connection.
* #param string $msg Line of text that should be transmitted.
*/
function sendMsg($id, $msg) {
echo "id: $id" . PHP_EOL;
echo "data: $msg" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
while (1) {
$game = fetchinfo("value","info","name","current_game");
$r = fetchinfo("starttime","games","id",$game);
if($r == 2147483647){
$var=120;
}else{
$var = $r += 120-time();
if($r < 0)
{
$var = 0;
/*if(empty($somebodywon))
include_once('getwinner34634f.php');*/
}
}
sendMsg(time(),$var);
usleep(500000); //1000000 = 1 seconds
}
?>
It's difficult to see what you are trying to do here without more information but the time() function in your PHP file runs only when the server-side PHP processor processes this file. The countdown display, however, is something that should be handled client side.
I recommend adding a javascript or jQuery file to handle the countdown display for you.
Try this its a very basic example :
$(function() {
var time_out = 10;
var timeout = setInterval(calculate, 1000);
function calculate() {
$('#timeleft').text(time_out--);
if (time_out < 0) {
clearInterval(timeout);
}
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h4 id="countdown-timer"><span id="timeleft">0</span></h4>
</body>
</html>
I just started learning PHP # school and for the third assignment we have to create a TicTacToe game. I have followed a turorial video on youtube and have made a game that is playable at the moment. But it doesnt know whos turn it is. IE: you can keep pressing the submit button and the computer will keep filling in O's without the player to have to fill in a X.
Please can someone explain how i can make the script know who's turn it is?
I want to know the logic behind it. So only a code wont help me at all, please explain how you check if the player has filled in a X before you switch turn for instance.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Boter, Kaas & Eieren</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
$winner = 'niemand';
$box = array('','','','','','','','','');
if (isset($_POST["submit"])){ //When the player hits submit, we retrieve the data
$box[0] = $_POST['box0'];
$box[1] = $_POST['box1'];
$box[2] = $_POST['box2'];
$box[3] = $_POST['box3'];
$box[4] = $_POST['box4'];
$box[5] = $_POST['box5'];
$box[6] = $_POST['box6'];
$box[7] = $_POST['box7'];
$box[8] = $_POST['box8'];
//print_r($box); //kijken in welke array, wat is ingevuld
//check if the player has won
if (($box[0]=='x' && $box[1]=='x' && $box[2]=='x') ||
($box[3]=='x' && $box[4]=='x' && $box[5]=='x') ||
($box[6]=='x' && $box[7]=='x' && $box[8]=='x') ||
($box[0]=='x' && $box[4]=='x' && $box[8]=='x') ||
($box[2]=='x' && $box[4]=='x' && $box[6]=='x') ||
($box[0]=='x' && $box[3]=='x' && $box[6]=='x') ||
($box[1]=='x' && $box[4]=='x' && $box[7]=='x') ||
($box[2]=='x' && $box[5]=='x' && $box[8]=='x')){
$winner = 'x';
echo "Speler wint";
}
//check if X has played and switch turn to O
$blank = 0; //assume there is no empty box
//check for an empty box
for ($i=0; $i<=8; $i++){
if ($box[$i]==''){
$blank=1;
}
}
//if there is an empty box and no winner yet its O's turn
if ($blank == 1 && $winner == 'niemand'){
$i = rand(0,8);
while ($box[$i]!=''){ //keep looking for an empty box if $i isnt empty
$i = rand(0,8);
}
$box[$i] = "o";
//check if O has won
if (($box[0]=='o' && $box[1]=='o' && $box[2]=='o') ||
($box[3]=='o' && $box[4]=='o' && $box[5]=='o') ||
($box[6]=='o' && $box[7]=='o' && $box[8]=='o') ||
($box[0]=='o' && $box[4]=='o' && $box[8]=='o') ||
($box[2]=='o' && $box[4]=='o' && $box[6]=='o') ||
($box[0]=='o' && $box[3]=='o' && $box[6]=='o') ||
($box[1]=='o' && $box[4]=='o' && $box[7]=='o') ||
($box[2]=='o' && $box[5]=='o' && $box[8]=='o')){
$winner = "o";
echo "KI wint";
}
}
//check if it is a draw
if ($blank == 0 && $winner == 'niemand'){
echo "Gelijkspel!";
}
}
?>
<div id="beurt">
<p>
<form action="destroy.php" method="get">
<input type="submit" id="destroy" onClick="windows.location.href'index.php'" value="Begin opnieuw!">
</form>
</p>
</div>
<form id="game" name="tictactoe" method="post">
<?php
//create the grid to play
for ($i=0; $i<=8; $i++){
echo "<input class=\"box\" type=\"text\" name=\"box$i\" value=\"$box[$i]\">";
if ($i==2||$i==5||$i==8){ //put in a break if $i is 2,5 or 8
echo "<br>";
}
}
if ($winner == 'niemand'){
echo "<br><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Spelen!\"><br></form>";
}
?>
</body>
</html>
Please help me out.
You can add a new variable $_POST['player'] which you can change from 0 to 1 or from 1 to 0 and that way know by the value which player should play now.
I'm not going to write you the code since I gave you a hint how it should be done and you have to learn by yourself :)
I would add a small txt file to save how was the last one to play.
For instance you could save many diffrent values into a single .txt file like this.
<?php
function savesettings($nextturn){
if(is_file("file.txt")){
$mysettings = unserialize(file_get_contents("file.txt"));
}else{
$mysettings = array();}
$mysettings["nextturn"]=$nextturn;
file_put_contents("file.txt",serialize($mysettings));
}
function loadsettings(){
if(is_file("file.txt")){
$mysettings = unserialize(file_get_contents("file.txt"));
}else{
$mysettings = array();
}
return $mysettings["nextturn"];
}
?>
You can use the following code like this:
//Save name of next player
savesettings("David");
// Load the player setting
$player = loadsettings();
I'm making PHP script to move shell script. when hoge < 50.
Code is this. Does it OK?
When I use that script , I click this link.
http://localhost/index.php?hoge=30
<?php
if(isset($_GET['hoge'])) {
$hoge = $_GET['hoge'];
print("$hoge<br>\n");
}
if ($hoge > 50 ){
echo 'F1';
}
elseif ($hoge == 50) {
echo 'F2';
} else {
echo 'F3';
exec(' /Users/hoge/Desktop/test.sh');
}
?>
You can add a test like
if (file_exists('/Users/hoge/Desktop/test.sh')) {
exec('/Users/hoge/Desktop/test.sh');
} else {
echo 'NO FILE'
}
Check also that the file /Users/hoge/Desktop/test.sh is executable
I have 2 seperate files.
The iframe executes the changePercent command in the parent window, but it doesn't change the number instantly, it is only changed after the loop finished. Is there any way to fix this? Need this for a progress bar thingy.
Thanks in advance!
This is the file I open in my browser
<span id="test">1</span><br />
<iframe src="script.php?league=Nemesis"></iframe>
<script type="text/javascript">
function changePercent(val) {
document.getElementById("test").innerHTML=val;
}
</script>
This is the embedded iframe
<?php set_time_limit(0);
include('../assets/includes/functions.inc.php'); ?>
<?php
$i = 0;
$step = 200;
$end = 15000 - $step;
$league = $_GET['league'];
$found = false;
$status = false;
while ($found == false && $i < $end) {
if($i < $end) { $i = $i + $step; }
$ladder = file_get_contents("http://api.pathofexile.com/ladders/".$league."?limit=".$step."&offset=".$i);
$ladder = str_replace('"online":false', '"online":"no"', $ladder);
$ladder = str_replace('"online":true', '"online":"yes"', $ladder);
$json = json_decode($ladder, true);
foreach ($json['entries'] as $address) {
if($address['online'] = "yes") {
$status = true;
// do something
}
}
?>
<script type="text/javascript">
parent.changePercent('<?php echo $i ?>');
</script>
<?php
}
?>
php is a server side language. when the php runs the while loop, that means the browser is still waiting for page data, and it will be sent to the browser only when the php is finished.
in your changePercent you have a php code that is located AFTER the while loop, so it will run the while loop before it can evaluate the php inside the changePercent
so there is no "fix", you should just learn the basics of web programming
You're exiting out of the PHP loop when you use ?> try the following code segment.
echo '<script type="text/javascript"> parent.changePercent(' . $i .'); </script>';
This will tell PHP to print out that line for each time your loop executes through.