A common issue many makers face when they move from microcontrollers to Single-Board-Computers is how to get them to run a program automatically when they are powered on. People who have cut their teeth on Arduino, Micro:bit, and other microcontroller-based systems, are used to writing a single program, uploading it to their board and having it run continuously from the moment the power is applied. The Onion Omega2 and other SBCs don't behave that way, as small computers they wait for user input before they will do anything besides boot up. Fear not though, there is a simple way to make the Omega2 behave more like the microcontrollers you are familiar with. And like most problems on linux systems, the solution lies in a text file...
All operating systems run through a list of commands and programs during bootup that get the computer ready to run, all we need to do is to add our program to the end of this list and then our program will run automatically as soon as the system has booted. It won't behave exactly like a microcontroller though as we still need to wait for the Omega2 to boot before the program will run.
We'll need a program to automate before we proceed. For this exercise, We'll use a simple python LED flashing script. First, make sure you are in the /root/ directory:
cd /root/
Now create a folder to store the python programs:
mkdir python
Change into the python directory:
cd python
Now open a new file flash.py
:
nano flash.py
This should open an empty text editor, copy the following into it:
#This demo program flashes GPIO0 indefinitely. import onionGpio import time # specify sleep duration to be used in the program sleepTime = 0.5 # instantiate a GPIO object gpio0 = onionGpio.OnionGpio(0) # set to output direction with zero (LOW) being the default value gpio0.setOutputDirection(0) # create a variable to hold the value of the LED ledValue = 1 #create a variable as a loop counter. loopCount = 0 # infinite loop - runs main program code continuously while 1: # set the GPIO's value gpio0.setValue(ledValue) loopCount += 1 print "flashing...%d" %loopCount # flip the value variable if ledValue == 1: ledValue = 0 else: ledValue = 1 # make the program pause time.sleep(sleepTime)
Press <CTRL> - <X>
to exit, following by <Y>
then <RETURN>
to save.
Now test your python flash program with:
python flash.py
You should see "flashing...1,2,3..." printed to the terminal while the LED attached to GPIO0 should also be flashing. Press <CTRL> - <C>
to stop the program.
Now that we have a sample program, we need to get the Omega2 to run this program automatically. On the Omega, one the list of tasks to be run at boot is called rc.local
open this file with:
nano /etc/rc.local
You should see a file containing the following:
# Put your custom commands here that should be executed once # the system init finished. By default this file does nothing. exit 0
As you can see this file is unused by default on the Onion Omega2. To make our python program run automatically, add the following line just before the exit 0
line:
python /root/python/flash.py
If you are running some other programming language, this is where you would put the command to run your program. Whatever command you would use to run your program from the command line, insert it here.
Now restart your Onion Omega2 to see whether you have been successful in automating your program launch. Remember the Omega2 takes a MINIMUM of ~60 seconds to boot.
If all has gone according to plan, the LED on GPIO0 should start blinking without any input from you! If not, double check to make sure you can manually run your python file, also check the /etc/rc.local
file for any mistakes.
But before we pop the digital-champagne cork, it turns out we've created a bit of a problem for our operating system. The rc.local
process is designed to run through each of its tasks at boot and then to close down. However, since we've added a python program that never ends, the rc.local
can't ever end. You can see one of the problems it causes if you issue the reboot
command. You'll see pretty quickly that the reboot command no longer works! But fear not, all we have to do to fix our mistake is to create a simple daemon to handle our python program. A "daemon" is just a process that runs in the background without requiring user intervention.
Re-open the /etc/rc.local
file with:
nano /etc/rc.local
Now change the line we created before from:
python /root/python/flash.py
To:
start-stop-daemon -b -S -x python /root/python/flash.py
The start-stop-daemon
command creates a daemon to handle the execution of our program. The -b
switch causes the program to be executed in the background. The -S
switch tells the daemon to start our program. And the -x
switch tells the daemon that our program is an executable.
Now press <CTRL> - <X>
to exit, following by <Y>
then <RETURN>
to save.
Now you can restart the board, you'll have to manually switch the power as the reboot command is still broken. However, once the board has finished restarting you should be able to issue the reboot
command without any problems. We can also use our daemon to temporarily stop our program without having to remove it from the rc.local
file and reboot. Just enter:
start-stop-daemon -K -x python
That should have stopped the program from running in the background. The -K
switch stands for KILL. While we have stopped the daemon from running, it will still restart the next time the system reboots. To stop it permanently we need to comment out or remove the entire line from the /etc/rc.local
file.
And that should be everything you need to know to get your programs to run automatically at boot time. I hope this guide has been helpful, if you have any comments, questions or issues please leave a comment below or head over to our forum, where our full-time team of makers will be happy to help you out. Happy Making!