Shell Programming in Python
Raspberry Pi DHT11 Sensor Project
The DHT11 and DHT22 sensors are low-cost components (~$1 to $10) that measure ambient temperature and humidity. Figure 5 shows a typical DHT11 setup with the data pin (blue wire) set to GPIO17 (note that sensor pin arrangements may vary).
The DHT11/DHT22 Python library is installed by:
pip install Adafruit-DHT
Listing 1 uses the Python Adafruit_DHT library to get the temperature and humidity (line 9) and then passes the data variables to a Zenity info dialog (line 11).
Listing 1
dht11_dlg.sh
01 #!/home/pi/.local/bin/xonsh 02 # 03 # dht11_dlg.sh - using xonsh to show DHT11 sensor data in a dialog 04 # 05 import Adafruit_DHT 06 sensor=Adafruit_DHT.DHT11 07 gpio=17 08 # Get the humidity and temperature 09 humidity, temp = Adafruit_DHT.read_retry(sensor, gpio) 10 # show the data in a Zenity info dialog 11 zenity --info --title=DHT11_Sensor_Data --width=150 --text=@( "Humidity: {} %\nTemperature: {}".format(humidity,temp) )
To run this script, enter:
$ xonsh dht11_dlg.sh $ # or make the script executable and then run it: $ chmod +x dht11_dlg.sh $ ./dht11_dlg.sh
Figure 6 shows the project hardware with the Zenity temperature and humidity dialog. Once you have the hang of how to use Zenity dialogs, you can modify this project to work with a variety of other Raspberry Pi sensors, such as BMP180 barometers, moisture sensors, or passive infrared (PIR) motion sensors.
LCD Project
A number of different LCD screens can be connected to a Rasperry Pi. For this project, we used a 16x2 (two rows of 16 characters) I2C LCD screen ($3-$15), which has an easy four-wire setup (SDA, data line; SCL, clock line; VCC, power input; and GND, ground).
The first step on this project is to check and enable I2C (inter-integrated circuit, a serial communication bus) on the Raspberry Pi, which you can do by running:
# Check I2C status: 0=enabled, 1 = disabled sudo raspi-config nonint get_i2c # Enable I2C: 0=enabled, 1 = disabled sudo raspi-config nonint do_i2c 0
The next step is to connect the LCD to the Raspberry Pi. The LCD lights up after it is wired to the Pi. The i2cdetect
utility shows active I2C hardware addresses (Figure 7).
A Python library for this hardware is installed with:
sudo pip3 install rpi_lcd
The rpi_lcd library assumes an I2C address of 27. Unfortunately the LCD device used was at address 3F, so you have to take an extra step to change the address:
# change the LCD default address from 27 to 3f cd /usr/local/lib/python3.9/dist-packages/rpi_lcd sudo sed -i 's/address=0x27/address=0x3f/' __init__.py
To test that the LCD screen is working, enter a few lines at the Xonsh prompt:
from rpi_lcd import LCD lcd = LCD() lcd.text("Hi from Xonsh",1)
The goal for this project was to show the Raspberry Pi's user time and idle time on the LCD screen. The top
utility displays Linux running processes and CPU stats. By piping the output from top
to a grep
statement, you can isolate a line of just the CPU stats. A third pipe with awk
creates a string with either the user time or the CPU idle times (Figure 8).
The output from these long Bash statements can be stored in variables to be used directly in Xonsh Python. The script in Listing 2 gets the user time (line 10) and the idle time (line 12) and shows these strings on the LCD display (lines 14, 15). The display is updated every five seconds by looping with a Python while statement (line 8) and delaying with a Bash sleep statement (line 16). By mixing Python and Bash in Xonsh, this script only requires eight lines of code. Figure 9 shows the LCD project with live data.
Listing 2
top_2_lcd.sh
01 #!/home/pi/.local/bin/xonsh 02 # 03 # top_2_lcd - show Bash Top data on a LCD screen 04 # 05 from rpi_lcd import LCD 06 lcd = LCD() 07 08 while True: 09 # Get the user CPU time 10 user=$(top -n 1 | grep %Cpu | awk '{printf "User Time: %s%%", $2}') 11 # Get the total idle time 12 idle=$(top -n 1 | grep %Cpu | awk '{printf "Idle Time: %s%%", $8}') 13 # Show user time on line 1, Idle time on line 2 14 lcd.text( user , 1) 15 lcd.text( idle , 2) 16 sleep 5
The 16x2 LCD display is a great device for a variety of projects. We had a lot of fun experimenting with other Bash utilities (e.g., df
, iostat
, vmstat
, and ifstat
) and displaying the result on the LCD screen.
Summary
Raspberry Pi users could really reap some benefits from lightweight Xonsh scripts. We found that the utility allowed us to do some manual testing at the command prompt and create some very lean code examples.
Because Xonsh is a Python shell, all control statements (e.g., while
and for
loops, if
statements) should be executed in Python and not in Bash.
Infos
- Xonsh: https://xon.sh/
- Zenity documentation: https://help.gnome.org/users/zenity/stable/
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.