Hi, I'm Dave here at Core Electronics and today we're going to learn how to set up Python on the Onion Omega2. Let's get started.
All right as always, we have an Onion Omega2 board, plugged into a suitable dock and I've also hooked up some LEDs to GPIO 0 and GPIO 1, so that we can get started on controlling those pins without python programs later.
Ok now we're going to perform this installation using the GUI console, so we need to make sure that our PC and the Onion Omega are both connected to the same Wi-Fi network and that we have a working internet connection. Bring up your browser go ahead and log in either to your onions IP address or using its omega - name. I'm going to use the IP address should bring up the console login momentarily. Login, again remember the default username is root and password is Onion Ear. If you've changed that password, then enter the password you set up. Alright now we're in the GUI console the first thing we need to do is bring up the terminal. Login to the terminal as well same login same password. Ok, now we're ready to get started. Ok, now we can go ahead and install python first command we need to run here's another OPKG update which will update the package manager to make sure we get the most recent version of python available. When you're installing anything using a OPKG it's always a good idea to run the update command first. Okay, now that's completed successfully we can see signature check passed, so now we can install python with this command so opkg install and python. Now before we press return we have a couple of different options or four options to be more precise. About which version of python, we install. There's Python 2.7 which is commonly known as Python and this Python3. There's also a like version, of both of those flavours which I would personally recommend using on the Onion Omega, because of its limited storage space available. I think you're better off to install the light version of Python and then to manually install the modules that you need for a specific project. But that's just me you can certainly install the full version of Python it will still fit. If you wanted to install Python you'd use the commands, you can see there on the screen. If you wanted the light version, you would add - light wanted to install Python 3 you would add the 3 at the end and if you wanted the light version of Python 3 you're going to add the 3 and the - light as I said I'm going to use Python 2 and the light version.
Ok, successfully installed. However, before we go running off and start work on our first Python program we should probably cover how to install Python modules, especially since I installed the light version of Python and so very few modules have been installed with it. There's two ways to install the Python modules or packages one is using opkg, which is certainly the simplest method but only has a reduced number of libraries available and there's also using pip, so I'll start with opkg. We'll do the opkg update it's not necessary since I only did it a few minutes ago but it's good practice. Okay now if we want to list all the packages available, you go opkg list pipe symbol grep Python or Python3, if you only want Python3 specific packages. As you can see, this list both great many Python packages, including one we really need to make yourself Python pip that's for the next session and a great many Python3.
Right, before we move on to pip there is a package that we should install without opkg that we'll use later and that pi Onion GPIO. Which is the Python module that'll let us control the Onion Omegas GPIO pins.
So, we can install our one without opkg install. Okay pyOnion capital o and GPIO with a capital G only. Seems already had that one installed. So, we're good to go alright we'll also install pip which is opkg install Python-pip or Python3-pip, if you're using Python3.
Ok pips been successfully installed. If you wanted to install a module using pip it's much the same as opkg its pip install and then the module name.
Now that we have Python successfully installed let's write our first hello world program.
Click on the Onion symbol to go back to your console start page and bring up the editor. Now I like to create a folder inside root there is called Python to store my Python programs when I'm experimenting. You might not have the folder there yet so just click on new folder to create it. Alright once we're in the folder let's create a new file and we'll call this hello world.py. Ok the file has been created but a little quirk of this interface, it doesn't open it you have to then click on the file you created to open it. Alright there's an empty file, now to get our Python program to say hello world, all we must do is add the command print a little world, with single quotation marks. Click save on that file, confirm say yes, save successful. Excellent now we can go back to the terminal, change directory into the Python folder we created. You can see it's got a few Python programs there or most note is hello world py. To run it all we do is type python or python3, if you're using python3, there we go hello world, program has been successfully run.
Let's have a look at a program that's a bit more interesting than just hello world, something that makes use of the GPIO input/output pins present on the Onion Omega tube.
If you head into the editor and you can create a new file and type what you see here, or you can get this code from our tutorial page. I'm going to open this file blink.py which is one I've created earlier. Let's just run through it shall we. The first thing we need to do is, import a couple of libraries, we import the time libraries, so that we can keep track of time to get the led to flash on and off and we import the Onion GPIO library so that Python knows how to turn the GPIO pins on and off. That's all-in lowercase except the G there are a few uppercase lowercase nuances with the Onion libraries so they do take a little getting used to. Alright, so first up we've got a variable that's controlling which pin we're gonna operate on I was using 0 I'm going to change it to pin 1. The next line line 5 creates a new object to control the GPIO pins which is GPIO object equals onionGpio.onionGpio. So, this is just going to use the Onion GPIO library to create an object to control GPIO pin 1 and these is pin ones come from that line there. Just a heads up the first Onion GPIO the first O is lowercase the second Onion GPIO the first O is uppercase. As I said there's a few eccentricities. Line eight down here is going to set that pin to be an output. So, we've got status equals DPI object dot set output direction and the zero here just means that it's going to start in the low state. If we're writing a more complicated program we could have a line that actually checks status variable, to make sure this command executed okay but since we're just experimenting here we'll just assume that it's worked. We'll know pretty quickly if it hasn't because the light won't flash. Okay we have down here loop equals one, because the loop it's an infinite loop it'll always equal one and value equals zero this is just a variable to store the LED state in. So, we've got while loop equals one and loop always equals one, so like I said this loop will just continue at infinitum and this section of code here is what toggles the value. If the value is equals zero, then we set it to one anything else and we set it to zero and now we write the new value to the GPIO pins status equals gpiObj.setValue value. So that line here will either turn the pin on or off depending on the value variable and we're also going to print out the status of the GPIO pin, so we've got some feedback on the terminal as well. We have GPIO set to GPIO num and value being written in there and then we need a delay to control how fast the LED is going to flash. This one's here set for 5 seconds but I think that's far too long so I'm going to set it to 1 second. All right now we should be able to save that file, return to the terminal and execute it with Python link.py.
And if we look on the board, we should see the LED flashing.
I hope you've all managed to successfully setup python on your Onion Omega tube. If you've run into any issues, please don't hesitate to leave comments below or head over to our forum with a full-time team of makers here and we're always happy to help wherever we can.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.