Testing the Adafruit PyPortal touchscreen
Data from the Web
The only thing missing after the wallpaper text is the Internet connection, so you acquire the weather data. The decisive commands are shown in line 11 of Listing 3, which imports the SSID and the WiFi password from the secrets.py
file. The code in lines 14-21 then establishes the connection with the ESP32 chip.
Listing 3
Getting the Data
01 import board 02 import busio 03 from digitalio import DigitalInOut 04 import time 05 import neopixel 06 import displayio 07 from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager 08 09 # --------------------------------------------- 10 11 from secrets import secrets # file secrets.py 12 13 def get_wifi(secrets): 14 esp32_ready = DigitalInOut(board.ESP_BUSY) 15 esp32_gpio0 = DigitalInOut(board.ESP_GPIO0) 16 esp32_reset = DigitalInOut(board.ESP_RESET) 17 esp32_cs = DigitalInOut(board.ESP_CS) 18 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 19 esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, esp32_gpio0) 20 status_rgb = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) 21 return adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_rgb) 22 23 # --- main-loop ------------------------------ 24 25 connection = get_wifi(secrets) 26 try: 27 response = connection.get("https://wttr.in/München?AT0&lang=en") 28 text = response.text 29 except: 30 text = "No data received" 31 group.append(get_label(text, FONT, COLOR)) 32 display.show(group) 33 34 while True: 35 time.sleep(60)
Once the connection is up, you can send standard HTTP requests like GET
and POST
and evaluate the server response – typically by HTML, but with JSON to query data servers. In contrast, wttr.in returns plain text (Listing 3, lines 28 and 30), which the program outputs directly after converting it to a label (Figure 5). I adapted the URL in line 27 to suit the small PyPortal, so the output only shows the current weather data instead of a detailed multiple-day forecast. Change the city name before the question mark in the URL for your location, and use lang=en
for English output.
![](/var/linux_magazin/storage/images/issues/2020/237/adafruit-pyportal/figure-5/771274-1-eng-US/Figure-5_large.png)
With fewer than 50 lines of code, the display shows the current weather data, but the program still lacks the ability to query the weather report regularly. To do this, you need to add a get()
command to the infinite loop at the end of the program. Also, you might want to display the current time and the indoor temperature from the built-in sensor in the PyPortal.
Unfortunately, the temperature sensor in the PyPortal turns out to be a flop. The display backlighting heats it up, so it delivers useless values. As a consequence, Adafruit got rid of this sensor in the PyPortal Pynt and Titano variants. A better alternative would be to attach an alternative device (e.g., a BME280 or LM75) with a short cable to the I2C interface.
An extended version of this sample program is on GitHub [5].
Better than the Raspberry Pi?
The sample project has shown at least some of the PyPortal's capabilities. The question arises as to how the intelligent Adafruit display compares with a Pi Zero plus a miniature display.
The biggest benefit the PyPortal offers is its compact design. The processors, components, and connections on the rear are unlikely to wear out. Conversely, the Raspberry Pi is attached in a fairly precarious way to the socket array or HDMI port on a typical miniature screen. In the former case, the screen usually blocks pins that it doesn't need, so connecting additional sensors then means purchasing a multiplexer board.
Unlike the Raspberry Pi, the PyPortal comes with a tiny speaker for sound output. It is more suitable for warnings and signal tones than for listening to music, but the Pi Zero lacks this feature completely.
Regardless of the hardware, the intelligent display is far easier to set up than the Pi Zero and a small screen. Tinkering with the /boot/config.txt
file with special overlays or HDMI parameters is not necessary. To switch it off, all you have to do is pull the plug, because there is no operating system that needs to be shut down cleanly first.
The PyPortal's current draw is about 200mA when the display is on and about 70mA otherwise. Therefore, the display is not suitable for battery operation. Even in continuous operation, though, it is unlikely to increase your electricity bill that much. A display of exactly the same size for the Raspberry Pi was not available for comparison measurements, but with a 4-inch screen from Waveshare, the combination clocked up to 260mA. The difference of 60mA between the PyPortal and Pi Zero including display can be measured, but in practice it hardly plays a role.
Of course, PyPortal does not always give you the better solution because many of its advantages turn into disadvantages in certain applications. If you need considerably more screen surface or better resolution, there is no escaping the Raspberry Pi, which also offers a faster CPU, more RAM, and multitasking. These more extensive resources also give you scope for more demanding and complex programs. When designing graphical user interfaces, especially, toolkits with layout management offer more possibilities for the developer than the PyPortal's displayio
library.
Conclusions
As the lively community that has grown up around the PyPortal shows, Adafruit has its finger on the users' pulse with this display. Thanks to the fixed form factor, everyone programs the same hardware, and open source promotes the exchange of ideas and solutions. If you want to discover more details about the PyPortal or are looking for suggestions, you will find the guides [6] to be a comprehensive resource. In many projects, teaming the Raspberry Pi and the PyPortal also makes sense. The Pi handles the computationally intensive and memory-intensive work, and the PyPortal presents the results.
Infos
- Buying the PyPortal display: https://www.adafruit.com/product/4116
- Housings and brackets: https://www.thingiverse.com/search?sort=relevant&q=PyPortal&type=collections
- Main documentation: https://learn.adafruit.com/adafruit-pyportal
- Generating bitmap fonts: https://learn.adafruit.com/custom-fonts-for-pyportal-circuitpython-display
- Sample project for this article: https://github.com/bablokb/pyportal-wttrin
- All PyPortal guides: https://learn.adafruit.com/products/4116/guides
« Previous 1 2 3
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.