Problem with u8b2 and DHT Sensor, buffer deletes data

#include <SimpleDHT.h>
#include <dht_nonblocking.h>
#include <U8g2lib.h>
#include <Wire.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

void setup( )
{
  Serial.begin( 9600);
  u8g2.begin();
  
}

static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }
  return( false );
}

void loop( )
{ 
  float temperature;
  float humidity;

  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" ); 
   }
  u8g2.clearBuffer();
  u8g2_prepare();
  u8g2.drawStr(1, 0, "Hello");
  u8g2.sendBuffer();
}

void u8g2_prepare() {
 u8g2.setFont(u8g2_font_6x10_tf); 
 u8g2.setFontRefHeightExtendedText(); 
 u8g2.setDrawColor(1);
 u8g2.setFontPosTop();
 u8g2.setFontDirection(0);
}

void u8g2_display(){
  u8g2.drawStr(0, 0, "This is working");
}

I want to Display the data I get from the sensor (temperature and humidity) on a OCD Screen. This is what i've come up with, but its not working that well. If i dont comment the clear and sendBuffer, the data is deleted and i only get zeros. But if i comment them out, i cant display something on the display. Also i dont understand how i can display the float variables onto the display. Could someone explain to me why the data is being deleted?

This describes it pretty good, but im already using the suggested solution, but it is still not working

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.