I'm trying to develop a "UART to BLE" and "UART to WIFI" bridge application on a ESP32-WROOM.
I'm using Arduino core 1.0.0 stable package available here
My application is the following:
Code: Select all
#include <EEPROM.h>
#include "Arduino.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <WiFi.h>
// UART: Serial1
#define UART_BR 115200
#define UART Serial
std::string rxValue; // RX STRING VALUE received from BLE
BLEServer * pServer = NULL;
BLECharacteristic * pTxCharacteristic = NULL;
BLEService * pService = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
#define SERVICE_UUID "49535343-fe7d-4ae5-8fa9-9fafd205e455" // UART service UUID
#define CHARACTERISTIC_UUID_TX "49535343-1e4d-4bd9-ba61-23c647249616"
#define CHARACTERISTIC_UUID_RX "49535343-8841-43f4-a8d4-ecbe34729bb3"
int countBytes = 0;
uint8_t RXbuffer[256];
uint8_t TXbuffer[256];
class MyServerCallbacks: public BLEServerCallbacks
{
void onConnect(BLEServer* pServer)
{
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer)
{
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0)
{
for(int i=0; i < rxValue.length(); i++)
{
TXbuffer[i] = rxValue[i];
}
UART.write(TXbuffer,rxValue.length());
}
}
};
const char* ssid = "ESP32-Access-Point";
const char* password = "123456789";
WiFiServer server(1001);
uint8_t byte_STATE = 0;
static uint8_t lastPinState;
BLEDevice *bleDevice;
uint8_t pinState;
void setBLE()
{
UART.println("Init BLE...");
deviceConnected = false;
oldDeviceConnected = false;
// Create the BLE Device
//BLEDevice::init("FORTEST-ESP32-BLE");
bleDevice->init("FORTEST-ESP32-BLE");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
}
void setWIFI(){
UART.println("Init WIFI...");
WiFi.softAP(ssid, password);
server.begin();
}
//The setup function is called once at startup of the sketch
void setup()
{
UART.begin(115200);
EEPROM.begin(1);
byte_STATE = EEPROM.read(0);
lastPinState = byte_STATE;
pinMode ( 5 , INPUT_PULLUP);
if(byte_STATE == 0)
{
// BLE
setBLE();
}else
{
// WIFI
setWIFI();
}
}
// The loop function is called in an endless loop
void loop()
{
pinState = digitalRead(5);
if(!pinState && lastPinState)
{
//UART.println("Button pressed!");
byte_STATE = !byte_STATE;
EEPROM.write(0,byte_STATE);
EEPROM.commit();
delay(100);
byte_STATE = EEPROM.read(0);
//UART.print("byte_STATE = ");
UART.println(byte_STATE);
delay(100);
ESP.restart();
}
lastPinState = pinState;
switch (byte_STATE)
{
case 0:
{ // BLE
// connected
if (deviceConnected)
{
countBytes = UART.available();
while (countBytes>=20)
{
UART.readBytes(RXbuffer, 20);
pTxCharacteristic->setValue(RXbuffer,20); // tx to BLE
pTxCharacteristic->notify();
countBytes-=20;
}
if (countBytes > 0) // rx from UART
{
UART.readBytes(RXbuffer, countBytes);
pTxCharacteristic->setValue(RXbuffer,countBytes); // tx to BLE
pTxCharacteristic->notify();
}
}
// disconnecting
if (!deviceConnected && oldDeviceConnected)
{
//UART.println("DISCONNECT!");
delay(500);
pServer->startAdvertising(); // restart advertising
oldDeviceConnected = deviceConnected;
//rxValue.clear();
//UART.flush();
}
// connecting
if (deviceConnected && !oldDeviceConnected)
{
//UART.println("CONNECTED!");
oldDeviceConnected = deviceConnected;
//rxValue.clear();
//UART.flush();
}
break;
}
case 1:
{
// WIFI
WiFiClient client = server.available(); // Listen for incoming clients
if (client)
{ // If a new client connects,
//Serial.println("New Client."); // print a message out in the serial port
while (client.connected())
{ // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
// read a byte, then
UART.write(client.read()); // print it out the serial monitor
}
if (UART.available())
{
if (client.connected()) client.write(UART.read());
}
}
}
break;
}
default:
{
break;
}
}
}
- At the startup an EEPROM address is read
- If a 0 value is read a BLE/UART bridge will run
- If a 1 value is read a WIFI/UART bridge will run
Now forget about the various obvious optimizations of the code.
My problem is that with the two bridges enabled i get the following errors when i start the application
Code: Select all
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
Fatal exception (0): IllegalInstruction
epc1=0x4008033d, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
others errors are the following
Code: Select all
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
Fatal exception (28): LoadProhibited
epc1=0x400368b8, epc2=0x00000000, epc3=0x00000000, excvaddr=0x000008f7, depc=0x00000000
Code: Select all
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
Fatal exception (0): IllegalInstruction
epc1=0x40080338, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000
If function call "setBLE()" is commented the application start the BLE/UART bridging without problems
If function call "setWIFI()" is commented the application start the WIFI/UART bridging without problems
Can it be a problem due to the lack of RAM?
Any suggestion is welcome
Thanks
Fabrizio