Table of content

What is this :grey_question:

The rules of the game are explained in my original.

55th Challenge

Challenge

Run hello world on an esp32 using rust.

Solution :white_check_mark:

We need to add some addtional toolchain pieces that are not in a default cargo installation the following command so that esp32 binaries can be built:

Call cargo install -f ldproxy espflash espmonitor

    curl -LO https://github.com/esp-rs/rust-build/releases/download/v1.63.0.2/install-rust-toolchain.sh
    chmod a+x install-rust-toolchain.sh
    ./install-rust-toolchain.sh
    

At the end, I missed it on my first install, the script tells you to add two lines to your .bashrc (or whatever shell you are using). This is important! It will not work otherwise.

Lets start from a template:

    cargo generate \
    --git https://github.com/ivmarkov/rust-esp32-std-demo \
    --name esp
    

The template, looks like this, when running tree -L 3

    ├── Cargo.toml
    ├── LICENSE-APACHE
    ├── LICENSE-MIT
    ├── README.md
    ├── build.rs
    ├── partitions.csv
    ├── qemu.sh
    ├── sdkconfig.defaults
    ├── sdkconfig.defaults.esp32
    ├── sdkconfig.defaults.esp32s2
    ├── src
    │   └── main.rs
    └── ulp
        └── rust-esp32-ulp-blink
    

Next lets go into the dir cd esp and run rustup override set esp so that we just use esp stuff inside this directory. Output will be

info: override toolchain for '../100-rust-snippets/esp' set to 'esp'

Next add wifi pass and ssid using env. variables again, or the compiler will hit you with the following error

   |
100 | const SSID: &str = env!("RUST_ESP32_STD_DEMO_WIFI_SSID");
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: environment variable `RUST_ESP32_STD_DEMO_WIFI_PASS` not defined
   --> src/main.rs:103:20
    |
103 | const PASS: &str = env!("RUST_ESP32_STD_DEMO_WIFI_PASS");
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Next try to build with cargo build --release and it should work.

There are a number of runners as usual. I had success running espflash target/xtensa-esp32-espidf/debug/rust-esp32-std-demo and selecting the right usb port. Warning: I had to hold down the reset button and release to succesfully flash, to get the thing running. Do this when the expflash app is trying to connect. Output should be as follows:

    $ espflash target/xtensa-esp32-espidf/debug/rust-esp32-std-demo
    Detected 2 serial ports. Ports which match a known common dev board are highlighted.

    Serial port: /dev/cu.usbserial-0001
    Connecting...

    Unable to connect, retrying with extra delay...
    Unable to connect, retrying with default delay...
    Unable to connect, retrying with extra delay...

    Chip type:         ESP32 (revision 1)
    Crystal frequency: 40MHz
    Flash size:        4MB
    Features:          WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    MAC address:       24:6f:28:7c:e9:20
    App/part. size:    1606624/4128768 bytes, 38.91%
    [00:00:01] ########################################      16/16      segment 0x1000                                                                                                                                                                                                    [00:00:00] ########################################       1/1       segment 0x8000                                                                                                                                                                                                    [00:01:36] ########################################     936/936     segment 0x10000                                                                                                                                                                                                   
    Flashing has completed!
    

Thats pretty much it, run èspflash serial-monitor, hold reset button if needed and you should get something like the following:

Hello from Rust!
More complex print ["foo", "bar"]
Result: 0, 1

See the repo on github