Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

running two scripts at the same time

Wed May 20, 2020 10:58 pm

Hi Im begginer with rpi/picon zero, I have two scripts
app.py for measuring distance with HC-SR04 and sending it to website
motorTest.py basic code for piconzero that controls motor forward/backward and speed

how do I start both scripts in same time and control motor AND send speed to website AND send distance value from hc-sr04?
Im trying sending speed from motorTest.py to app.py and than send it to website
and loading both scripts with "& "
sudo python app.py & sudo python motorTest.py
but it doesnt work

here is whole project, in templates folder is website structure
https://drive.google.com/file/d/1RBGYbZ ... 1fRQtrR7Xs

Thank you for help.

RonR
Posts: 1194
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: running two scripts at the same time

Wed May 20, 2020 11:19 pm

Ersi wrote:
Wed May 20, 2020 10:58 pm
how do I start both scripts in same time and control motor AND send speed to website AND send distance value from hc-sr04?
Im trying sending speed from motorTest.py to app.py and than send it to website
and loading both scripts with "& "
sudo python app.py & sudo python motorTest.py
but it doesnt work

If you want both scripts to run simultaneously in the background, you need two separate lines:

Code: Select all

sudo python app.py &
sudo python motorTest.py &

A trailing '&' detaches the console but leaves program running.

jahboater
Posts: 5680
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: running two scripts at the same time

Wed May 20, 2020 11:48 pm

You can do:-

command & command & command & command & command & ...

I suspect the problem lies elsewhere.
Pi4 8GB running PIOS64

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: running two scripts at the same time

Thu May 21, 2020 8:40 am

Why are you running the python programs with sudo ?

That may have something to do with your problem as you are running them as user root and not user pi when you use sudo.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

Re: running two scripts at the same time

Thu May 21, 2020 9:23 am

If I try to run them with & it doesnt work, motorTest works without sudo but when I want to use app.py I have to use sudo because of website, app.py doesnt work without sudo

Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

Re: running two scripts at the same time

Thu May 21, 2020 1:10 pm

Here is the code on pastebin, I have tried rewriting motorTest to function also and return speed to app.py but it didnt work also.

codes:
https://pastebin.com/PQSJPJ5b -> app.py
https://pastebin.com/b3rAmrQk -> motorTest.py
https://pastebin.com/5FyefNZJ -> tabs.html

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: running two scripts at the same time

Thu May 21, 2020 1:55 pm

Ersi wrote:
Thu May 21, 2020 1:10 pm
Here is the code on pastebin, I have tried rewriting motorTest to function also and return speed to app.py but it didnt work also.

codes:
https://pastebin.com/PQSJPJ5b -> app.py
https://pastebin.com/b3rAmrQk -> motorTest.py
https://pastebin.com/5FyefNZJ -> tabs.html
How is MotorTest.py communicating with app.py?
I haven't looked in detail but motorTest seems to require terminal I/o so you can't just background it. You need some interprocess communications.

Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

Re: running two scripts at the same time

Thu May 21, 2020 2:12 pm

think this might be better direction

updated app.py https://pastebin.com/tdwRmjdq

so I took code from motorTest.py and Im trying to add it directly to app.py and run just app.py

lines 9-12 imports
line 23 picon zero initialization
lines 25-46 functions for char/key reading
line 51 speed value definition
lines 55-65 if/elseif for key press

the error Im getting now is line 56 unexpected indent

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: running two scripts at the same time

Thu May 21, 2020 2:50 pm

You need to make the if,elif statements indent match line 55 indent

Code: Select all

    while True:
        
        keyp = readkey()
        if keyp == 'w' or ord(keyp) == 16:
            pz.forward(speed)
        elif keyp == 'z' or ord(keyp) == 17:
            pz.reverse(speed)
        elif keyp == '.' or keyp == '>':
            speed = min(60, speed+2.5)
        elif keyp == ',' or keyp == '<':
            speed = max (0, speed-2.5)
        elif keyp == ' ':
            pz.stop()
                
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

Re: running two scripts at the same time

Thu May 21, 2020 3:27 pm

pcmanbob wrote:
Thu May 21, 2020 2:50 pm
You need to make the if,elif statements indent match line 55 indent

Code: Select all

    while True:
        
        keyp = readkey()
        if keyp == 'w' or ord(keyp) == 16:
            pz.forward(speed)
        elif keyp == 'z' or ord(keyp) == 17:
            pz.reverse(speed)
        elif keyp == '.' or keyp == '>':
            speed = min(60, speed+2.5)
        elif keyp == ',' or keyp == '<':
            speed = max (0, speed-2.5)
        elif keyp == ' ':
            pz.stop()
                
Oh okey fixed that now when I try to run it there is something else but I cannot see any problem :cry:
look here are photos, first is after I run it second is after I press ctrl+c
https://imgur.com/a/fRNa9lH

Ersi
Posts: 21
Joined: Sun Apr 19, 2020 12:54 pm

Re: running two scripts at the same time

Thu May 21, 2020 7:22 pm

update app.py but still not working, no error but doesnt work

I think there might be problem with picon zero initialization but idk :/ tried alot of things but still doesnt work

https://pastebin.com/f4JLCdk9

Return to “Beginners”