I am trying to make a html/javascript controlled raspberry pi robot.
So far, I have installed a LAMP server and I am hosting a local webpage with buttons which trigger events and sends commands to my robot via AJAX. AJAX calls a php script which calls a python script (no CGI) to control the robot.
This solution is currently working for me and I can control my raspberry pi hardware from html and simple send data back and forth.
Now what I want to do is, on my web page, have an on/off button which initializes and shuts down the robot, as well as having other events (currently measuring tilt angles from an accelerometer in an android device) to control the speed of the robot. This is where I am getting confused by the procedure of how things will work.
When I make an AJAX call to start the robot, that ajax call does not close until the python script ends (robot shuts off). So I have a python script running (start.py) which contains my robot object where the methods for changing the speed are stored. If I have seperate events in JS to change the speed (sends a call to changespeed.py), how can I change the properties of an object which is stored in a different python script (start.py) that is already running (See figure below)?
Possible solutions I have thought of thus far:
1) store the object somewhere in memory. Perhaps as a python object, or store its properties in a SQL database. This way, changespeed.py (or Javascript directly) can change the speed properties in that memory, and start.py can continuously read them.
2) Rather than having the new speed values sent to python through AJAX, let python try to retrieve the values from javascript variables. in other words, instead of javascript giving data to python, have python take the data from javascript (scraping?).
3) abandon my current method of having javascript and python communicate through AJAX and php, and set up a websocket to exchange data. I dont know how this works quite yet but it may be the solution I need.
4) I also have not done any programming with interrupts and I dont know much about it. Is it possible to have my main script running and when the speed needs to change, I can send an interrupt at any moment?
I am sorry if my question is a little vague or wordy. I have a lot going on in my head right now. Let me know if it is unclear. Thank you.
TLDR: what is the best way to have javascript execute python scripts and then feed data to that script asynchronously