[BeagleBone Black] Launch Python Script at Boot like Arduino Sketch

In order to use the BealgeBone Black as a flight computer or embedded system in a product, it must be able to launch programs we wrote on start up without having to manually start it after logging in via ssh. Something sweet and simple like an Arduino, when it start, it run the program you put on it. I spent days looking for ways to do this, editing init.d and all, none of them work. And finally, I came across this blog for Raspberry Pi and work perfectly!

So to be sure that the script is running without logging in through the computer, I decided to do a “blink” loop like what we do when we first started Arduino.

SAMSUNG

Setup LED Blink Python Script

To begin, we need to hook up a LED with Resistor on the BeagleBone Black. You can do it the lazy way like my pic above, or follow Adafruit’s schematic below, which hook up P8_10 to GND:

And now, onto the program. First ssh into BeagleBone Black and navigate to the folder where you want to store your program. For me, I am just going to leave it on debian user’s home folder.

// Navigate to User Home Directory
cd ~

// Show current directory
pwd

And thus, my user path will be:

/home/debian

And now, we are going to write our blink program like the one on Adafruit. I am going to assume that you guys have the Adafruit BeagleBone Black Python Library installed.

// Edit File blink.py
nano blink.py 

And then paste the code in there:

import Adafruit_BBIO.GPIO as GPIO
import time
 
GPIO.setup('P8_10', GPIO.OUT)
 
while True:
    GPIO.output('P8_10', GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output('P8_10', GPIO.LOW)
    time.sleep(0.5)

To test it to make sure the program run as intended:

 sudo python blink.py

Setup Run at Boot using Cron

Basically, Cron is a task management tool built-in into Debian. We want to add a new Cron Job at which a Python script will be ran at boot. I will save the details, read the blog I get this from for the theory.

We will open “crontab” and edit it as sudo. This way, Cron will run the script as the system boot and not when a user log in.

sudo crontab -e

You should get something like this:

boot script Cron

Scroll all the way down and add the following line to run blink.py at power on automatically without logging in.

@reboot python /home/debian/blink.py &

“@reboot” denote this action when a system is boot up. “/home/debian/blink.py” is the directory we got earlier. And finally remember the “&” to run this process in the background. DON’T FORGET about it, because it will hold the system up and don’t let the BBB boot the rest of the process. Save the file by Ctrl+X , Y and Enter.

Remove your USB cable and plug in a power supply. See if the LEDs blink after everything is loaded.

Ending Background Processes

Since the python script will run in the background, we need to find it and end it manually. Enter this to find the processing running off the file we wrote earlier.

 ps aux | grep /home/debian/blink.py

You should get something like:

root       873  0.1  0.6   7260  3264 ?        S    22:19   0:01 python /home/debian/blink.py

The number 873 is the process ID. Then, just use the process ID and kill the process.

sudo kill 873
Advertisements

6 thoughts on “[BeagleBone Black] Launch Python Script at Boot like Arduino Sketch

  1. Pingback: Corvettes, Beer, Microcontrollers, and Future Plans | My Broken Skateboard

  2. Pingback: Beaglebone crontab restart script not working – 1OO Club

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s