This repository holds a minimal example for reading the BME280 sensor on an ESP32 using Rust instead of the C++/Arduino.
- Waveshare ESP32-C3
- Bosch BME280 sensor on a breakout-board (can't remember which one exactly)
Unlike the other ESP32 chips, which are built on the Xtensa architecture, the ESP32-C3 is using a RISC-V instruction set. Therefore, the latest Rust nightly chain on llvm can build binaries directly for the ESP32-C3. The specific Waveshare chip listed above does have only 2MB flash, so it does need some customization here and there.
I am not sure what the default I2C pins are on the board I used, so I ended up to manually configure GPIO 8 and 9 for SDA and SCL respectively. According to the docu you could use any GPIO for this. The BME's VIN and GND is connected to the board's 3V3
and GND
respectively.
The example was derived from the esp-idf-template, it holds general build instructions for the various ESP32 chips and how to get the rust nightly chain for RISC-V, or the Xtensa llvm fork for all others.
Specifically for ESP32-C3:
cargo build
(orcargo build --release
...)espflash ./target/risv32imc-esp-espidf/debug/rust-esp32-bme280
(or.../release/...
orcargo flash
)espmonitor /dev/ttyUSB0
(or whatever usb port the board is connected to)
Note, the Waveshare ESP32-C3 board only has 2MB flash on board. The 1.3.0 release of espflash is not yet compatible with this, but the latest master is.
> espmonitor /dev/ttyUSB0
...
I (290) cpu_start: Starting scheduler.
starting...
creating I2C bus...
I (298) gpio: GPIO[8]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (308) gpio: GPIO[9]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
creating BME280...
initializing BME280...
starting measurements...
Relative Humidity = 52.61 %, Temperature = 23.30 °C, Pressure = 974.11 hPa
Relative Humidity = 52.59 %, Temperature = 23.30 °C, Pressure = 974.11 hPa
Relative Humidity = 52.60 %, Temperature = 23.30 °C, Pressure = 974.10 hPa
Relative Humidity = 52.60 %, Temperature = 23.30 °C, Pressure = 974.10 hPa
...
I stumbled over the following issues throughout development:
E (259) spi_flash: Detected size(2048k) smaller than the size in the binary image header(4096k). Probe failed.
- Outdated version of
espflash
is used. Either use a more recent version (or master), or switch toesptool.py
- Outdated version of
initializing BME280... Guru Meditation Error: Core 0 panic'ed (Illegal instruction). Exception was unhandled.
- Double check the SDA and SCL pins, they might need to be swapped in the code.
esp-idf-hal
provides severalDelay
providers, they all seem to work.