RUN PROGRAM ON STARTUP

THROUGHOUT THIS BOOK, I’VE SHOWN YOU HOW TO RUN YOUR ROBOT’S PROGRAMS USING SIMPLE TERMINAL COMMANDS. WHILE THIS IS GREAT FOR PROTOTYPING, IN THE FUTURE YOU MIGHT WANT TO MAKE YOUR ROBOT RUN A PROGRAM AS SOON AS YOU TURN IT ON, WITHOUT HAVING TO ACCESS IT REMOTELY. YOU CAN DO THIS EASILY BY EDITING THE RC.LOCAL CONFIGURATION FILE.

When you power on your Raspberry Pi, it goes through a boot process. When the boot process ends, your Pi looks to the rc.local file for any last commands or code to execute. By adding your own custom Python command, you can make any of the programs from this book run on startup. Here’s how!

EDITING THE RC.LOCAL FILE

First, make sure the program you want to run on startup is complete and working the way you want it to. It’s much better to go through the programming, editing, and debugging process in the terminal rather than wait to see if the program runs when you power on your Pi. This approach will save you from turning your Pi on and off all the time.

Then, from any location in the terminal, open the rc.local file using the Nano text editor like so:

pi@raspberrypi:~ $ sudo nano /etc/rc.local

Make sure you include sudo at the start. This command lets you edit with root user privileges so you can save changes you make to the file; otherwise, they’ll just disappear!

After you enter in the preceding line, you’ll see a file that looks like Listing E-1.

   #!/bin/sh -e
   #
   # rc.local
   #
   # This script is executed at the end of each multiuser runlevel.
   # Make sure that the script will "exit 0" on success or any other
   # value on error.
   #
   # In order to enable or disable this script just change the
   # execution bits.
   #
   # By default this script does nothing.

   # Print the IP address
   _IP=$(hostname -I) || true
   if [ "$_IP" ]; then
     printf "My IP address is %s " "$_IP"
   fi

   exit 0

LISTING E-1 The contents of the rc.local file

Now, use your arrow keys to scroll down to the space between fi and exit 0. This is where you can add any commands you want your Raspberry Pi to execute on startup. No matter what you add, you must leave exit 0 unedited at the very bottom of the file.

If you want to run a Python 3 program on startup, insert at a line that looks like this:

python3 /your/file/path/here/filename.py &

Replace the filepath with a valid one that points toward the correct directory and your program. Also, make sure to add the ampersand symbol (&) onto the end of the command so that your program doesn’t stop your Raspberry Pi from continuing to boot.

After adding the program you want to execute on startup, save your work and exit the Nano text editor by pressing CTRL-X and following the prompts.

A PRACTICE EXAMPLE

Let’s say that you want to run the ball_follower.py program from Chapter 8 whenever you turn on your robot. To do this, open the rc.local file on your Pi and insert this line before the exit 0 statement:

python3 /home/pi/robot/ball_follower.py &

Now the last part of the file should look like this:

--snip--
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s " "$_IP"
fi

python3 /home/pi/raspirobots/ball_follower.py &

exit 0

Let’s test it to see if it works. Save the file and then reboot your Raspberry Pi as follows:

pi@raspberrypi:~ $ sudo reboot

If it’s successful, your robot will execute the ball_follower.py code. If not, then just remotely access your Pi over SSH and try editing the rc.local file again. Make sure that you have the full correct filepath and that you haven’t made any typos.

That’s all there is to making a program run on startup!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset