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>