Ph1lj2
Posts: 35
Joined: Sat Oct 20, 2012 9:21 pm

How to Auto start a python script that uses GPIO tx/rx pins

Fri Jul 05, 2013 6:49 pm

Hi
Can someone please help, written a script that sends out data using the GPIO tx/rx pins - when I start the script from a terminal all is well, my serial device see's the data :D

So the next step was to auto-start the same script, in other words automatically run the script on power-up. - that seems to be a problem, I've tried as when ways as I can find to get this functioning - but the transmission of data does not happen. :evil:

I'm using wheezy, with version B RPi.

It is even possible ?

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Fri Jul 05, 2013 9:01 pm

Have you tried using upstart? This simple upstart script will start on boot and respawn if it crashes.

Code: Select all


# gps data logger service

description "GPS Data Logger service"
author "User"

start on runlevel [2345]
stop on runlevel [016]
chdir /home/user/
exec python /home/user/gps_data.py
respawn
It lives in /etc/init/ and should be named the name of the service you like .conf

gps.conf

it would then be started with
sudo service gps start

It will be run as root at startup so no need for sudo in the script.

Upstart is easy and very useful I have used it loads of times.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: How to Auto start a python script that uses GPIO tx/rx p

Sat Jul 06, 2013 8:42 am

It should work.

Do you read data from a file? if you do you need to make sure that the file paths are tolerant of starting from a directory other than the one in which the code is stored, also using sudo alters the user to 'root' and therefore the home directory.

With Pi Presents I found it necessary for the user to specify the full paths in the command options.
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

Nexus12
Posts: 10
Joined: Sat Jan 05, 2013 10:37 pm

Re: How to Auto start a python script that uses GPIO tx/rx p

Sun Jul 07, 2013 2:55 pm

Hi kghunt

Thanks for your reply, I've never used or heard of upstart. I noted you included the

Sudo service gps start

Where/how does that auto start from a power up ?

Ideally I just want the script to run

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: How to Auto start a python script that uses GPIO tx/rx p

Sun Jul 07, 2013 3:11 pm

If I have it right. To answer the question anything in the directory /etc/init named xxx.conf will load when the machine boots it basically just points to your python file and says start this file when the pi boots. (Like the Start Up folder in Windows if your familiar with it.) the respawn at the end of the file says run if it ends so if it is one time script you would remove that.

Hope I got all this correct. If not someone will educate me :)

To break down how the file needs to be setup:

The sample provided just needs to be done the same but with your script:

Have you tried using upstart? This simple upstart script will start on boot and respawn if it crashes.

The example given goes like this if I got it correct.

To run a python file on startup you need a conf file to point to the .py file you created.

This conf file needs to be in /etc/init/ and should be named the name of the service you like .conf

So to make your own service (name it what you want gps was just an example)

Code: Select all

cd /etc/init/

nano nameofpythonfile.conf
nameofpythonfile = is any name you want to call the service so pick what ever you think your service should be called

paste the code into it or type it out:

Code: Select all

    # gps data logger service

    description "GPS Data Logger service"
    author "User"

    start on runlevel [2345]
    stop on runlevel [016]
    chdir /home/user/
    exec python /home/user/gps_data.py
    respawn
The following is what you change to reflect the program you want to run or just leave it alone if you don't have a gps going

Code: Select all

    # gps data logger service

    description "GPS Data Logger service"
    author "User"
This part points to where the file is and what it is named change it to what you have and it must be correct or it won't be able to find your file

Code: Select all

    chdir /home/user/
    exec python /home/user/gps_data.py
respawn just says if it dies start it again

Test your settings by typing

Code: Select all

sudo service gps start
replace gps with the name you chose for your .conf file.

If it errors go back to step one cd to the .conf file edit it save it then suso service whatever start til no errors and you program loads.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

Nexus12
Posts: 10
Joined: Sat Jan 05, 2013 10:37 pm

Re: How to Auto start a python script that uses GPIO tx/rx p

Sun Jul 07, 2013 3:29 pm

Malakai

Thanks for detail, I give it try

Cheers

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Sun Jul 07, 2013 7:14 pm

The other poster is pretty much right. Name it what you want the service to be called.

The top line is a comment and not needed.

The author and description must be there but can be whatever you want them to be.

Call your script with full path no need for sudo in here though as it will been run by root at the init stage.

You can optionally change to a different working directory if you need to I was accessing other files so had to which is why I change directory before execution but you may not.

Mine is a simple example that I used to open up up a tcp listening server that takes and processes information from cheapo GPRS GPS trackers.

You can get a bit more complex by adding startup and shutdown scripts that are run before and after your main script respectively.

The upstart docs are pretty good I am by no means an expert and they helped me. I have sinced used upstart loads of times and its rock solid. Previously I used to use cron and have my script detect if it is boot time or not.

Ph1lj2
Posts: 35
Joined: Sat Oct 20, 2012 9:21 pm

Re: How to Auto start a python script that uses GPIO tx/rx p

Mon Jul 08, 2013 4:17 pm

Hi all

# Greenhouse service

description "Greenhouse service"
author "User"

start on runlevel [2345]
stop on runlevel [016]
chdir /home/pi/
exec python /home/pi/grnhouse3.py
respawn
So this is my conf file named as gogh.conf, located in the /ect/init/ folder, to test it I ran
sudo service gogh start

and it did not start my app, the message returned is
gogh: unrecognized service

I'm using the desktop - don't know if that matters

Any idea what I did wrong ?

Phil
Last edited by Ph1lj2 on Mon Jul 08, 2013 7:49 pm, edited 1 time in total.

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Mon Jul 08, 2013 7:49 pm

It looks like upstart is not installed in wheezy by default. You can either install it

Code: Select all

sudo apt-get install upstart
Or figure out how to make a sysvinit service. I have not tested upstart with wheezy yet I may just work but it may also break the startup process. But as its in the repos I imagine it does work on a standard build. I can't test in on my home pi as It set up for something and I don't have a spare sd to burn. I can maybe test it tomorrow though I have tonnes of sd cards at work and another pi there.

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Tue Jul 23, 2013 10:06 pm

I just installed upstart and all seems well. I'm going to make my default image use moebius and upstart.

JimT
Posts: 21
Joined: Wed Feb 13, 2013 12:24 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Thu Aug 01, 2013 9:50 pm

Is there a way to start a python script at boot that doesn't require installing Upstart, or installing anything else that is not included in the default Raspbian Wheezy distribution?

kghunt
Posts: 383
Joined: Sun Mar 04, 2012 9:28 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Fri Aug 02, 2013 9:24 pm

Yes use sysvinit or use cron to call your script every minute and have your script check to see if the system just booted.

But just installing upstart is a lot easier as the point of upstart is its ease of use. Sysvinit scripts are a lot more complex.

Or the most simple method by far is to add a cronjob using @reboot instead of a schedule but be sure to add an ampersand after the command or it my halt the boot process while it does its stuff.

Although upstart has always been the most stable for me.

lowellbert
Posts: 1
Joined: Thu Sep 25, 2014 2:32 am

Re: How to Auto start a python script that uses GPIO tx/rx p

Thu Sep 25, 2014 2:38 am

Hi

Currently running this in /etc/init/myscript.conf

Code: Select all

start on run level [2345]
stop on runlevel [016]
chdir /home/pi/
exec python /home/pi/sunsetter/manage.py run sunrise
respawn
Running Raspberry pi with 2014-06-20-wheezy-raspbian and latest Upstart

this thread has had the best info on how to create a loadup script for upstart.

Mine is not loading on start up

Any help greatly appreciated

Return to “Automation, sensing and robotics”