Enclosure for Temp + Humidity node with OLED display



  • Hi everyone,

    Already posted this over at the troubleshooting section, so im posting here also for whoever is interested. My "compact" temperature + humidity node enclosure with a little window for a 0.96" OLED display.
    Every part is in place using the printed spacers and the back is attached with 2 self drilling screwers.

    https://www.thingiverse.com/thing:3019468

    2_1532525241892_03-back.jpg 1_1532525241892_02-inside.jpg 0_1532525241892_01-front.jpg



  • Looks good ๐Ÿ™‚




  • Plugin Developer

    I hide my sensors in old library books. I try to get the name of the book to be a pun on the sensor inside. So for example, I have an air quality sensor that is inside a murder mystery called "poison".



  • Can you share sketch and connection?



  • @pihome Sure! Its no trade secret. ๐Ÿ˜‰

    Ive used the U8g2lib for the display which is connected at A4 and A5, as defined in the code.
    You should power the display directly to a good power source (3.3 ~ 5.5V) since you are also powering a radio module.

    Its basically the temp+hum sketch where Ive included some lines of code for the display. At boot it displays the message "Connectingโ€ฆ". If the gateway or repeater is offline or out of range, it wont do anything else unless MY_TRANSPORT_WAIT_READY_MS is of a different value than the default (not pretty sure but ive read that the default is to wait forever).

    It will then display temp + hum with a small chain icon at the bottom (connected). If the gateway goes offline it displays "Disconnected!"

    Im not sure if this is bug free, so feel free to debug and add new functionalities. ๐Ÿ™‚

    // Enable debug prints
    //#define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
    
    #define MY_RF24_CHANNEL 1   //////////////
    
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH   //////////////
    
    #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 (PINS: 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
      u8g2.setFont(u8g2_font_open_iconic_www_2x_t); // 16 pix height
      u8g2.drawGlyph(2, 60, 0x004f); // CONNECTION ICON
      
      } while ( u8g2.nextPage() );
    
      
      // Sleep for a while to save energy
      //sleep(UPDATE_INTERVAL); 
      delay(UPDATE_INTERVAL);  // POWERED NODE
    }
    

Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts