kenvida
Posts: 4
Joined: Mon Nov 26, 2018 5:53 pm

Python getting input from the web or locally

Mon Nov 26, 2018 6:19 pm

Hi Everyone,

I am working on a robotic project that I want to control from the web. The brain will be a RPi3 controlling a number of arduinos as well as a camera.

What I want is a self contained python script running on the robot, this script works now through the python shell. I can execute the program and type in commands and the robot reacts.

What I want is to keep that working, but add in an interface from the web, in pseudo -code something like:

wi = <web input>
ki = <keyboard input>

if ki <> " " or wi <> " "
process


I've tried using cgi.FieldStorage() and identifying the python script in the action parameter on the page but I can;t seem to get it to work. I get no error in the apache error log, but I get the access entries in the apache access log. So it is doing something but I don't know what

I can provide the code on what I have, but I would prefer to start from scratch using the current working python script and just add in the new elements, but I am looking for the best solution. I've tried having the page write a file (the file never generated) and talk directly using the cgi functions with no luck. and embedding the HTML into the python, but the problem with that is the RPi3 needs to connect the arduinos on the serial ports so there are functions that need to run while the page is loading. to eliminate that I am looking to keep the current python running constantly on the robot and have the web page just send data to it to process like from the keyboard.

Any guidance on this would be greatly appreciated!
Thanks in advance.
KenV

Andyroo

Re: Python getting input from the web or locally

Mon Nov 26, 2018 9:04 pm

Have a look at Flask.

It allows you to build the website within Python while using html files for complex designs.

kenvida
Posts: 4
Joined: Mon Nov 26, 2018 5:53 pm

Re: Python getting input from the web or locally

Mon Nov 26, 2018 9:20 pm

Thank you for telling me about Flask, however, I don't want to embed the html into the python script.

When the script first runs it ids which arduino is on which serial port. This needs to happen without the website.

I would like the robot to work from the touchscreen display as it does today, but if it gets a command from the web it should react to it as well.

I'll worry about trigger priority later, right now I just want to get the python script to sit waiting for input. If it gets input from the touchscreen or it gets input from the web, it needs to react. I can handle the keyboard input (looking into guizero for the touchscreen), I just need a channel open in the python script that will listen for input from the web.

Thanks

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: Python getting input from the web or locally

Tue Nov 27, 2018 4:56 am

Do you need to pass arguments ? you can use sys.argv like python3 test.py on or test.py off
than you will be able to call it from web such as node red etc.

kenvida
Posts: 4
Joined: Mon Nov 26, 2018 5:53 pm

Re: Python getting input from the web or locally

Tue Nov 27, 2018 1:34 pm

that sounds like a option, thanks, I will research the call. If I can run the Python script on the bot and send arguments to it via this call from the webpage, then this may solve it. I was also looking at the socket connection as it would be a good bidirectional pipe as well...

kenvida
Posts: 4
Joined: Mon Nov 26, 2018 5:53 pm

Re: Python getting input from the web or locally

Tue Nov 27, 2018 5:08 pm

Ok, I think I have a possible solution...


I will create a python listening program that will get executed at trigger from the webpage. that script will collect the data and pass it to the constantly running bot control program. This will allow me to make a stand alone comm script to execute to accept the data and pass it and then terminate until called again.

So...

My solution will be
Bot app i <running>
Listening App
webpage


If I am on the bot touchscreen the running app will handle it the input commands
if i am on the webpage, the webpage will pass the command to the listening program which will feed it tot the running bot program..

I think this architecture will work, other than any lag between web>listener>bot which I can work on once I get this actually working

Can anyone see any issue/limitation/"gotchas I should consider?

Thanks,
Ken

onlinegill
Posts: 45
Joined: Sun Oct 14, 2018 1:57 pm

Re: Python getting input from the web or locally

Wed Nov 28, 2018 5:49 am

i need help with arguments as well let me know if you can help me lol. i have hard time to understand sys.argv

Return to “Python”