ESP32 + SSD1306 + DHT22 - how output temp & humidity on Display ???

35p32fan
Posts: 12
Joined: Wed Oct 11, 2023 1:12 pm

ESP32 + SSD1306 + DHT22 - how output temp & humidity on Display ???

Postby 35p32fan » Sun Dec 17, 2023 7:58 am

Hi,
found some code and try to combine this Codes to one.

Here you can see an play with the Code: https://wokwi.com/projects/384345803812527105

But had no success to display the: temprature & humidity on the display !

Could someone be so kind an help me with this ?

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: ESP32 + SSD1306 + DHT22 - how output temp & humidity on Display ???

Postby liaifat85 » Wed Dec 20, 2023 12:58 pm

I think this tutorial is much easier: https://randomnerdtutorials.com/esp32-e ... d-display/
I also recommend cross-checking the DHT22 with an Arduino separately. You can follow this code here for the testing purposes:
https://www.theengineeringprojects.com/ ... dht22.html

35p32fan
Posts: 12
Joined: Wed Oct 11, 2023 1:12 pm

Re: ESP32 + SSD1306 + DHT22 - how output temp & humidity on Display ???

Postby 35p32fan » Thu Dec 21, 2023 7:40 am

@liaifat85

thank you but I need a Rust solution not Arduino

35p32fan
Posts: 12
Joined: Wed Oct 11, 2023 1:12 pm

Re: ESP32 + SSD1306 + DHT22 - how output temp & humidity on Display ???

Postby 35p32fan » Sat Jan 06, 2024 4:18 pm

Code: Select all

cargo generate --git https://github.com/esp-rs/esp-idf-template cargo
cd esp32-ssd1306-dht22
cargo add esp-idf-svc 
cargo add ssd1306
cargo add esp-idf-hal
cargo add embedded-svc
cargo add embedded-graphics
cargo add anyhow
cargo add esp-idf-sys
cargo add epd-waveshare
cargo add dht_sensor
. ~/export-esp.sh
cargo check
cargo run
main.rs

Code: Select all

//SSD1306:
//SDA -> GPIO21
//SCL -> GPIO22

//DHT22:
//DATA -> GPIO18


use esp_idf_hal::i2c::*;
use esp_idf_hal::prelude::*;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_svc::hal::{delay, gpio::PinDriver, peripherals::Peripherals};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use std::thread;
use std::time::Duration;
use dht_sensor::{dht22, DhtReading};


use embedded_graphics::{
    mono_font::{ascii::FONT_6X10, MonoTextStyleBuilder},
    pixelcolor::BinaryColor,
    prelude::*,
    text::{Baseline, Text},
    primitives::{Circle, PrimitiveStyleBuilder}
};

fn main() -> anyhow::Result<()> {
    esp_idf_svc::sys::link_patches();
    let peripherals = Peripherals::take().unwrap();
    let i2c = peripherals.i2c0;
    let sda = peripherals.pins.gpio21;
    let scl = peripherals.pins.gpio22;

    let mut pin = PinDriver::input_output_od(peripherals.pins.gpio18).unwrap();
    pin.set_high().unwrap();
    // The DHT datasheet suggests waiting 1 second
    thread::sleep(Duration::from_millis(1000));

    let mut d = delay::Ets;
    let mut iterations = 1500;


    let config = I2cConfig::new().baudrate(100.kHz().into());
    let i2c = I2cDriver::new(i2c, sda, scl, &config)?;

    let interface = I2CDisplayInterface::new(i2c);
    let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
        .into_buffered_graphics_mode();
    display.init().unwrap();

    let text_style = MonoTextStyleBuilder::new()
        .font(&FONT_6X10)
        .text_color(BinaryColor::On)
        .build();

    let off = PrimitiveStyleBuilder::new()
        .stroke_width(1)
        .stroke_color(BinaryColor::Off)
        .build();

    let on = PrimitiveStyleBuilder::new()
        .stroke_width(1)
        .stroke_color(BinaryColor::On)
        .build();

    let mut i = 0;
    let mut dir = 1;

    Text::with_baseline("Hello world!", Point::new(30, 0), text_style, Baseline::Top)
        .draw(&mut display)
        .unwrap();

    // Text::with_baseline("with Rust!!", Point::new(30, 16), text_style, Baseline::Top)
    //     .draw(&mut display)
    //     .unwrap();

    loop {
      while iterations > 0 {
        thread::sleep(Duration::from_secs(3));
        iterations -= 1;

        let temp_str = format!("    ");
        let hum_str = format!("     ");
        
        Text::with_baseline(&temp_str, Point::new(30, 16), text_style, Baseline::Top)
        .draw(&mut display)
        .unwrap();
        display.init().unwrap();
        display.flush().unwrap();

        Text::with_baseline(&hum_str, Point::new(30, 28), text_style, Baseline::Top)
        .draw(&mut display)
        .unwrap();


        if let Ok(r) = dht22::Reading::read(&mut d, &mut pin) {
          println!("Temperature: {}\tHumidity: {}",
                    r.temperature, r.relative_humidity
                  );

          let temp_str = format!("temp: {}",  r.temperature);
          Text::with_baseline(&temp_str, Point::new(30, 16), text_style, Baseline::Top)
          .draw(&mut display)
          .unwrap();

          let hum_str = format!("hum: {}",  r.relative_humidity);
          Text::with_baseline(&hum_str, Point::new(30, 28), text_style, Baseline::Top)
          .draw(&mut display)
          .unwrap();
        } 
      
  

        Circle::new(Point::new(i, 40), 8)
            .into_styled(off)
            .draw(&mut display)
            .unwrap();

        

        if i > 100 { 
            dir = -1; 
        } 

        if i == 0 {
            dir = 1;
        }

        i += dir << 4;

        Circle::new(Point::new(i, 40), 8)
            .into_styled(on)
            .draw(&mut display)
            .unwrap();

        display.flush().unwrap();

        FreeRtos::delay_ms(100);
    }
  }  
}

Who is online

Users browsing this forum: No registered users and 7 guests