Pico 2 W/CircuitPython download bmp via WiFi, and display using OnDiskBitmap.
Previous Raspberry Pi Pico 2W/CircuitPython 9 exercise display bmp in local filesystem on 1.54" IPS 240x240 with SPI ST7789 driver using displayio.OnDiskBitmap. This exercise using Pico 2 W wireless feature, download bmp via WiFi to local filesystem, then display on ST7789 LCD using OnDiskBitmap.
We are going to setup a python http server on Windows 11, then Pico 2 W connect to the same WiFi network ("ssid" here) and request bmp from the http server.
The simplest way to setup http server in Python is:
python -m http.server
Then you can reach the http server at http://[IP address]:8000/Exercise code:
cpy_pico2w_wifi_OnDiskBitmap.py
"""
Raspberry Pi Pico 2 W/CircuitPython 9
to display bmp on 1.54" 240x240 ST7789 SPI IPS
Download bmp from wifi and
display on LCD using OnDiskBitmap
Connection:
-----------
GND GND
VCC 3V3
SCL GP18
SDA GP19
RES GP20
DC GP21
CS GP17
BLK GP22
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- adafruit_st7789.mpy
- adafruit_requests.mpy
- adafruit_connection_manager.mpy
Remark:
If you don't know how to install CircuitPython Libraries Bundle, read the video in the post:
https://coxxect.blogspot.com/2024/11/raspberry-pi-pico-2circuitpython-9.html
Connection kept same as in:
https://coxxect.blogspot.com/2024/11/raspberry-pi-pico-2circuitpython-9.html
All testing images were in 240x240 RGB888/RGB565, resized using Python, read:
https://coxxect.blogspot.com/2024/11/resize-jpg-and-convert-to-bmp-in-rgb888.html
In order to write bmp on Pico/CircuitPython filesystem,
have to write boot.py in CIRCUITPY to make it writable. read:
https://coxxect.blogspot.com/2024/12/set-circuitpython-file-system-writable.html
"""
import board
import busio
import displayio
import adafruit_st7789
import wifi
import socketpool
import adafruit_requests
import gc
disp_sck = board.GP18
disp_mosi = board.GP19
disp_res = board.GP20
disp_dc = board.GP21
disp_cs = board.GP17
disp_blk = board.GP22
# Release any resources currently in use for the displays
displayio.release_displays()
disp_spi = busio.SPI(clock=disp_sck, MOSI=disp_mosi)
display_bus = displayio.FourWire(spi_bus=disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
ssid = "ssid"
password="password"
print("Connecting", ssid)
wifi.radio.connect(ssid, password)
print("wifi.radio.connected:", wifi.radio.connected)
#download bmp
url = "http://192.168.22.211:8000/img_003_pythonConverted_240x240_rgb888.bmp"
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, socketpool.SocketPool.socket)
response = requests.get(url, stream=True) # Enable streaming mode
print("Save to /image.bmp")
with open("/image.bmp", "wb") as file:
for chunk in response.iter_content(1024): # Download in chunks of 1024 bytes
file.write(chunk)
file.close()
print("Saved")
print("Display /image.bmp")
bitmap = displayio.OnDiskBitmap("/image.bmp")
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
group = displayio.Group()
group.append(tile_grid)
display.root_group = group
while True:
pass
Comments
Post a Comment