Bluetooth Resources

Hi All,

I have a project I’m working on using a ESP32 Dev Module (mini-1). This project uses 74% of the resources.

“Sketch uses 976874 bytes (74%) of program storage space. Maximum is 1310720 bytes.
Global variables use 32564 bytes (9%) of dynamic memory, leaving 295116 bytes for local variables. Maximum is 327680 bytes.”

When I remove the BT capability the resources got down to 45%. These are my included libraries
#include “esp_bt_main.h”
#include “esp_bt_device.h”
#include “BluetoothSerial.h”

Is there a more efficient way to achieve this, I want to be around 55-65%, a 10-20% reduction.

What other info do you need from me so as to be able to help?

CODE BELOW (is there a better way to paste into a post):

// this header is needed for Bluetooth Serial -> works ONLY on ESP32

#include <stdio.h>

//-#include “esp_bt_main.h”

#include “esp_bt_device.h”

#include “BluetoothSerial.h”

#include <SPI.h>

//-#include <Wire.h>

//-#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// “HSPI” Declaration for SSD1306 display connected using software SPI (default case):

// 2.5" OLED connections on prototype PCB

#define OLED_MOSI 13 //GPIO13, PIN 19 to Disp Conn Pin 5 (DI)

#define OLED_CLK 14 //GPIO14, PIN 15 to Disp Conn Pin 4 (CLK)

#define OLED_DC 2 //GPIO02, PIN 21 to Disp Conn Pin 16(DC)

#define OLED_CS 15 //GPIO15, PIN 20 to Disp Conn Pin 12(CS)

#define OLED_RESET 4 //GPIO04, PIN 23 to Disp Conn Pin 14(RES)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,

OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

const int Analog_channel_pin= 36; //adc1_ch0

const int button1 = 21; // SW1 GPIO21

const int button2 = 23; // SW2 GPIO23

const int button3 = 18; // SW3 GPIO18

const int button4 = 5; // SW4 GPIO05

#define AVG 10

#define UPDATE 2000

#define LED1 32

#define LED2 33

#define LED3 25

#define HBEAT 4000

int ADC_VALUE = 0;

float voltage_value = 0;

char dewpoint[8];

int SW1_state,SW2_state,SW3_state,SW4_state;

int curr_sw;

int led_no=0;

char dev_addr[3]; //last two of MAC for name

char btName[20] = “XDTII_”;

// init Class:

BluetoothSerial ESP_BT;

// Parameters for Bluetooth interface

int units = 0; //0=C, 1=F, 2=ppmV

void printDeviceAddress() {

char str[3];

const uint8_t* point = esp_bt_dev_get_address();

for (int i = 0; i < 6; i++) {

 sprintf(str, "%02X", (int)point[i]);

Serial.print(str);

if (i < 5){

  Serial.print(":");

}

}

dev_addr[0]=str[0];

dev_addr[1]=str[1];

dev_addr[2]=str[2];

Serial.println();

Serial.println(dev_addr);

}

int read_switches(void)

{

int SW_value;

SW1_state = digitalRead(button1);

SW2_state = digitalRead(button2);

SW3_state = digitalRead(button3);

SW4_state = digitalRead(button4);

SW1_state = !SW1_state; SW2_state = !SW2_state; SW3_state = !SW3_state; SW4_state = !SW4_state;

SW_value = SW4_state222 + SW3_state22 + SW2_state2 + SW1_state;

return SW_value;

}

void init_display(void)

{

display.setRotation(2); //0=0, 1=90, 2=180, 3=270

display.clearDisplay();

display.setTextSize(1); // Normal 1:1 pixel scale

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor(12,0); // Start at top-left corner

display.println(F(“Cosa Xentaur XDTII”));

//display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw ‘inverse’ text

//display.println(3.141592);

display.println("");

display.setTextSize(1); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE);

// display.print(F(“0x”));

display.setCursor(6,10); // Start at top-left corner

display.println(F(“Code Rev p1 05/22/23”));

mainMenu();

display.display();

// delay(2000);

}

void setup()

