Hi all,
I am working on some code and trying to upload it to an Lolin32 ESP32:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <WiFi.h> // ESP32 WiFi
bool newData = false; //varaible for storing if there is new data
bool dataAvailable = false; //variable for storing if there is data available
bool connectWiFi = false; //variable for storing if it's time to connect to WiFi
char receivedData[50]; //store received data
char wifiSSID[25];
char wifiPassword[50];
BLECharacteristic *pCharacteristic; //create object
bool deviceConnected = false; //variable for storing if BLE is connected
/* UUIDs */
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
/* ON BLE DISCONNECTION */
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
/* WHEN BLE DATA IS RECEIVED */
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue(); //get the data
/* STORE THE DATA */
if (rxValue.length() > 0) {
for (int i = 0; i < rxValue.length(); i++)
newData = true; //notify that data has been received
}
/* IF THERE IS UNREAD DATA */
if (newData == true) {
receivedData[rxValue.length() + 1] = {};
strcpy(receivedData, rxValue.c_str());
newData = false;
dataAvailable = true;
}
/* WAIT FOR WIFI SSID AND PASSWORD TO BE RECEIVED */
while (strstr (wifiPassword, "PASSWORD") == NULL) { //once all data has been entered
if (dataAvailable == true) { //if there is unread data available
/* IF THE INCOMING PACKAGE IS THE WIFI SSID */
if (strstr(receivedData, "SSID")) { //if data received include the word "lat" (received the latitude)
strcpy(wifiSSID, (char *)(strstr(receivedData, "SSID") + 4)); //make variable wifiSSID = the received data excluding the word "SSID"
/* SEND A SUCCESS MESSAGE */
char datastring[] = "Success!";
pCharacteristic->setValue(datastring);
pCharacteristic->notify();
dataAvailable = false;
}
/* IF THE INCOMING PACKAGE IS THE WIFI PASSWORD */
if (strstr(receivedData, "PASSWORD")) { //if data received include the word "lat" (received the latitude)
strcpy(wifiPassword, (char *)(strstr(receivedData, "PASSWORD") + 8)); //make variable wifiSSID = the received data excluding the word "SSID"
/* SEND A SUCCESS MESSAGE */
char datastring[] = "Success!";
Serial.println("*** Sent Value: ***");
Serial.println (datastring);
pCharacteristic->setValue(datastring);
pCharacteristic->notify();
dataAvailable = false;
connectWiFi = true;
}
}
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("BLE ESP32"); // Create the BLE Device
/*CREATE A BLE SERVER */
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID); // Create the BLE Service
pCharacteristic = pService->createCharacteristic( // Create a BLE Characteristic
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_READ
);
pCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start(); // Start the service
// Start advertising
pServer->getAdvertising()->start(); //be available for connection
Serial.println("Waiting a client connection to notify...");
}
void loop() {
Serial.println("loop");
delay(1000);
/* IF WE HAVE RECEIVED THE WIFI CREDENTIALS */
if (connectWiFi == true) { //if we have the WiFi credentials
ConnectWiFi(); //connect to wifi
}
}
void ConnectWiFi() {
/* CONNECT TO WIFI */
WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP()); //print your IP
connectWiFi = false;
}
But I get this error:
Global variables use 57860 bytes (17%) of dynamic memory, leaving 269820 bytes for local variables. Maximum is 327680 bytes.
Sketch too big; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing it.
Error compiling for board WEMOS LOLIN32.
Surely my program is not to big?
Thanks,
Zeb