Need help to optimize code

Board - ESP32 UNO D1

Controlling Dc motor via Alexa . Browser(wifi) , Bluetooth and using door sensors to stop at start/end

Pins -

Motor controller
D16-IM1
D17-IN2

Door sensors

Sensor 1 - 12
Sensor 2 - 25

Libraries used - AsyncTCP .fauxmoESP,BluetoothSerial,WiFi

#include <Arduino.h>
#include <WiFi.h>
#include "BluetoothSerial.h"
#define RELAY_PIN_1 16
#define RELAY_PIN_2 17
const int sensor1 = 12;    // set the sensor1 pin mode
const int sensor2 = 25;    // set the sensor2 pin mode
WiFiServer server(80);
int state;
#include "fauxmoESP.h"

#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "WIFI_SSID"
#define WIFI_PASS "WIFI_PASS"

#define LAMP_1 "open door"
#define LAMP_2 "close door"

fauxmoESP fauxmo;

// Wi-Fi Connection
void wifiSetup() {

  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();
  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
    server.begin();
}
BluetoothSerial SerialBT;
void setup() 
{
    SerialBT.begin("ESP32test"); //Bluetooth device name
    pinMode(sensor1, INPUT_PULLUP); // set the sensor1
    pinMode(sensor2, INPUT_PULLUP); // set the sensor2
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();

  wifiSetup();
  // LED
  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, LOW);
  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, LOW);
  
  fauxmo.enable(true);

  // Add virtual devices
  fauxmo.addDevice(LAMP_1);
  fauxmo.addDevice(LAMP_2);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) {
    Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
    if ( (strcmp(device_name, LAMP_1) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 1 switched by Alexa");
      //digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
      if (state) {
        digitalWrite(RELAY_PIN_1, HIGH);
      } else {
        digitalWrite(RELAY_PIN_1, LOW);
      }
    }
    if ( (strcmp(device_name, LAMP_2) == 0) ) {
      Serial.println("RELAY 2 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_2, HIGH);
      } else {
        digitalWrite(RELAY_PIN_2, LOW);
      }
    }
  });

}

void loop() {
  fauxmo.handle();

    state = !digitalRead(sensor2);
  
    if (state == HIGH){
          digitalWrite(RELAY_PIN_1, HIGH);
        } 
  state = !digitalRead(sensor1);
  
    if (state == HIGH){
          digitalWrite(RELAY_PIN_2, HIGH);
        } 
 WiFiClient client = server.available();   // listen for incoming clients


  if (client) {                             
   Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) { 
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n') {
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H1\">here</a> to open-16
");
            client.print("Click <a href=\"/L1\">here</a> to turn motor off.
");
            client.print("Click <a href=\"/H2\">here</a> to close-17
");
            client.print("Click <a href=\"/L2\">here</a> to turn motor off.
");

            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H1")) {
          digitalWrite(RELAY_PIN_1, LOW); 
        }
        if (currentLine.endsWith("GET /L1")) {
          digitalWrite(RELAY_PIN_1, HIGH); 
        }
        if (currentLine.endsWith("GET /H2")) {
          digitalWrite(RELAY_PIN_2, LOW); 
        }
        if (currentLine.endsWith("GET /L2")) {
          digitalWrite(RELAY_PIN_2, HIGH);
        }        
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");

    if (SerialBT.available()) {
    byte data;
    data = SerialBT.read();
    if (data == '1') //press '1' turns the LED on
    {
      digitalWrite(RELAY_PIN_1, HIGH);
    }
    else if (data == '2') //press '2' turns the LED off
    {
      digitalWrite(RELAY_PIN_1, LOW);
    }
     if (data == '11') //press '1' turns the LED on
    {
      digitalWrite(RELAY_PIN_2, HIGH);
    }
    else if (data == '22') //press '2' turns the LED off
    {
      digitalWrite(RELAY_PIN_2, LOW);
    }
  }
}
}

code became too big with libraries attached.

How much do you have to save?
ROM or RAM?

If you're short of RAM, you could make better use of F() macro.

I am not sure
Newbie here :smiley:


Arduino: 1.8.5 (Windows 7), Board: "ESP32 Dev Module, Default, QIO, 80MHz, 4MB (32Mb), 921600, None"

Archiving built core (caching) in: C:\U...\core_espressif_esp32_esp32_PartitionScheme_default,FlashMode_qio,FlashFreq_80,FlashSize_4M,UploadSpeed_921600,DebugLevel_none_f7764ada9df3fcdb4d00ee644c78b17f.a
Sketch uses 1401142 bytes (106%) of program storage space. Maximum is 1310720 bytes.

Global variables use 66284 bytes (22%) of dynamic memory, leaving 228628 bytes for local variables. Maximum is 294912 bytes.
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
Error compiling for board ESP32 Dev Module.