{

Serial.begin(19200);

pinMode(button1, INPUT);

pinMode(button1, INPUT_PULLUP);

pinMode(button2, INPUT);

pinMode(button2, INPUT_PULLUP);

pinMode(button3, INPUT);

pinMode(button3, INPUT_PULLUP);

pinMode(button4, INPUT);

pinMode(button4, INPUT_PULLUP);

pinMode(LED1, OUTPUT);

digitalWrite(LED1, LOW);

pinMode(LED2, OUTPUT);

digitalWrite(LED2, LOW);

pinMode(LED3, OUTPUT);

digitalWrite(LED3, LOW);

led_no=0;

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally

if(!display.begin(SSD1306_SWITCHCAPVCC)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Don't proceed, loop forever

}

init_display();

// btName = “XDTII_”;

ESP_BT.begin(“BT_IF”); //Name of your Bluetooth interface -> will show up on your phone

printDeviceAddress();

strcat(btName,dev_addr);

// ESP_BT.begin(“ESP32_Interface”); //Name of your Bluetooth interface -> will show up on your phone

ESP_BT.begin(btName); //Name of your Bluetooth interface -> will show up on your phone

// printDeviceAddress();

attachInterrupt(digitalPinToInterrupt(21),sw_int,FALLING); //SW1

attachInterrupt(digitalPinToInterrupt(23),sw_int,FALLING); //SW2

attachInterrupt(digitalPinToInterrupt(18),sw_int,FALLING); //SW3

attachInterrupt(digitalPinToInterrupt(5) ,sw_int,FALLING); //SW4

}

void loop()

{

static int hb_cnt=0;

static bool hb_sts=0;

int bt_sw=0;

// -------------------- Receive Bluetooth signal ----------------------

/*

if (ESP_BT.available())

{

units = ESP_BT.read(); //Read what we receive

Serial.println(units);

}

*/

if (ESP_BT.available())

{

bt_sw = ESP_BT.read(); //Read what we receive

Serial.println(bt_sw);

if (bt_sw)  //non-zero

{

  display.clearDisplay();

  display.setTextSize(1);             // Draw 2X-scale text



  display.setCursor(0,40);             // Start at top-left corner

  display.println(F("SW = "));  



  display.setCursor(30,40);             // Start at top-left corner

  display.println(bt_sw);  

  switch(bt_sw)

  {

    case 1:

      Menu1_1();

    break;

    case 2:

      Menu1_2();

    break;

    case 3:

      Menu1_3();

    break;

    case 4:

      Menu1_4();

    break;

    default:

      mainMenu();

  break;

  }

  bt_sw = 0;  //reset

}

}

process_switches();

process_dewpoint();

if(hb_cnt++ > HBEAT)

{

hb_cnt=0;

hb_sts = !hb_sts;

switch(led_no)

{

  case 0:

      digitalWrite(LED1, hb_sts);

      digitalWrite(LED2, 0);

      digitalWrite(LED3, 0);

  break;

  case 1:

      digitalWrite(LED2, hb_sts);

      digitalWrite(LED1, 0);

      digitalWrite(LED3, 0);

  break;

  case 2:

      digitalWrite(LED3, hb_sts);

      digitalWrite(LED2, 0);

      digitalWrite(LED1, 0);

      led_no=-1;

  break;

}

led_no++;

}

}

void process_dewpoint(void)

{

char disp_buf[8];

static int avg_cnt, dsp_cnt=0;

voltage_value += ReadVoltage(Analog_channel_pin);

if(avg_cnt++ > (AVG-2))

{

voltage_value /= AVG;

// ESP_BT.println(dewpoint); //send data

//Put numeric dewpoint on the display

if(dsp_cnt++ > (UPDATE))

{

//**************************Clear numeric area *****************

//display.drawRect(60, 45, 67, 21, WHITE);  //test size

display.fillRect(60, 45, 67, 21, BLACK);//clear

display.display();

//*****************************************************************

display.setTextSize(2);            

display.setCursor(0,50);              // set cursor pos textsize(1)

//display.print("Dewpoint = ");

display.print("DewPt=");

display.setTextSize(2);               // Draw 2X-scale text

display.setCursor(60,50);             // set cursor pos textsize(1)

//display.println(dewpoint);

sprintf(disp_buf,"%5s",dewpoint);

display.print(disp_buf);

display.display();

ESP_BT.println(dewpoint);  //send data

//*****************************

switch(units)

{

  case 0:

    voltage_value *= 3;

  break;

  case 1:

    voltage_value *= 6;

  break;

  case 2:

    voltage_value *= 9;

  break;

}

dsp_cnt=0;

} //display loop

//sprintf(dewpoint,"%3.3f",voltage_value);

dtostrf(voltage_value,3,2,dewpoint);

avg_cnt=0;

voltage_value = 0;

} //voltage loop

}

