0

I'm running a python script on my raspberry pi that reads Serial input from an arduino. This is done with a while true loop. I'm trying to figure out how i can stop reading and call a function or another script to write data to the arduino from my PHP code.

I've created two different scripts, a reading and a writing script and i call the writing script from the PHP code when neccesary. But it seems to me that this can be done in a more efficient and more simpel way.

Reading Scipt.

while True:

data = serial.readline().decode('ascii')

if data.find("#UID") != -1 :
    print("UID found" + data)
elif data.find("#USER") != -1:
    UpdateUser(data)
elif data.find("#ALARM") != -1:
    UpdateAlarm(data)
elif data.find("#INHABITANTS") != -1:
    if FindUserHome() == False:
        serial.write(b'NO#')
    else:
        serial.write(b'YES#')

Writing Script

import serial
import sys
import time

serial = serial.Serial("/dev/ttyACM0", baudrate = 9600, timeout = 1)
time.sleep(3)
name = sys.argv[1]
print(sys.argv[1])
serial.reset_output_buffer()
serial.write(b"WRITE:"+name.encode() + b"________#")
exit()

PHP

 <?php
include_once "../DataAcces/Database_Functions.inc";
if (isset($_POST["submit"]) && isset($_POST["idPerson"]) && isset($_GET["query"])) 
{
    $firstName = getFirstName($_POST["idPerson"])[0];
    system("sudo python /home/pi/Python_Scripts/writing.py " . escapeshellarg($firstName));
    system("sudo python /home/pi/Python_Scripts/reading.py");
} else if (!isset($_GET["query"]) && is_null($_GET["query"]))
    header("redirect: Person.php");
else system("sudo killall python");
?>
<!doctype html>
  • Your comment is quite confusing to me as i'm not familiar with EOF or even reading serial input. Is it posible to show me a code example ? – Balding_Lama Aug 11 '17 at 20:56
  • Friend can you check question title ? You write and read on **same serial port ?** If you want write and read on same serial port `HTTP_PHP <<>> SECURE_LOOPBACK_PYTHON_APP <<>> SERIAL_PORT`, and another offer don't use PHP ! Write a WSGI app on python and serve it ! Yes my comment is wrong(deleted, sorry). – dsgdfg Aug 11 '17 at 21:03
  • I would suggest you look at one of the below frameworks - [Flask](http://flask.pocoo.org/) - [Bottle](https://bottlepy.org/docs/dev/) - [Web2py](http://www.web2py.com/) - [Django](https://www.djangoproject.com/) Flask, Bottle, Web2Py are quite light weight frameworks and very good building such stuff. This will allow you to merge your Writing and PHP script into. Also look at the below SO threads, that will help you out in your quest https://stackoverflow.com/questions/14022933/pyserial-reading-serial-port-in-flask-maybe-using-gevent https://stackoverflow.com/questions/34881133/passing-serial- – Tarun Lalwani Aug 12 '17 at 05:21

0 Answers0