Hi, I'm Dave here at Core Electronics and in this video, I'm going to show you how to get the Onion Omega to run your program automatically on boot-up without any user input required.
Okay, to demonstrate this obviously we'll need a program to automate. So, I've written a simple Python code that we can see here, which is designed just to flash the LED attached to GPIO zero and output some text to the console. We can see it run here. Alright, we can see the LED is flashing and we are getting some text on the console if you'd like a copy of that code, it's on the tutorial page attached to this video. Fortunately for us, it's actually quite easy to get the Onion Omega to run a program automatically. First thing to do is to open this file in the text editor which is
/etc/RC.local
Any commands added to this file will run automatically on boot. So, if we add the command
Python /root/python/flash.py
right at the bottom of the file just before the exit zero-line exit and save. Now if we reboot, which will still take about a minute, hopefully, we should see the Python program run automatically.
Excellent, we can see the LED has started flashing all on its own. We don't see the text output on the console because the program is now running in the background. Now while this may seem like everything we need, we have in fact created a bit of a problem.
Now, unfortunately, the problem we've created is because the RC dot local file is meant to run through on boot and then finish and since our program is running on an infinite loop it'll never exit, and the RC Telugu file never finishes the boot sequence. You can spot one of the many flaws this will cause if we try and issue a reboot command, you will notice doesn't work anymore. However, as good news this problem is also easy to fix fixing this problem is very easy to open an RC.local again and in front of our Python line that we added add this. This line here will create instead of launching the program directly, will create a daemon to handle the launch for us. The B switch here is for the background where right so the program will run in the background the capital S is for start and the lower-case X is for executable. So, if we save the changes to this file now and restart the Onion which will have to do physically, of course, the reboot command won't work yet. I should hopefully still see the program running automatically in the background but without causing any other problems.
Excellent, you can see the LED is flashing again and now if I try the reboot command, it works perfectly.
So now we have the Onion Omega set up to automatically run our program in the background on boot and to do it properly without disrupting the rest of the boot sequence.
One last thing that is always useful is the command to stop the program and that one is quite simple. Let's just start stop daemon the capital K for "kill", lower case x for executable and then the program that we use the daemon to run. There we are the led has stopped flushing.
And that's how we set up the Onion Omega to automatically run a program on boot. I hope that was helpful if you have any comments issues or questions feel free to comment below or head over to our forum. We've got a team of makers who are just itching to help you out however they can.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.