DHT11, W5100, OLED display node



  • Hi.
    I have a node composed by Arduino Nano, DHT11 sensor, mini W5100 ethernet shield and OLED 128x32 display.
    I'm using Adafruit GFX library to control the OLED display.
    The problem is the code size that seems to be more than 32K therefore I cannot compile it.
    I tried to squeeze the code but with no success. Any help? Suggestion?

    Here my code

    // ----------------------------------------------------------- Definitions
    #define MY_DEBUG
    
    #define MY_GATEWAY_W5100
    #define MY_IP_ADDRESS 192,168,1,71
    #define MY_PORT 5070
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    #define MY_MAC_ADDRESS 0x00, 0x01, 0x02, 0x03, 0x04, 0x05
    
    // Set this to the pin you connected the DHT's data pin to
    #define DHT_DATA_PIN 3
    
    // If any Permanent Small Offset to the real temperatures in °C
    #define SENSOR_TEMP_OFFSET 0
    
    // OLED SSD1306 
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 32 // OLED display height, in pixels
    
    // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
    #define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
    
    #define COLORBLACK 0x0000 // BLACK
    
    // ----------------------------------------------------------- Libraries
    #include <Ethernet.h>
    //#include <SPI.h>
    #include <MySensors.h>
    #include <DHT.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    
    // ----------------------------------------------------------- Variables
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 5000;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    unsigned long startMillis;  //some global variables available anywhere in the program
    unsigned long currentMillis;
    const unsigned long period = 2000;  //the value is a number of milliseconds
    
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    
    
    bool FirstTime; // to execute an action once only
    
    // ----------------------------------------------------------- Functions
    
    void presentation()  
    {
       sendSketchInfo("Temp/Humidity Soggiorno", "1.0");
       present(CHILD_ID_HUM, S_HUM);
       present(CHILD_ID_TEMP, S_TEMP);
    
       metric = getControllerConfig().isMetric;
    }
    
    
    void setup()
    {
    //---------------------------------------------------------- Serial Begin
    Serial.begin(9600);
    //---------------------------------------------------------- DHT11
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) 
      {
        #ifdef MY_DEBUG
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
        #endif
      }
      
      // 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());
    
    //---------------------------------------------------------- OLED SSD1306
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
      if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) // Address 0x3C for 128x32
      { 
        #ifdef MY_DEBUG
        Serial.println(F("SSD1306 allocation failed"));
        #endif
        for(;;); // Don't proceed, loop forever
      }
    
      // Show initial display buffer contents on the screen the library initializes this with an Adafruit splash screen.
      display.display();
      delay(500); // Pause for 0,5 second
    
      // Clear the buffer
      display.clearDisplay();
    
      display.setTextSize(2); // Draw 2X-scale text
      display.setTextColor(SSD1306_WHITE);
    
      display.setCursor(2, 16);
      display.println(F("Waiting...."));
      display.display();      // Show initial text
      delay(100);
    
      // Variable to make action just the first time
      FirstTime = true;
     
    //---------------------------------------------------------- Timing
      startMillis = millis();  //initial start time
    }
    
    
    void loop()      
    {  
      bool changed; // to note the change of value
      
    //---------------------------------------------------------- Verify Timing for Reading    
      currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
      if (currentMillis - startMillis >= period)  //test whether the period has elapsed
      {
        startMillis = currentMillis;  
        changed = false;
      
    //---------------------------------------------------------- WakeUp Sensor
        dht.readSensor(true);
    
    //---------------------------------------------------------- Temperature    
        int temperature = dht.getTemperature();
        
        if (isnan(temperature)) 
        {
          #ifdef MY_DEBUG
          Serial.println("Failed reading temperature from DHT!");
          #endif
        } 
        else 
        {
          // apply the offset before converting to something different than Celsius degrees
          temperature += SENSOR_TEMP_OFFSET;
           if (!metric) 
           {
              temperature = dht.toFahrenheit(temperature);
           }
           send(msgTemp.set(temperature, 1));
          #ifdef MY_DEBUG
          Serial.print("T: ");
          Serial.println(temperature);
          #endif
          
          changed = true;
          CancellaQuadro();
    
          display.setCursor(64, 0);
          display.print(temperature);
        } 
    //---------------------------------------------------------- Humidity    
        int humidity = dht.getHumidity();
        
        if (isnan(humidity)) 
        {
          #ifdef MY_DEBUG
          Serial.println("Failed reading humidity from DHT");
          #endif
        } 
        else 
        {
           send(msgHum.set(humidity, 1));
          #ifdef MY_DEBUG
          Serial.print("H: ");
          Serial.println(humidity);
          #endif
    
          if (changed == false)
          {
              CancellaQuadro();
          }
          display.setCursor(64, 18);
          display.print(humidity);
        } 
        display.display();      
        delay(100);
      }
    }
    
    void CancellaQuadro(void) {
        if (FirstTime == true)
        {
          display.clearDisplay();
          display.setCursor(2, 0);
          display.print(F("Temp:"));
          display.setCursor(90, 0);
          display.print((char)247); // degree symbol
          display.setCursor(105, 0);
          display.print(F("C"));
          display.setCursor(2, 18);
          display.print(F("Hum :"));
          display.setCursor(90, 18);
          display.print(F("%"));
          display.display();
          delay(100);
          FirstTime = false;
        }else
        {
          display.fillRect(64,0, 25, 16, COLORBLACK);
          display.fillRect(64,16, 25, 32, COLORBLACK);
          display.display();
          delay(1);
        }
    }

  • Mod

    @giangired maybe you can use https://tenbaht.github.io/sduino/api/Mini_SSD1306/ instead?
    https://github.com/stanleyseow/ArduinoTracker-MicroAPRS/tree/master/libraries/SSD1306_text might be an alternative.

    There is not much you can do to optimize your sketch, almost all space is used by the libraries.



  • @mfalkvidd Thanks a lot as usual! Any I cannot download the libraries: can you give me some instructions to load on my Arduino tool?



  • Did you try commenting out #define MY_DEBUG ?
    That might save you a few bytes.


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 3
  • 6
  • 5
  • 1
  • 2

187
Online

9.2k
Users

9.9k
Topics

103.2k
Posts