Lab 12. Zephyr RTOS: Shell, LittleFS, Webserver

This lab walks you through building three Zephyr applications for Sparrow:

  • Example 1: UART Shell app with standard commands + a custom I2C scan command
  • Example 2: UART Shell app + LittleFS mounted from internal flash (ESP32‑C6)
  • Example 3: Wi‑Fi webserver that reads BME680 sensor data and serves it to a browser

Verify your environment

Open a terminal in your Zephyr workspace and run:

west --version
west boards | grep esp32c6

If the board (ESP32C6 Xiao) is listed and west works, continue.

Tip: If you switch branches or update modules often, use:
> west update

1. Common Project Layout

Each lab uses the same basic structure:

<APP_ROOT>/
  CMakeLists.txt
  prj.conf
  src/
    main.c
  boards/
    esp32c6_devkitc_esp32c6_hpcore.overlay
  (optional) Kconfig

Build command used in all labs:

west build -b esp32c6_devkitc/esp32c6/hpcore -p auto .

Flash:

west flash -d build --runner esp32

Monitor serial output:

screen <your_port_name> 115200

2. Sparrow Shell + Custom I2C Scan Command

Goal

Build a UART shell application that includes standard Zephyr shell commands and adds a custom command:

  • i2c_scan — scans the I2C bus for responding device addresses

What you will build

  • A booting application with a serial shell prompt like:
    • sparrow$
  • Shell commands available (examples):
    • help, kernel, device, log, etc.
  • Custom command:
    • i2c_scan

Create the project

Create a new folder, e.g. sparrow_console and unzip the project here.

sparrow_shell_i2c_scan/
  CMakeLists.txt
  prj.conf
  src/main.c
  boards/esp32c6_devkitc_esp32c6_hpcore.overlay

Build, flash, and test

Build:

west build -b esp32c6_devkitc/esp32c6/hpcore -p auto .

Flash:

west flash -d build --runner esp32

Open the serial console and test:

  • help
  • i2c_scan

Expected: you see a list of detected I2C addresses or “no devices found”.

Troubleshooting

  • If no devices are found:
    • verify SDA/SCL pins match your wiring
    • confirm pull-ups are present (internal/external)
    • verify bus speed matches device capabilities
  • If shell doesn’t appear:
    • verify console device selection in overlay (chosen nodes)
    • ensure shell backend is serial and autostarted

3. Sparrow Shell + LittleFS Mounted From Internal Flash

Goal

Build a shell app that provides a “Linux-like” file navigation experience (as much as Zephyr supports), using:

  • LittleFS
  • mounted on an internal flash partition
  • with fs shell commands enabled

Typical workflow in shell:

  • fs mount (or auto-mount at boot)
  • ls /lfs
  • cd /lfs
  • pwd
  • create/read files (depending on enabled commands)

Build the Project

Create a new folder, e.g. sparrow_littlefs and unzip the project here.

Build, flash, test

Build:

west build -b esp32c6_devkitc/esp32c6/hpcore -p auto .

Flash:

west flash -d build --runner esp32

Test in shell:

  • fs (to see available fs commands)
  • ls /
  • ls /lfs (it is mounted at boot)
  • cd /lfs
  • pwd
  • fs mkdir /lfs/folder1
  • fs write /lfs/folder1/hello.txt -o 0 48 65 6c 6c 6f 20 57 6f 72 6c 64 21
  • fs cat /lfs/folder1/hello.txt

Expected: you can navigate the mount point and list directory contents, create and view files, etc..

Troubleshooting

  • Devicetree error about duplicate partition labels:
    • ensure you only define one storage_partition label
    • don’t copy/paste partition blocks twice
  • Mount fails:
    • verify the storage partition exists in build/zephyr/zephyr.dts
    • ensure partition does not overlap other partitions
    • confirm the flash driver and flash map are enabled
  • Shell missing fs commands:
    • verify CONFIG_FS_SHELL (and any related symbols) are enabled

Assignment: Add a BME680 logging service with a new “bme_log” shell command, plus a background thread that appends CSV entries with uptime timestamps to a file named bme_log.dat.

Usage examples:

bme_log start 5
bme_log status
bme_log interval 10
bme_log stop
fs cat /lfs/logs/bme_log.dat

4. Wi‑Fi Webserver

Goal

Build a webserver that:

  • connects to Wi‑Fi (WPA2‑PSK)
  • obtains an IPv4 address via DHCP
  • serves a web page to a browser

Important performance note (ESP32 Wi‑Fi memory)

Wi‑Fi + TLS stacks can be memory hungry. For a simple HTTP server:

  • keep responses small
  • avoid allocating large buffers repeatedly
  • limit concurrent connections (1 client at a time is fine for a lab)

Configure the Project

Create a new folder, e.g. sparrow_web and unzip the project here.

Build, flash, test

Build:

west build -b esp32c6_devkitc/esp32c6/hpcore -p auto .

Flash:

west flash -d build --runner esp32

The code runs a HTTP static file server backed by LittleFS and auto-started at boot; it serves files from /lfs/www on port 8080 and uses the HTTP server’s static_fs handler.

In the serial shell, you need to run the following commands:

1. Connect to WiFi:

wifi scan
wifi connect -s "SSID" -p "PASS" -k 1
wifi status

2. Check the webserver is up and running and get the board IP address:

net http
net iface

3. Write a simple index.html file into /lfs/www:

fs write /lfs/www/index.html -o 0 3c 68 31 3e 48 69 3c 2f 68 31 3e

4. Browse: http://[board-ip]:8080/

Expected:

  • browser receives a response containing “Hi”

Assignment: Add a dynamic HTTP endpoint that serves the current contents of bme_log.dat as an HTML page with a 10‑second auto‑refresh. So, if you conect to http://[board-ip]:8080/bme_log it will automatically serve the contents of the log file.

iothings/laboratoare/2025/lab12.txt · Last modified: 2026/01/03 23:05 by dan.tudose
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0