download bmp via WiFi and display using OnDiskBitmap, running on ESP32S3/CircuitPython 9.

Previous exercises post "Seeed Studio XIAO ESP32S3 Sense/CircuitPython display on 1.28" 240x240 GC9A01 Round IPS LCD" and "Display bmp with XIAO ESP32S3 Sense/CircuitPython on GC9A01 Round LCD using OnDiskBitmap, adafruit_imageload and adafruit_slideshow". It's another exercise running on Seeed Studio XIAO ESP32S3 Sense/CircuitPython 9.2.1, to download bmp from Python http server via WiFi, save to local filesystem, and display on 1.28" 240x240 GC9A01 Round IPS LCD using OnDiskBitmap. Actually, it's ESP32S3 version of another previous exercise "Pico 2 W/CircuitPython download bmp via WiFi, and display using OnDiskBitmap".

Connection between XIAO ESP32S3 Sense and GC9A01 Round IPS LCD, refer to previous exercise "Seeed Studio XIAO ESP32S3 Sense/CircuitPython display on 1.28" 240x240 GC9A01 Round IPS LCD".

To run http server on using Python (tested in Windows 11/Python 3):
> python -m http.server

Exercise Code:

cpy_XS3_gc9a01_OnDiskBitmap_wifi.py
"""
Seeed Studio XIAO ESP32S3 Sense/CircuitPython 9.2.1
with 1.28" 240x240 Round GC9A01 SPI IPS LCD

Download bmp to local filesystem and display it using OnDiskBitmap

In this exercise, ESP32S3 download image from web server,
save it on CircuitPython local filesystem, named "image.bmp,
then display it on 1.28" 240x240 Round GC9A01 SPI IPS LCD using OnDiskBitmap.

GC9A01 Connection
-----------------
Following the post
https://coxxect.blogspot.com/2024/12/seeed-studio-xiao-esp32s3.html

Prepare Image Server
--------------------
A simple way in Python to run a Web Server is switching to the folder with images, run:
> python -m http.server

Both the Image server and the ESP32S3 have to connect to the same WiFi network;
ssid/password = "ssid"/"password" in this exercise.

All testing images generated using "Image Creator in Bing", not real.
Resize to 240x240 RGB888/RGB565 .bmp using Python.
https://coxxect.blogspot.com/2024/11/resize-jpg-and-convert-to-bmp-in-rgb888.html

Prepare Libraries
-----------------
CircuitPython Libraries Bundle for Version 9.x needed:
(https://circuitpython.org/libraries)
- gc9a01.mpy
- adafruit_requests.mpy
- adafruit_connection_manager.mpy

To install libraries using circup, enter the command:
> circup install gc9a01, adafruit_requests
(Install adafruit_requests will install adafruit_connection_manager also.)

For CircUp, read:
https://coxxect.blogspot.com/2024/12/install-and-using-circup-circuitpython.html

Make filesystem writable
------------------------
In order to save "image.bmp" on CircuitPython local filesystem,
have to make the filesystem writable.
Visit: https://coxxect.blogspot.com/2024/12/set-circuitpython-file-system-writable.html
Copy boot_writable.py, save it in CIRCUITPY's '/', name it boot.py.
and reset.


"""
import os, sys
import board
import time
import displayio
import busio

import wifi
import socketpool
import adafruit_requests

import gc9a01

# Release any resources currently in use for the displays
displayio.release_displays()

display_spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)

disp_res  = board.D0
disp_dc   = board.D1
disp_cs   = board.D2
disp_blk  = board.D3

# ===========

display_bus = displayio.FourWire(spi_bus=display_spi,
                                 command=disp_dc,
                                 chip_select=disp_cs,
                                 reset=disp_res)

display = gc9a01.GC9A01(display_bus, width=240, height=240, backlight_pin=disp_blk)

#=======================================
info = os.uname()[4] + "\n" + \
       sys.implementation[0] + " " + os.uname()[3] + "\n" + \
       gc9a01.__name__ + " " + gc9a01.__version__
print("=======================================")
print(info)
print("=======================================")
print()

# Create a dummy bitmap
dummy_bitmap = displayio.Bitmap(240, 240, 1)
dummy_palette = displayio.Palette(1)
dummy_palette[0] = 0x000000
bmpTileGrid = displayio.TileGrid(dummy_bitmap, pixel_shader=dummy_palette)

group = displayio.Group()
group.append(bmpTileGrid)
display.root_group = group
    
ssid = "ssid"
password="password"
print("Connecting", ssid)
wifi.radio.connect(ssid, password)
print("wifi.radio.connected:", wifi.radio.connected)
print("wifi.radio.ipv4_address", wifi.radio.ipv4_address)

#download bmp
url = "http://192.168.22.211:8000/img_009_pythonConverted_240x240_rgb888.bmp"

pool = socketpool.SocketPool(wifi.radio)

# Both work in my test
#requests = adafruit_requests.Session(pool, socketpool.SocketPool.socket)
requests = adafruit_requests.Session(pool, wifi)

# This line sometimes fail in first try,
# so I place it in try..except...block.
#response = requests.get(url, stream=True)  # Enable streaming mode
while True:
    try:
        print("requests.get()", end="")
        response = requests.get(url, stream=True)  # Enable streaming mode
        print(" - success")
        break
    except OSError as err:
        print(" - failed! retry in 1 second")
        print("OSError:", err)
        time.sleep(1)

save_image_file = "/image.bmp"

print("Save to", save_image_file)

with open(save_image_file, "wb") as file:
    for chunk in response.iter_content(1024):  # Download in chunks of 1024 bytes
        file.write(chunk)

print("Saved")

file.close()
response.close()

print("Display", save_image_file)
bitmap = displayio.OnDiskBitmap(save_image_file)
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

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator