Sketch program storage space

#include <SPI.h>
#include <Wire.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <MQ135.h>
#include <Adafruit_SSD1306.h>


float conPM25;
unsigned long durationPM25;
unsigned long starttime;
unsigned long sampletime_ms = 15000;
unsigned long lowpulseoccupancyPM25 = 0;


uint8_t co2Value = 0;
uint8_t CO2Pin = A0;
uint8_t coValue = 0;
uint8_t analogPinC0 = A1;
uint8_t PM25PIN = 7;

MQ135 co2Sensor = MQ135(CO2Pin);


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using I2C
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

const char broker[] = "test.mosquitto.org";


void reconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect("arduinoClient")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      mqttClient.publish("outTopic","hello world");
      // ... and resubscribe
//      mqttClient.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

   while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(analogPinC0, INPUT);
  pinMode(PM25PIN,INPUT);
    // initialize the OLED object
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  delay(2000);
  display.clearDisplay();

    Serial.println("MQTT Arduino: ");

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // no point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
  }    
  // print your local IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  delay(1500);
  
  mqttClient.setServer( broker, 1883);


}

void loop() {
  // put your main code here, to run repeatedly:

   if (!mqttClient.connected()) {
    reconnect();
  }
  // call regularly to allow the library to send MQTT keep alive which
  // avoids being disconnected by the broker

  mqttClient.loop();


}


This Sketch uses 28670 bytes (88%) of program storage space. Maximum is 32256 bytes.
Global variables use 1274 bytes (62%) of dynamic memory, leaving 774 bytes for local variables. Maximum is 2048 bytes.

When I upload it with the crux of the program (the code in the loop function) it excceds to 113% program storage space. But the maximum space being used is here . Need help to optimize this.

The compiler/linker doesn't know about the memory used by the screen, or any other memory dynamically allocated.

Are you really to hope run all of above in Uno/Nano board?

Even if you manage to compile it, it probably won't work. As mentioned earlier, the numbers that the IDE gives when compiling - take into account only static memory. Dynamically allocated arrays are not taken into account, and when working with a screens, they can be up to half the size of memory.
You need to transfer this project to a board with network support - to esp32 or raspberry pico W

well you could start to
a) use the F-Makro
b) shorten your text

Serial.print("Attempting MQTT connection...");

to

Serial.print(F("MQTT conn"));

or

Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");

to

Serial.println(F("Eth missing"));

F() macro transfer memroy from sram 2kb to flash32kb. Flash is what I am short of.

Tried shortening serial print text , not helping. Very minimal difference, 113% means I need to save upwards of 3kb memory.

not exactly true.
The F-Makro avoids that text is copied to SRAM.
It will need program space in any case.

Search for a ASCII only OLED library,
or replace the OLED with a character LCD
or even leave away I2C and use something with SPI.

Hi noiasca, First of all thanks for responding.

Looks like I can't use F() because it will need program space.

I tried to run the code with individual libraries, looks like the oled and ethernet libraries use the most space. Is there any better way to find out the size of the libraries?

Also I have to use I2C OLED as that is what I have.

I have to make it work on UNO. I am not using SPI so that can be removed but when I remove SPI it's not showing any difference in memory. Also wire.h is needed for oled.
Ethernet for the shield. PubSubClient for mqtt. mq135 can be removed(but doesn't make much difference in memroy). And the most memory consuming library for oled which is the adafruit.
I was also using
#include <Adafruit_GFX.h>
but later removed it because I am only displaying text.

Okay when I use the function
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); along with display.begin

it increases memory usage by 30%

The Ethernet Shield will include SPI, therefore it will be included anyway.

search for the
SSD1306Ascii
version=1.3.3 from Bill Greiman

@gmanan If you follow the advice given you would have a better chance of your project fitting within the resources available.

The character strings are already stored in program space. Using the F() macro won't store them twice. If you are still running out of program space, then shorten your text messages as already suggested.

Follow the advice from @noiasca in post #11 and replace the Adafruit SSD1306 library with the ASCII text version. Here's the github page for the library if you can't find it via library manager:

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