Temperature + Humidity with OLED



  • 0_1537124978902_temphumoled-01.jpg

    Hi everyone.

    I've introduced a small 0.96" OLED display (based on the SSD1306) on the temperature and humidity node, so you can read directly the values as you enter the house compartment (as you can see above).
    Since i dont know the reliability of this kind of displays and they are a bit bright (of if they have some kind of burn-in issue), im asking you guys for some ideas to turn it off and back on, maybe taking into account info got from the controller (and even display other info).

    Im using the u8g2 display library wich has the instruction "setContrast", but these displays dont support contrast control. It seems the only way is to turn it off with the instruction "setPowerSave".

    I was thinking of displaying the current date also and maybe turn it off at night. For this i was thinking of using the "requestTime()" command…

    What do you guys think?

    Here is my code so you can take a look and use on your own projects:

    // Enable debug prints
    //#define MY_DEBUG
    
    #define MY_RADIO_RFM69
    #define MY_IS_RFM69HW
    #define MY_RFM69_NEW_DRIVER
    #define MY_RFM69_FREQUENCY RFM69_433MHZ
    
    #define MY_NODE_ID 15
    
    // #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>
    
    #include <U8g2lib.h>  
    #include <Wire.h>    
    
    U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, A5, A4);   // U8G2 Constructor (A5 - Clock SCL ; A4 - Data SDA)
    
    // Set this to the pin you connected the DHT's data pin to
    #define DHT_DATA_PIN 3
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures.
    // In Celsius degrees (as measured by the device)
    #define SENSOR_TEMP_OFFSET 0
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 60000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    void before(){
    
       u8g2.begin();
    
       u8g2.firstPage();
       do {
         u8g2.setFont(u8g2_font_helvR14_tf);
         u8g2.drawStr(38,15,"OLED");
         u8g2.drawStr(15,35,"Temp+Hum");
         u8g2.drawStr(46,60,"v1.2");
       } while ( u8g2.nextPage() );
      
      delay(3000);
    
      u8g2.clear();
     
       u8g2.firstPage();
        do {
         u8g2.drawStr(3, 32, "Connecting...");
       } while ( u8g2.nextPage() );
    }
    
    void presentation()  
    { 
    
      // Send the sketch version information to the gateway
      sendSketchInfo("TEMPHUM_OLED", "1.2");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      metric = getControllerConfig().isMetric;
    }
    
    void setup()
    {
      
      u8g2.clear();
      
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
    
        u8g2.firstPage();
        do {
            u8g2.setFont(u8g2_font_haxrcorp4089_tr); // 7 PIXEL HIGHT
            u8g2.drawStr(1,12,"WARNING: UPDATE_INTERVAL");
           u8g2.drawStr(1,24,"is smaller than supported by");
            u8g2.drawStr(1,36,"the sensor!");
           } while ( u8g2.nextPage() );
        delay(4000);
    
      }
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      sleep(dht.getMinimumSamplingPeriod());
    }
    
    void loop()      
    {  
    
     while (transportCheckUplink() == false){
    
      u8g2.firstPage();
      do {
        u8g2.setFont(u8g2_font_helvR14_tf); // 14 px height
        u8g2.drawStr(3, 32, "Disconnected!");
        } while ( u8g2.nextPage() );
     }
    
        // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
    
      // Get temperature from DHT library
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
    
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp = temperature;
    
        // apply the offset before converting to something different than Celsius degrees
        temperature += SENSOR_TEMP_OFFSET;
    
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp++;
      }
    
      // Get humidity from DHT library
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
    
        #ifdef MY_DEBUG
        Serial.print("H: ");
        Serial.println(humidity);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum++;
      }
    
      u8g2.firstPage();
      do {
       u8g2.setFont(u8g2_font_fub30_tn);
      u8g2.setCursor(2, 35);
      u8g2.print(temperature, 1);
      u8g2.setFont(u8g2_font_inb16_mf);
      u8g2.drawGlyph(88, 35, 0x00b0); // degree
      u8g2.drawStr(100, 35, "C");
      u8g2.setCursor(70, 60);
      u8g2.print(humidity, 0);
      u8g2.drawStr(100, 60, "%");
      u8g2.setFont(u8g2_font_open_iconic_thing_2x_t); // 16 pix height
      u8g2.drawGlyph(45, 60, 0x0048); // drop
     } while ( u8g2.nextPage() );
      
      // Sleep for a while to save energy
      wait(UPDATE_INTERVAL);  // POWERED NODE
    }
    

    Be advised that this code is almost near the memory capacity of the Nano 328P... 😛


  • Mod

    @titvs I don’t think burn-in is a problem with these displays, but an elegant solution vould be to turn it on/off based on a PIR/motion sensor. Maybe you already have such a sensor in the same room?



  • @mfalkvidd hmm, that’s a nice suggestion... I don’t have motion sensors where they are but maybe I can integrate it on the actual node...
    Will give it some thought.

    Thanks! 😄


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 10
  • 5
  • 6
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts