Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Execute a command only once

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Execute a command only once

Postby Playagood » Thu Feb 28, 2013 7:28 pm

Hi,

I have a code that allows me to control a led from my pc to a microcontroller.The led goes on and off.

The problem:

-When I press the "Light On" button: It takes 2 seconds to light on.
-When I press the "Light Off" button: It goes off right away.

I would understand the 2 seconds because I put a sleep(2), but why is the light goes off with no delay.How can I get no delay on both side (I put ths 2 seconds sleep because deviceOpen reset the microcontroller and it takes about this time to be ready)

So, what I think I need to do: Make sur that the connection with the microcontroller get execute only one time.


Please help me. Thank you (all codes found online. all open source)




Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Arduino Project 1: Serial LED Control</title>
</head>
<body>

<h1>Arduino Project 1: Serial LED Control</h1>
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenon" ?>">
Click here to turn the GREEN LED on.</a></p>
<p><a href="<?=$_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
Click here to turn the GREEN LED off.</a></p>
 
</body>
</html>

<?php

include("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(115200); //Baud rate: 9600
$serial->deviceOpen();
sleep(2) ;

if (isset($_GET['action'])) {

    if ($_GET['action'] == "greenon") {
         
        $serial->sendMessage("1");
     
    } else if ($_GET['action'] == "greenoff") {
       
        $serial->sendMessage("2");
    }
   

    $serial->deviceClose();
 

}

?>
Attachments
Untitled.png
Untitled.png (7.09 KiB) Viewed 688 times
Playagood
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Feb 26, 2013 7:20 pm

Re: Execute a command only once

Postby seandisanti » Fri Mar 01, 2013 9:23 am

alright, i don't have php_serial.class.php,so i couldn't really test but I think this will work all the same. This code will persist the same serial connection via a session variable, and utilizes a singleton approach on both the session and the serial connection to avoid multiple instances. Let me know if it fails, or if you have any questions about the changes i've made.
Code: Select all


<?php
Session
::start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Arduino Project 1: Serial LED Control</title>
    </head>
    <body>

        <h1>Arduino Project 1: Serial LED Control</h1>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=greenon" ?>">
                Click here to turn the GREEN LED on.</a></p>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
                Click here to turn the GREEN LED off.</a></p>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=close" ?>">
                Click here to close the serial connection.</a></p>

    </body>
</html>

<?php 
include("php_serial.class.php");
$serial = new MyLed();
if (isset($_GET['action'])){
    if ($_GET['action']=='greenon'){
        $serial->turnOn();
    }
    if ($_GET['action']=='greenoff'){
        $serial->turnOff();
    }
    if ($_GET['action']=='close'){
        $serial->close();
    }
}

  
class Session 
{
    
    
//this is a flag to make sure we can only start a session if one has not already been started
    private static $_sessionStarted = false;
   
    
/*
     * 
     * function start
     * checks if session has been started already, if not, it starts one and 
     * sets the flag to let future calls know to be ignored.
     * 
     */
    public static function start(){
        if (! self::$_sessionStarted){
            session_start();
            self::$_sessionStarted = true;
        }
    }
}
class MyLed extends phpSerial {

    public static $instance;
    protected $ledState;

    public static function getInstance() {
        if (!self::$instance) {
            if (! $_SESSION['led']){
            self::$instance = new myLed();    
            
} else {
                self::$instance = $_SESSION['led'];
            }
        }
        return self::$instance;
    }

    function __construct($device = '/dev/ttyACM0', $baud = 115200) {
        $this->deviceSet = $device;
        $this->confBaudRate = $baud;
        $this->deviceOpen();
        $_SESSION['led']=  $this;
        self::$instance = $_SESSION['led'];
    }
    public function turnOn(){
        $this->sendMessage("1");
        $this->ledState=1;
    }
    public function turnOff(){
        $this->sendMessage("2");
        $this->ledState=0;
    }
    public function checkState(){
        return ($this->ledState==1)?'on':'off';
    }
}

***edit*** sorry, removed a variable in the session class that was from another project
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Execute a command only once

Postby Playagood » Sat Mar 02, 2013 8:01 am

Hey Hey...Thank you !!! I will try that tonight ...I might have a few questions. :D


Merci
Playagood
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Feb 26, 2013 7:20 pm

Re: Execute a command only once

Postby seandisanti » Mon Mar 04, 2013 10:05 am

Did that work for you?
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Execute a command only once

Postby Playagood » Tue Mar 05, 2013 5:47 pm

Ok...the web page appears as it should. When I try to light the led on, nothing happens. The Arduino doesn't receive any signal...the Rx/Tx lights doesn't blinking on the Arduino.

I have created an arduino.php with your code inside:

Code: Select all
<?php
Session::start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Arduino Project 1: Serial LED Control</title>
    </head>
    <body>

        <h1>Arduino Project 1: Serial LED Control</h1>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=greenon" ?>">
                Click here to turn the GREEN LED on.</a></p>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=greenoff" ?>">
                Click here to turn the GREEN LED off.</a></p>
        <p><a href="<?= $_SERVER['PHP_SELF'] . "?action=close" ?>">
                Click here to close the serial connection.</a></p>

    </body>
</html>

<?php
include("php_serial.class.php");
$serial = new MyLed();
if (isset($_GET['action'])){
    if ($_GET['action']=='greenon'){
        $serial->turnOn();
    }
    if ($_GET['action']=='greenoff'){
        $serial->turnOff();
    }
    if ($_GET['action']=='close'){
        $serial->close();
    }
}

 
class Session {
   
    //this is a flag to make sure we can only start a session if one has not already been started
    private static $_sessionStarted = false;
   
    /*
     *
     * function start
     * checks if session has been started already, if not, it starts one and
     * sets the flag to let future calls know to be ignored.
     *
     */
    public static function start(){
        if (! self::$_sessionStarted){
            session_start();
            self::$_sessionStarted = true;
        }
    }
}
class MyLed extends phpSerial {

    public static $instance;
    protected $ledState;

    public static function getInstance() {
        if (!self::$instance) {
            if (! $_SESSION['led']){
            self::$instance = new myLed();   
            } else {
                self::$instance = $_SESSION['led'];
            }
        }
        return self::$instance;
    }

    function __construct($device = '/dev/ttyACM0', $baud = 115200) {
        $this->deviceSet = $device;
        $this->confBaudRate = $baud;
        $this->deviceOpen();
        $_SESSION['led']=  $this;
        self::$instance = $_SESSION['led'];
    }
    public function turnOn(){
        $this->sendMessage("1");
        $this->ledState=1;
    }
    public function turnOff(){
        $this->sendMessage("2");
        $this->ledState=0;
    }
    public function checkState(){
        return ($this->ledState==1)?'on':'off';
    }
}

?>
Playagood
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Feb 26, 2013 7:20 pm

Re: Execute a command only once

Postby seandisanti » Wed Mar 06, 2013 11:17 am

are you getting any errors? maybe put in some echo's to write to the screen as things are clicked to make sure that the functions are being entered correctly
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], thesmahesh and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.