PHP to Arduino bridge (or any other script)

Discussion in 'General Programming Discussion' started by francescomm, Dec 24, 2013.

  1. francescomm

    francescomm Member

    Joined:
    Dec 14, 2013
    Messages:
    80
    Likes Received:
    4
    https://github.com/francescom/Pilot.ino

    (Remote procedure call from PHP to Arduino C)
    PHP Class -> function set(a,b,c) -> serial -> Arduino -> void doSet(a,b,c)

    PLUS:

    Set analog/PWM pins, read analog inputs from PHP
    Set digital pins , read digital inputs from PHP

    (web script or shell script , example included).

    Very simple to use once installed:

    Code:
    #!/usr/bin/php
    <?php
            require('pilotino.php');
            $UDOOArduino=new PilotIno('/dev/ttymxc3',115200);
            
            $UDOOArduino->dir('out',11);
            $UDOOArduino->dir('out',12);
            $UDOOArduino->dir('out',13);
           
            while(TRUE) {
            
                    
                    $UDOOArduino->set('d',11,'lo'); // use 'a' for analog and a numeric value;
                    $UDOOArduino->set('d',12,'hi');
                    $UDOOArduino->set('d',13,'lo');
                    usleep(0200000);
                    $UDOOArduino->set('d',11,'lo');
                    $UDOOArduino->set('d',12,'lo');
                    $UDOOArduino->set('d',13,'hi');
                    usleep(0200000);
                    $UDOOArduino->set('d',11,'hi');
                    $UDOOArduino->set('d',12,'lo');
                    $UDOOArduino->set('d',13,'lo');
                    usleep(0200000);
                    
                    $sensor=$UDOOArduino->get('a','A0');
                    echo($sensor."\n"); // maybe upload to server here
                    
            }
            $UDOOArduino=null;
    ?>
    
    EASILY MODIFY TO ADD YOUR PROCEDURE on the Arduino and be able to call it from PHP (with return values).

    MINUS:

    It requires to load the given sketch on the Arduino side.
    It communicates via serial at 115K, so it is not good if you need superfast access.

    Help wanted to add same functionality to other scripting languages (node with node-udoo to allow analog sensors, shell scripting, etc..)

    Have fun, UDOO rocks, and merry Christmas.
     
  2. delba

    delba Administrator Staff Member

    Joined:
    May 8, 2013
    Messages:
    1,064
    Likes Received:
    9
    Awesome! Gott try it asap. Happy holidays!
     
  3. francescomm

    francescomm Member

    Joined:
    Dec 14, 2013
    Messages:
    80
    Likes Received:
    4
    Updated: PHP to Arduino bridge (or any other script)

    Code updated, fixed stuff on the PHP side (like clearing and flushing port before issuing every command), now it actually works, runs at 115k and with no glitches (apparently, yet).

    Next step will be connecting to/from a webserver out of the LAN.
    sensor/led <-> Arduino C <-> UDOO PHP <-> Public Webserver
     
  4. cam

    cam New Member

    Joined:
    Jan 21, 2014
    Messages:
    8
    Likes Received:
    0
    Thanks for the code it works quite well. (at least on my VM ubuntu/arduino mega) I'm waiting for my second Udoo board to see if the serial works better on it. I think the board I have has something wrong with it.

    Here's some quick Python code to get people going with the Arduino pilot!

    Code:
    #!/usr/bin/python
    import serial
    import time
    
    ser = serial.Serial('/dev/ttyACM1', 115200)
    ser.timeout=.3
    try: 
    	ser.open()
    	print("Initializing ")
    except Exception:
    	print("error open serial port: ")
    time.sleep(1) # waiting the initialization...
    ser.write("dir out 10\n"); 
    print("Define Pin")
    for x in range(0, 50):
    	ser.write("set D 10 hi\n")
    	print("Set Pin 10 High")
    	response1 = ser.readline()
    	print("response " +response1)
    	time.sleep(.5)
    	ser.write("set D 10 lo\n")
    	print("Set Pin 10 Low")
    	response1 = ser.readline()
    	print("response " +response1)
    	time.sleep(.5)
     
  5. francescomm

    francescomm Member

    Joined:
    Dec 14, 2013
    Messages:
    80
    Likes Received:
    4
    Thanks Cam!!!

    If this can help with your UDOO serial problems, in the first version of PHP code, I had noticed some kind of random/wrong chars if running too fast (>=56K), some lines arrived or returned "broken" (missing chars), I don't have an Arduino yet so I cannot compare, but I felt it was strange. I fixed it by clearing the serial port before any write: so before any write instruction, I perform a read() (and ignore the result) just to clean the buffers, and now it runs much better, at 115K without problems.
     
  6. cam

    cam New Member

    Joined:
    Jan 21, 2014
    Messages:
    8
    Likes Received:
    0
    That's funny because I was having the same problem at 115200. I was playing with time delays between commands as well as flushing the buffers before writing to them but I ended up removing all of those things for now.

    I added a readiness check function to the Arduino pilot side. It simply looks for an "ok" command and then returns an "ok" as the status. At start up, on the Python side, after the serial connection has been established, I don't start sending any instructions until the readiness check is successful. This cleaned up some errors I had at start up.

    I then took this approach further and I wait for a confirmation "ok" from all commands sent to the pilot before moving on to the next command. This allowed me to remove all of the waits/sleeps I had to try and prevent things from going too fast. Now the commands can only run as fast as the Arduino says it's ok.

    Even with this approach my LEDS on the Arduino blink so fast they basically don't turn off. I need to slow it to .02 of a second to notice a blink. Running multiple LEDs on/off through a loop of 10,000 doesn't produce a single error. The Pilot seems rock solid!

    Now that I've confirmed that the code is good/reliable I'll run it on the Udoo in the next couple of days.
     
  7. francescomm

    francescomm Member

    Joined:
    Dec 14, 2013
    Messages:
    80
    Likes Received:
    4
    Yes the PHP version does something similar.
    Clear buffers (read)
    Sends (write) a command
    Continuously read chars and add them at the end of a string.
    If string ends with "\n" save it, clear it and pass it as result.
    If this does not happen after some time, return a timeout error (this to avoid locking on case of errors).

    Remember that not all commands return "OK"...
    For instance command "get a A0" will return something like "0512\n" if sensor at pin A0 is at half value. Checking for "\n" at end of string will work every time, as all commands results end with a newline (or some other fixed char).

    Great work!
     

Share This Page