skip to Main Content

I am trying to do Web API to control Arduino by using Python, but I have known web browser cannot open pyserial. Thus I am trying to send data to Arduino from PHP via Python by using serial communication. When I tried to useshell_exec and sys.argv on data sending from PHP to python, PHP can show the python print data. When I tried to use pyserial on data sending from python to Arduino, Arduino can receive the data from Python.

However, it doesn’t work when I am trying to combine the both, the result is showed ‘Undefined variable $output’ on the php page that mean python cannot receive the data properly.

I have figured out the problem issue when I use sys and serial on Python at same time, I would like to know that is it sys.argv cannot work with serial communication? and can I have another way to connect the both side?

This is my entire php code:

<!DOCTYPE html>
<html>
<head>
<title>Arduino Controlled based PHP</title>
</head>
<body>

<form action="phppythontest.php" method="POST">
<input type="hidden"  name="turn1" value="1" />
<input type="submit" name="turn1" value="1" />
</form>

<form action="phppythontest.php" method="POST">
<input type="hidden" name="turn2" value="2" />
<input type="submit" name="turn2" value="2" />
</form>

<?php
if(isset($_POST["turn1"])) {
  $var1 = 2;
  $pyfilepath = "python_to_arduino_p2.py";
  $output = shell_exec("/usr/bin/python3 $pyfilepath $var1");
}
if(isset($_POST["turn2"])) {
  $var1 = 3;
  $pyfilepath = "python_to_arduino_p2.py";
  $output = shell_exec("/usr/bin/python3 $pyfilepath $var1");
}


echo $output;
 
?>

</body>
</html>

This is my python code

import serial import time import sys

arduino = serial.Serial(port="/dev/cu.usbmodem1101",  baudrate=115200, timeout=.1)

def write_read(x):
    arduino.write(bytes(x,  'utf-8'))
    time.sleep(0.05)
    data = arduino.readline()
    return  data


while True:
    num = sys.argv[1] if len(sys.argv) > 1 else '.'
    value  = write_read(num)
    print("total", num)
    break

I know I can send data to Arduino by using phpserial directly, but I want to trying this way and it is seem work.

I am trying to connect Arduino, PHP and Python for data transfer, I know it can be worked when I find the right code.

2

Answers


  1. Chosen as BEST ANSWER

    I have found the another way to do it that use fwrite. Let PHP to write .txt file by Post request, and then Python read that .txt for avoiding import sys in Python script. I have made some change at PHP script and Python script as below:

    I changed the PHP script from

    <?php
    if(isset($_POST["turn1"])) {
      $var1 = 2;
      $pyfilepath = "python_to_arduino.py";
      $output = shell_exec("/usr/bin/python3 $pyfilepath $var1");
    }
    if(isset($_POST["turn2"])) {
      $var1 = 3;
      $pyfilepath = "python_to_arduino.py";
      $output = shell_exec("/usr/bin/python3 $pyfilepath $var1");
    }
    
    
    echo $output;
     
    ?>
    

    to

    <?php
      if(isset($_POST["turn1"])) {
      $var1 = 2;
      $myfile = "geeks_data.txt";
      $fh = fopen($myfile, "w");
      fwrite($fh, $var1);
      fclose($fh);
    
    }
      if(isset($_POST["turn2"])) {
      $var1 = 3;
      $myfile = "geeks_data.txt";
      $fh = fopen($myfile, "w");
      fwrite($fh, $var1);
      fclose($fh);
    }
    ?>
    

    and Python script from

    while True:
        num = sys.argv[1] if len(sys.argv) > 1 else '.'
        value  = write_read(num)
        print("total", num)
        break
    

    to

    sleeptime = 10
    speed = 0
    
    while True:
        fo = open("geeks_data.txt", "r+")
        speed = fo.read()
        fo.close()
        print(speed)
        value = write_read(speed)
        print(value)
        time.sleep(sleeptime)
    

    The result is working. Thanks.


  2. ‘Undefined variable $output’

    I am no expert in PHP, but to me it looks like the output variable isn’t being defined. Since you only assign to that variable in your if statements, I would assume that the conditions in your if statements don’t apply. Can you maybe share more information on how you’re calling the PHP code? Are you sure that you’re sending a post request? Are you sure you’ve included the correct parameter?

    Update:

    Thanks for providing additional context. First of all, as far as I know the most common way to POST to the same page (which I assume you are trying to do here) is to just leave the action attribute empty (like action=""). If you’re still on the same page after clicking either of the submit buttons (which is what you want) and it still doesn’t work, you might try to log one or both of the post params (e.g. echo $_POST["turn1"]) or even the whole raw POST request data with print_r($_POST). If you’ve gotten to this point and things still don’t work, feel free to provide some of your findings / logs here so that we can try to assist further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search