We have yet another great update for you this month with the introduction of Python3 compatible versions of our Python modules! Now you can take advantage of the greater features available in Python3 like better library support, more intuitive syntax and a more-future-friendly program to name a few. Not only that, but with an official announcement on the end-of-life date for Python2 being 2020, there’s no better time to migrate onto Python3.

With this new update the Relay, OLED, ADC and Servo Expansions can now be controlled from Python3. Not only that, but hot on the heels of our I2C enhancements last month, you can now also interact with I2C devices in Python3. For those that may have missed that update, check it out, there’s a whole lot of useful improvements that can be used on a daily basis.

We will take you through the installation process and also provide you with examples on using the modules. At the bottom of the article you will also find links to any relevant documentation you may need.

Installation

To start off you need to make sure you are connected to the Omega’s command line as we will need to install Python3 as well as the modules. So first run the following:

opkg update
opkg install python3-light

Now that Python3 is installed, let’s go ahead and install the Relay, PWM, OLED, and ADC modules, respectively:

opkg install python3-relay-exp
opkg install python3-pwm-exp
opkg install python3-oled-exp
opkg install python3-adc-exp

Using the Modules

The Python3 modules work exactly like their Python 2.7 counter-parts. Here’s an example program

This will write “Omega2 :heart: Python3” on the OLED Expansion:

from OmegaExpansion import oledExp

print('Writing to OLED screen')
text = 'Omega2 <3 Python3'

# initialize the screen
oledExp.setVerbosity(0)
ret = oledExp.driverInit()

# set the cursor position
ret = oledExp.setCursor(3,0)

# write the message
ret = oledExp.write(text)

print('Done')

Assuming you named the Python program oled-test.py, run the program with the following:

python3 oled-test.py

You should see something like this:

If you would like to explore more code examples, just check out the examples we have on GitHub here and here.

As mentioned above, full documentation for all of the modules is also available here.

Feedback

We would love to hear what you think about the Omega’s Python3 compatibility. Let us know in the Onion Community or tweet @OnionIoT

That’s it from us this week, and happy Pythoning and we will link back up soon with more great updates!