Skip to content

Conversation

syrtcevvi
Copy link

Hello and thank you for your awesome crate and the article!

The problem

In my project I use the defmt logger. I faced the problem with printing the SensorError value to the logs:

use esp_println as _;
use defmt::{error, info, Debug2Format};

match dht11.read() {
    Ok(sensor_reading) => info!(
        "DHT 11 Sensor - Temperature: {} °C, humidity: {} %",
        sensor_reading.temperature, sensor_reading.humidity
    ),
    Err(error) => error!("An error occured: {}", error),
}

Workarounds

The SensorError implements the Debug trait, so it's possible to print it to logs, but it brings a bit overhead and it's unnecessary verbose (Debug2Format):

match dht11.read() {
    Ok(sensor_reading) => info!(
        "DHT 11 Sensor - Temperature: {} °C, humidity: {} %",
        sensor_reading.temperature, sensor_reading.humidity
    ),
    Err(error) => Err(error) => error!("An error occured: {}", defmt::Debug2Format(&error)),
}

Changes

  • I've added the optional defmt feature and derived the defmt::Format for the SensorError
  • cargo fmt slightly changed the code in a few places

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant