Go Down

Topic: Running python script on startup (Read 12841 times) previous topic - next topic

NickH

Hello,
I have a python script on an sd card on a YUN that I am currently able to run by using ssh to go to the relevant directory and starting it with python mypythonfile.py

Is it possible to get this script to run automatically when the YUN is started?

Thanks,
NickH

noblepepper

Just put the command (python mypythonfile.py) in /etc/rc.local, either in a console session or at the bottom of the System->Start Up page of Luci

NickH

Thank you,

I have also realized that I can use the setup section of the arduino sketch and insert the following:

Process p;
  p.runShellCommand("python /mnt/sda1/arduino/www/touchosc/mypythonfile.py");



Nick

noblepepper

That would be the best way to do it if the script is unique to the sketch you put the runShellCommand in. If it is something that you want to use in all or many of your sketches the rc.local method would prevent you from needing the runShellCommand in all the sketches.

GinVan

#4
Dec 27, 2013, 09:16 am Last Edit: Dec 27, 2013, 09:24 am by GinVan Reason: 1
Just as nick mentioned starting a process with "  p.runShellCommand("python /mnt/sda1/arduino/www/touchosc/mypythonfile.py");"

... I am doing the same thing... However if my python script contains and endless loop to receive Bridge.put notices...
My arduion program does not continue... Unless I kill the python process.
How can I leave a python endless loop run and still return control/flow back to my arduino program?

If I start the python process through ssh/putty there is no problem (even it the script contains the endless loop).. The arduino program will continue to run... But I want to be able to start the process directly from the arduino program.  Again - also - no problem with running the python script  when started from p.run... As long as it does not contain an endless loop.

Perhaps this could be solved by running the script as a different user (if I am on the right track)?  But I am not sure how this could be done from the arduino side?

NickH

I wonder if we are doing the same thing GinVan,

I now realize that my python script, which is one I found on the internet to translate OSC commands, runs in a blocking mode which means that the script works fine in receiving/sending information to and from my iphone but it does not return to the yun sketch and so does not control the pins of the YUN.

p.runShellCommandAsynchronously apparently allows the script to run in the background and it still works but i have not yet got it to control the YUN. More tinkering required. :smiley-eek:

Nick

GinVan

#6
Dec 27, 2013, 09:58 am Last Edit: Dec 27, 2013, 10:24 am by GinVan Reason: 1
Nick... Thanks... Your info solved my problem - "...Asynchronously".
Not sure what issue you are having with the pin state... But it has been working for me by using
Bridge.put("D7","1") as an example.
Seems you may have to send as a string.
I have been converting int with String newx = String(xx) so...
Bridge.put("D7", newx) newx being the pin state - seems to be working for me.
(Not sure if this is related to your matter)

We are not working on the same thing at this point... I am simply sending email based on pin state.
I would be interested in what you are working on... If you don't mind... Will you send the link?

I am guessing that if you want to change the state of pins in response to the python script then perhaps - Bridge.get

GinVan

Well I thought I had this runShellCommandAsynchronously figured out... Not so:

If the python script is started from within void setup()... It runs and as long as your code does not enter other functions... ie - void loop() ... The python script will continue to run.
However... If after starting the python script in setup() and moving forward to loop() the python script exits.
If I start the script once in loop()  it will begin but exit within moments.

My goal is to start a python script  from inside my arduino app and have it continue to execute and receive posts from the arduino side.

Hope someone has some answer for me?

NickH

Well I have experienced the same thing.

I am resorting to starting the python script using SSH which works fine. I have tried putting the command for the script into rc.local using LUCI as was previously suggested but this doesn't seem to work in my case. Looking at the log produced during startup, the python script fails to run. I think this may be because the arduino is not responding at this point during the bootup process to requests to join clients and servers etc.

The python code I am using is below. Any suggestions very welcome.
Thanks,
Nick

Code: [Select]

from OSC import OSCServer,OSCClient, OSCMessage
import sys
from time import sleep
import types
sys.path.insert(0, '/usr/lib/python2.7/bridge/') # make sure python can see the tcp module

from tcp import TCPJSONClient
                                                                                                       
#set up the json client and the osc client and server. The server should have the ip of your Yun.
#The client.connect should have the ip address of you phone.
json = TCPJSONClient('127.0.0.1', 5700)
server = OSCServer( ("192.168.0.21", 8000) ) #this is the address of the YUN
client = OSCClient()
client.connect( ("192.168.0.19", 9000) ) #this is the address of the phone




NewLine

I think typically the MCU side starts up much faster than the CPU. If you want to start the script from the sketch side, I think you need to wait until the CPU has completely booted. But the other way around should work fine?

mamu

I think starting a python script from a sketch is not that easy because MCU starts much faster and Lininio takes about 1 minutes to come up.
I would suggest using Luci and put something like this
Code: [Select]
(sleep 1;python /www/sd/myproject/myscript.py)&
under "System : Startup". This will start your python script in a new shell without blocking the current one.
How have you configured your python script using Luci?

NickH

Thanks Mamu,
I have not tried this yet but will do as soon as I get my script working without problems.

I didn't quite understand your last question but my rc.local file in LUCI contained the following:

python /www/sd/myproject/myscript.py

Nick

mpenno

Hi, guys,

I know it's an older subject than that. But thanks to the hints I was able to solve the automatic startup of a python script on system startup.

mpenno

I found the most elegant way to start it was via the sketch directly. So you don't have to change anything in the rc.local file.

My example:
Code: [Select]

#include <Bridge.h>
#include <Process.h>
#include <stdio.h>

void setup() {
  // Start using the Bridge.
  Bridge.begin();

  //function to start the python script
  runPython();
}

void loop() {
  ...
}

void runPython() {
  delay(120000); // That's about all the time it takes for the Arduino to start. It may be possible to reduce this.
  Process p;
  p.runShellCommandAsynchronously("/usr/bin/python /mnt/sda1/Scripte/mqtt.py&");
}


My Python script works in a loop and does not end. It has the task to forward data via MQTT and nothing more.

After startup you can check the processes via ssh (please see attachment)



Go Up