14. December 2023

How to connect USB and PS/2 Keyboards to ESP32 with Rust no_std and std

Have you ever wondered how to connect a PC keyboard to your ESP32 for innovative projects? Whether it’s an old PS/2 keyboard or a modern USB one, this article will show you how to integrate these input devices with your ESP32, using the versatile and powerful Rust programming language. The first part of the article describes Rust no_std solution. Rust std for USB solution is at the end of the article.

Preparing the ESP32-C3

First, let’s tackle the wiring for the ESP32-C3. Detailed in bjoernQ’s GitHub repository, “ps2keyboard-esp32c3”, this setup requires three resistors and is perfectly illustrated in the “Circuit” section. For a reliable and efficient connection, consider the wire-wrap technique recommended by Andreas Spiess in his enlightening video, “#243 Better than Dupont Wires: Wire Wrapping for our Projects”.

For the PS/2 connector, refer to the detailed pinout available in the Wikipedia article, “PS/2 port”, particularly the pinout diagram for the female connector (PS/2 port pinout diagram). Please, keep in mind that the pinout is for the female connector from the front side. For the male connector or the back side of the female connector you need to flip the image.

For USB keyboart you’ll need a USB keyboards support a COMBO PS/2 support (still available in many USB keyboards, allowing them to function as PS/2 devices). You can connect these USB keyboards directly by following the schematics from the Instructables project, “USB to PS/2 Convertor” with “ps2keyboard-esp32c3 schematics”,. Specifically, use this wiring diagram (USB to PS/2 wiring diagram) to integrate into the ESP32-C3 setup. No code changes are needed for this part.

Flashing the ESP32-C3

After setting up the hardware, it’s time to program the ESP32-C3. The software part is handled using the pc-keyboard crate, developed by thejpster’s. Flash your ESP32-C3 with the cargo espflash --release command, and watch as keyboard inputs appear on your console.

Interesting Project Ideas

Now that you have your keyboard connected to the ESP32. Here are some project ideas to get you started:

  • Smart Home Controller: Use the keyboard to control lights, temperature, or other IoT devices in your home.
  • Custom Game Controller: Create a gaming experience by mapping keyboard keys to game controls.
  • Educational Tool: Teach programming or robotics, using the keyboard as an input device for experiments and projects.
  • Artistic Installations: Incorporate the keyboard into interactive art projects or music synthesizers.

Rust std + ESP-IDF with USB support

The text above describes Rust no_std solution. If you’re using Rust std on top of ESP-IDF, you can use direct USB support on ESP32-S3 with USB HID for mouse and keyboard Board Support Package (BSP).

24. July 2023

ESP32 How to merge firmware into signle binary

Application built on top of ESP-IDF can be merged into single binary using following commands:

cd build
esptool.py --chip ESP32 merge_bin -o merged-flash.bin @flash_args

The esptool will output following command and perform the conversion:

esptool.py --chip ESP32 merge_bin -o merged-flash.bin --flash_mode dio --flash_freq 80m --flash_size 16MB 0x0 bootloader/bootloader.bin 0x10000 wasmachine.bin 0x8000 partition_table/partition-table.bin 0xd000 ota_data_initial.bin
esptool.py v3.3.2
Wrote 0x2ad4d0 bytes to file merged-flash.bin, ready to flash to offset 0x0

The output is stored in build/merged-flash.bin.

2. May 2023

How to build single flashable binary for ESP32 with esptool.py

Build of ESP-IDF project produces several files, like bootloader, application binary or partition table.

Having several files makes it harder to ship the application outside of build computer.

Solution to the problem is merging binaries into single flashable file.

Build your project with idf.py as always:

cd project
idf.py build

Merge binaries into single file. At the end of build process the tool will display command for flashing. This can be used to compose command like this:

esptool.py --chip ESP32 merge_bin -o merged-flash.bin --flash_mode dio ^
  --flash_size ^
  2MB 0x0 build\bootloader\bootloader.bin ^
  0x8000 build\partition_table\partition-table.bin ^
  0x10000 build\my_app.bin

Luckily there is a simpler way, because all those arguments are stored in build/flash_args file:

Example for Bash

(cd build; esptool.py --chip esp32 merge_bin -o merged-flash.bin @flash_args)

Example for PowerShell

cd build
esptool.py --chip esp32 merge_bin -o merged-flash.bin "@flash_args"

17. April 2023

Matter: chip-tool pairing ble-wifi failing with Abort trap: 6 on macOS

chip-tool can be used for commissioning ESP32 with Matter.

macOS users may face the following error with iTerm:

chip-tool interactive start

pairing ble-wifi 0x7283 SSID PASS PIN 3840
..[FP] Validating NOC chain
..[FP] NOC chain validation successful
..[FP] Added new fabric at index: 0x1
...
Abort trap: 6

It’s necessary to grant access to Bluetooth to apps launched from iTerm.

Click System SettingsPrivacy & Security. Select Bluetooth. Click +, Select Applications, and find iTerm.

Close and open iTerm. The problem with “trap 6” should be gone.

Just do not forget to install Bluetooth Central Matter Client Developer mode profile. Otherwise the provisioning will fail with UUID problem.

14. April 2023

Setting up esp-matter development environtment on openSUSE

When setting up development environment for esp-matter on OpenSUSE you may encouter following error:

./install.sh
...
Error
uilding wheels for collected packages: gevent
  Building wheel for gevent (pyproject.toml): started
  error: subprocess-exited-with-error
  
  × Building wheel for gevent (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  Building wheel for gevent (pyproject.toml): finished with status 'error'
  ERROR: Failed building wheel for gevent
Failed to build gevent
ERROR: Could not build wheels for gevent, which is required to install pyproject.toml-based projects
['esp-matter/connectedhomeip/connectedhomeip/.environment/pigweed-venv/bin/python', '-m', 'pip', 'install', '--log', 'esp-matter/connectedhomeip/connectedhomeip/.environment/pigweed-venv/pip-requirements.log', '--requirement=esp-matter/connectedhomeip/connectedhomeip/scripts/setup/requirements.txt', '--constraint=esp-matter/connectedhomeip/connectedhomeip/scripts/setup/constraints.txt'] {'stdout': <_io.TextIOWrapper name=3 mode='w+' encoding='UTF-8'>, 'stderr': -2}

Problem is caused by gdbgui dependency in ESP-IDF. The dependency is not necessary to build the project.

Edit file esp-matter/connectedhomeip/connectedhomeip/scripts/setup/requirements.esp32.txt .

Disable the line with gdbgui:

#gdbgui==0.13.2.0 ; platform_machine != 'aarch64' and sys_platform == 'linux'

Continue with `./install.sh` script.