double ReadVoltage(byte pin)

{

double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095

// Serial.print("ADC VALUE = ");

// Serial.println((int)reading);

if(reading < 1 || reading > 4095) return 0;

return -9e-15 * pow(reading,4) + 5e-11 * pow(reading,3)- 1e-7 * pow(reading,2) + 0.0009 * reading + 0.1064;

//  return - 2e-11 * pow(reading,3) + 1e-7 * pow(reading,2) + 0.0007 * reading + 0.1336;

} // Added an improved polynomial, use either, comment out as required

void sw_int(void)

{

//int curr_sw;

curr_sw = read_switches();

}

void process_switches(void)

{

if (curr_sw) //non-zero

{

display.clearDisplay();

display.setTextSize(1);             // Draw 2X-scale text



display.setCursor(0,40);             // Start at top-left corner

display.println(F("SW = "));  



display.setCursor(30,40);             // Start at top-left corner

display.println(curr_sw);  

switch(curr_sw)

{

  case 1:

    Menu1_1();

  break;

  case 2:

    Menu1_2();

  break;

  case 4:

    Menu1_3();

  break;

  case 8:

    Menu1_4();

  break;

  default:

  break;

}

curr_sw = 0;  //reset

}

}

void mainMenu(void)

{

display.clearDisplay();

display.setTextSize(1); // Draw 2X-scale text

display.setCursor(0,0); // Start at top-left corner

display.print(“SW1 Opt”);

display.setCursor(86,0); // Start at top-left corner

display.print(“Set SW2”);

display.setCursor(0,20); // Start at top-left corner

display.print(“SW3 Units”);

display.setCursor(74,20); // Start at top-left corner

display.print(“Calib SW4”);

display.display();

}

void Menu1_1(void)

{

Serial.print(“Menu Item 1:1”); //debug

//digitalWrite(LED1, LOW);

display.clearDisplay();

display.setTextSize(1); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor((SCREEN_WIDTH-60)/2,SCREEN_HEIGHT/2); // Start at top-left corner

display.print(“Menu 1:1”);

display.display();

delay(1000);

mainMenu();

}

void Menu1_2(void)

{

Serial.print(“Menu Item 1:2”); //debug

//digitalWrite(LED1, HIGH);

display.clearDisplay();

display.setTextSize(1); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor((SCREEN_WIDTH-60)/2,SCREEN_HEIGHT/2); // Start at top-left corner

display.print(“Menu 1:2”);

display.display();

delay(1000);

mainMenu();

}

void Menu1_3(void)

{

Serial.println(“Menu Item 1:3”); //debug

if(units<2)

units++;

else

units=0;

Serial.println(units);

display.clearDisplay();

display.setTextSize(1); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor((SCREEN_WIDTH-60)/2,SCREEN_HEIGHT/2); // Start at top-left corner

display.print(“Menu 1:3”);

display.display();

delay(1000);

mainMenu();

}

void Menu1_4(void)

{

Serial.print(“Menu Item 1:4”); //debug

display.clearDisplay();

display.setTextSize(1); // Draw 2X-scale text

display.setTextColor(SSD1306_WHITE); // Draw white text

display.setCursor((SCREEN_WIDTH-60)/2,SCREEN_HEIGHT/2); // Start at top-left corner

display.print(“Menu 1:4”);

display.display();

delay(1000);

mainMenu();

}