How can I shrink my code?

(sorry about the poor english) Hello guys, I was working on a smart greenhouse project that utilize very chars and strings, ntp servers for telling time, bluetooth for app compability etc. I use a WEMOS D1 R32 Esp32. I specifically chose this board because i needed more storage for my projects. But my project is too heavy for my board. The sketch uses 1674221 bytes (127%) of program storage space. The maximum is 1310720 bytes.
And global variables use 60096 bytes (18%) of dynamic memory, leaving 267584 bytes for local variables. The maximum is 327680 bytes. I do not want to buy an SD card reader and do not want to break my code. Can anyone help me? The code is:

`#include <Arduino.h>
#include "DHT.h"
#include <BluetoothSerial.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

const char* ssid = "*******";
const char* password = "**********";

const char* ntpServer = "tr.pool.ntp.org";
#define sulamaPin 26
#define DHTPIN 27    
#define DHTTYPE DHT11
#define veri3169 A0
#define buzzerPin 13
#define soilWet 1699  
#define soilDry 880

int relayOnHour = 18, relayOnMinute = 31;
int relayOffHour = 18, relayOffMinute = 32;
bool relayState = false;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, 10800, 3600);
BluetoothSerial SerialBT;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    SerialBT.begin("AgroBot TR31B");
    pinMode(sulamaPin, OUTPUT);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(500);
    timeClient.begin();
    dht.begin();
}

void loop() {
    delay(1000);
    float h = dht.readHumidity(), t = dht.readTemperature();
    if (isnan(h) || isnan(t)) return;

    int nemPin = analogRead(veri3169);
    SerialBT.printf("Nem: %.1f%% Sıcaklık: %.1f°C Toprak: %d\n", h, t, nemPin);
    digitalWrite(buzzerPin, (nemPin < soilDry) ? HIGH : LOW);

    timeClient.update();
    int currentHour = timeClient.getHours(), currentMinute = timeClient.getMinutes();
    relayState = (currentHour == relayOnHour && currentMinute == relayOnMinute) ? HIGH :
                 (currentHour == relayOffHour && currentMinute == relayOffMinute) ? LOW : relayState;
    digitalWrite(sulamaPin, relayState);

    if (SerialBT.available()) {
        int data = SerialBT.readString().toInt();
        if (data >= 1 && data <= 48) {
            relayOnHour = (data - 1) / 2;
            relayOnMinute = (data % 2) ? 0 : 30;
            relayOffHour = relayOnHour;
            relayOffMinute = relayOnMinute + 1;
        }
    }
}
`

If anyone can help me I'll be so grateful.

Try with Huge App on partition scheme.

3 Likes

You may want to visit this Arduino page, which describes how to reduce your sketch size and memory usage.

You may also want to consider replacing your current board with an ESP32-S3 board that provides a more significant amount of flash. Adafruit has several boards with 8MB of flash. The downside is that they are S3-type boards that support BLE but not Bluetooth Classic. There is a BLE serial library that implements Nordic UART.

I have used the HardwareBLESerial library for one of my projects, which works without issue.

1 Like

@kmin had a great suggestion. I selected large partition (no OTA), and here is the result:

Sketch uses 1679524 bytes (80%) of program storage space. Maximum is 2097152 bytes.
Global variables use 61484 bytes (18%) of dynamic memory, leaving 266196 bytes for local variables. Maximum is 327680 bytes.

Still, would be interesting to know what take up that much memory. 80% of 4 MB is crazy, your sketch isn't that big, unless I've overlooked something.

1 Like

Thank you guys very much, I tried out the code and it worked with no issues, also gonna state that I cannot afford another board right now because the econamical state in my country is bad so things get overpriced from our perspective. Still ty very much, appreciate it.

1 Like

Well I have so much char and strings as I stated (as i remember) so it could be that.
My normal code was just over 600 lines long but i shortend the code a little.

So the problem was in the code that you didn't post

It would be interesting to see the full sketch that caused the problem

Well remember to mark the answer that solved the problem as Solution, have a nice night and come again :upside_down_face:

Well, it's 80% of only 2MB. Apparently half the flash is "reserved" for OTA upgrades, or something. (although, even the non-OTA, no FS, partition schemes seem to max out at 3MB. I don't know whether that's a device limitation, or just a lack of menu options...)

2 Likes

Aha, a bit better but still a lot of code. I bet those wireless libs wants much memory..?

You got it. I did not have an real time module so I just used a ntp server for the time. Its not efficient but hey, it works.

It IS the full code.

3 posts were split to a new topic: Bluescreen of death while using IDE: "Stop code: DRIVER_IRQL_NOT_LESS_OR_EQUAL"

Using the "Huge App" partition scheme indeed helps reduce the firmware size. However, if your code still occupies too much space, consider reviewing and optimizing the libraries you're using. Some libraries can be replaced with lighter alternatives or even custom implementations. Also, be cautious with the use of String variables, as they can consume a lot of memory; replacing them with character arrays (char[]) might be beneficial.

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