Hi, i trying to create code for my project that use Bluetooth and WiFi connection in ESP32. But i facing a problem with insufficient storage. What should i do to reduce the size?
This is my code
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include "BluetoothSerial.h"
#include "Adafruit_Thermal.h"
// Bluetooth
BluetoothSerial SerialBT;
Adafruit_Thermal printer(&SerialBT);
// Web Target
String URL = "x";
// Network Configuration
const char* ssid = "x"; // Nama WiFi
const char* password = "x"; // Password WiFi
// Usltrasonik Pin Configuration
#define echoPin 2 // Echo pin for HC-SR04
#define trigPin 4 // Trigger pin for HC-SR04
long duration, distance;
// Thermal printer pin configuration
uint8_t address[6] = {0x66, 0x32, 0xD5, 0xE0, 0x5D, 0xC1};
char *pin = "0000";
bool connected;
void setup() {
Serial.begin(115200);
// Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// WiFi
connectWiFi();
// Thermal printer
SerialBT.setPin(pin);
SerialBT.begin("ESP32test", true); // master = true
Serial.println("The device started in master mode, make sure remote BT device is on!");
connected = SerialBT.connect(address);
Serial.print("connected : ");
Serial.println(connected);
if(connected) {
Serial.println("Connected Succesfully!");
} else {
while(!SerialBT.connected(5000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
SerialBT.connect();
if(connected){
Serial.println("Test printer begin");
printer.begin(); // Init printer (same regardless of serial type)
// Print centered "Printer OK"
printer.justify('C');
printer.println(F("Printer OK"));
// After printing procedure
printer.sleep(); // Tell printer to sleep
delay(3000L); // Sleep for 3 seconds
printer.wake(); // MUST wake() before printing again, even if reset
printer.setDefault(); // Restore printer to defaults
}
}
void loop() {
//
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2;
if(distance < 5){
// Cek ketersediaan jaringan WiFi
if(WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
// Data
String postData = "distance=" + String(distance);
// Konfigurasi HTTP Request
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// HTTP Request dan Simpan hasilnya di variabel
int httpCode = http.POST(postData);
String payload = http.getString();
// Print hasil request
Serial.print("URL : "); Serial.println(URL);
Serial.print("Data: "); Serial.println(postData);
Serial.print("httpCode: "); Serial.println(httpCode);
Serial.print("payload : "); Serial.println(payload);
Serial.println("--------------------------------------------------");
}
}
// Function
void connectWiFi() {
WiFi.mode(WIFI_OFF);
delay(1000);
//This line hides the viewing of ESP as wifi hotspot
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Menghubungkan ke jaringan WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Terkoneksi ke jaringan : "); Serial.println(ssid);
Serial.print("Alamat IP : "); Serial.println(WiFi.localIP());
}
and this is my error
Arduino: 1.8.19 (Windows 10), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None" text section exceeds available space in boardSketch uses 1400529 bytes (106%) of program storage space. Maximum is 1310720 bytes. Global variables use 45888 bytes (14%) of dynamic memory, leaving 281792 bytes for local variables. Maximum is 327680 bytes. Error compiling for board DOIT ESP32 DEVKIT V1.
Thank's!