未加星标

Display System Information On The Onion Omega2 OLED Expansion

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

So I was a recent Kickstarter backer for the Onion Omega2 Internet of Things (IoT) device. My package finally came and I wanted to see what I can do with it. My package included the Omega2, expansion dock, and OLED display so I figured it would be cool to write an application that displayed to the screen.

A problem I always had with the Raspberry Pi, my other favorite IoT device, is that I never knew the IP address or system information because I was always using it as a headless unit. With that in mind, I decided to write an application that displayed this information to the Onion Omega2 OLED screen at boot.

We’re going to see how to write a basic script to show system informationusing python and the OLED extension library.

If you’re not familiar with the Omega2, it is a small IoT device with built in WiFi that has a price of only $5.00. However, it has very little storage space and very little memory. Based on this information, we’re going to create what is shown in the image below.


Display System Information On The Onion Omega2 OLED Expansion

We’re going to display the network IP address of the unit and constantly display the current memory usage. This memory tracking will refresh constantly.

The Requirements

To be successful with this Internet of Things project there are a few hardware and software requirements.

Onion Omega2 Onion Omega2 expansion dock Onion Omega2 OLED expansion display Python Python OLED module

The hardware can all be purchased for pretty cheap from the Onion Omega2 website, but the software installation can be a bit tricky.

After connecting to the Omega2 via SSH, execute the following commands:

opkgupdate opkginstallpython-lightpyOledExp

This should leave us with just enough space for a nice application. Installing the full version of Python will require some external storage which you may or may not have.

Creating a Python Script to Calculate and Display Information

Because of the Onion Omega2, I was introduced to Python development for the first time. My first choice would have been Golang, but I had some issues getting a MIPS32 build going.

Create a /root/status.py file on your device and include the following code:

from OmegaExpansionimport oledExp import os import time oledExp.driverInit() ipv4 = os.popen('ip addr show apcli0').read().split("inet ")[1].split("/")[0] mem_total = os.popen('free -h | grep Mem |awk \'{printf "%.2f %s", $2/1024, "MB"}\'').read() while True: mem_used = os.popen('free -h | grep Mem |awk \'{printf "%.2f %s", $3/1024, "MB"}\'').read() oledExp.setCursor(0,0) oledExp.write("IP") oledExp.setCursor(1,0) oledExp.write(ipv4) oledExp.setCursor(3,0) oledExp.write("MEMORY") oledExp.setCursor(4,0) oledExp.write(mem_used + " / " + mem_total) time.sleep(3)

Let’s break down what is happening in the above script.

The first thing we are doing is importing the OLED module and other Python dependencies that we’ll be using within our project.

Before we can use our OLED display we have to initialize it. This initialization only needs to happen one time at which point things can be rendered or cleared from the screen.

This is where things can get a little nutty:

# http://stackoverflow.com/a/38394394/498479 ipv4 = os.popen('ip addr show apcli0').read().split("inet ")[1].split("/")[0]

You can see in the above that we are trying to get the IPv4 address from the WiFi module named apcli0 . I didn’t come up with this line of code, but instead got it from Stack Overflow , due to my novice understanding of Python. What is happening is we are executing a system command and parsing it out until we end up with the inet value.

Based on our knowledge of the os.popen command we can come up with something like this:

mem_total = os.popen('free -h | grep Mem |awk \'{printf "%.2f %s", $2/1024, "MB"}\'').read()

The above line will do nearly the same thing, but parsing is happening via the shell command rather than through Python. We’re saying we want to get the total memory in kilobytes and convert it to megabytes for displaying on the screen.

Finally we have our loop which will continuously refresh the OLED display forever:

while True: mem_used = os.popen('free -h | grep Mem |awk \'{printf "%.2f %s", $3/1024, "MB"}\'').read() oledExp.setCursor(0,0) oledExp.write("IP") oledExp.setCursor(1,0) oledExp.write(ipv4) oledExp.setCursor(3,0) oledExp.write("MEMORY") oledExp.setCursor(4,0) oledExp.write(mem_used + " / " + mem_total) time.sleep(3)

The total memory on the device doesn’t change, but the memory used does. Every loop iteration we get the new memory and display all the variables on the screen. To prevent just a constant flicker, we sleep the loop for three seconds every iteration.

You should be able to test your script by executing the following:

python /root/status.py

That is all good, but we really want this information to display when the device starts. We’ll need to create a boot command to make this happen.

Setting the Script to Start at Boot

It is not difficult to start our script at boot. We’re pretty much just going to paste our execute command somewhere. While connected to the Omega2 via SSH, open the /etc/rc.local file in a text editor. This file should contain the following:

# Put your custom commands here that should be executed once # the system init finished. By default this file does nothing. python /root/status.py & exit 0

Noticethat our execute line now contains an ampersand at the end. Because we have an infinite loop in our script, we need to send it to the background, otherwise our Omega2 will never finish the boot process. The ampersand allows us to do this.

Reboot your device and see what happens.

Conclusion

You just saw how to write a Python script that displayed system information on the Onion Omega2 OLED expansion screen. This could be very useful if you need to monitor for changes on the system or maybe you get more expansions that can monitor other things. Because the Omega2 uses OpenWRT, you have to think a little differently when you develop.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: PythonRaspberry PiRYWiFiIPv4
分页:12
转载请注明
本文标题:Display System Information On The Onion Omega2 OLED Expansion
本站链接:http://www.codesec.net/view/520793.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(68)