Nano 33 BLE Bluetooth crash

I use the Nano 33 BLE to forward the data from Uart interface. The problem is the write byte array to BLE, the writeValue only work with some data, another will crash the BLE, and the length of both is same.

So, I can’t figure out how to fix the problem , is there anyone meet similar problem?

Here is the code

#include <Arduino.h>
// #include <CooperativeMultitasking.h>
#include <ArduinoBLE.h>
#include <Wire.h>

// CooperativeMultitasking tasks;
// CooperativeMultitasking task2;

//Pin Declaration

//UUID Setting

//Global Var
bool isBLEconnected = false;
bool BLEnotBegin = false;
bool LEDState = false;

bool flag_BLE = false;
bool flag_NonReading = false;

int FeedBackCounter = 0;
uint8_t readbyte_flag = 0;
uint8_t CmdCounter = 0;
uint8_t FlushCounter = 0;
uint8_t arrIndex = 0;

byte from_BLE[] = {};
byte length = 0;
uint8_t FeedBack[300] = {};
int FeedBack_index = 0;

// PreviousMillis
// unsigned long previousMillis = 0;
unsigned long previousMillis_LEDBlink = 0;
unsigned long previousMillis_checkSum = 0;
unsigned long previousMillis_cmd2reader = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis_Forward = 0;

unsigned long currentMicros = 0;
unsigned long previousMicros_readFeedBack = 0;
unsigned long previousMicros_readStart = 0;
unsigned long previousMicros_NonReading = 0;
unsigned long previousMicros_Flush = 0;

// Uart communication
UART uart(digitalPinToPinName(3), digitalPinToPinName(2), NC, NC); // TX, RX

// Functions Declaration
void LEDBlink();
void cmd2reader(byte cmd[], uint8_t cmd_size);
void ReadFeedBack();
void sendnreveice(byte cmd[], uint8_t length);

// BLE communication
BLEService ble(service_uuid);
BLEService Battery(battery_levelSer_uuid);
BLECharacteristic to_readerChar(to_readerChar_uuid, BLEWrite | BLEWriteWithoutResponse, 60);
BLECharacteristic from_readerChar(from_readerChar_uuid, BLERead | BLENotify, 400);
BLEByteCharacteristic battery_leveChar(battery_levelSer_uuid, BLERead | BLENotify);

void setup()
{
  // put your setup code here, to run once:
  // pinMode(LEDB, OUTPUT);
  Serial.begin(115200);
  // Start BLE serial
  uart.begin(115200);
  //Initializes the BLE device
  if (!BLE.begin())
  {
    BLEnotBegin = true;
  }
  // set advertised local name and service UUID:
  BLE.setLocalName(device_name);
  BLE.setDeviceName(device_name);
  BLE.setAdvertisedService(ble);
  //BLE.setAdvertisedService(Battery);
  // add the characteristic to the service
  ble.addCharacteristic(to_readerChar);
  ble.addCharacteristic(from_readerChar);
  Battery.addCharacteristic(battery_leveChar);
  // add service
  BLE.addService(ble);
  BLE.addService(Battery);
  // start advertising
  BLE.advertise();
  //enable the reader
  pinMode(13, OUTPUT);
}

void loop()
{
  currentMillis = millis();
  currentMicros = micros();
  // put your main code here, to run repeatedly:
  LEDBlink();
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  if (central)
  {
    // Only send data if we are connected to a central device.
    isBLEconnected = (central.connected() ? true : false);
    if (central.connected())
    {
      if (!flag_BLE)
        memset(from_BLE, 0, sizeof(from_BLE));
      if (to_readerChar.written())
      { // revice the comand through BLE
        length = to_readerChar.valueLength();
        to_readerChar.readValue(from_BLE, length);
        if (from_BLE[0] == (byte)0xA0 && from_BLE[2] == (byte)0xFE)
        {
          if (!readbyte_flag)
          {
            flag_BLE = true;
            readbyte_flag = 1;
          }
        }
      }
      sendnreveice(from_BLE, length);
    }
  }
}

void sendnreveice(byte cmd[], uint8_t length)
{
    if (readbyte_flag == 1)
    {
      cmd2reader(cmd, length);
    }

    else if (readbyte_flag == 2)
    {
      ReadFeedBack();
    }
    else if (readbyte_flag == 3)
    {
          from_readerChar.writeValue(FeedBack, FeedBack_index, true);
          Serial.println("Send");
          readbyte_flag = 0;

    }
    else
    {
      memset(FeedBack, 0, sizeof(FeedBack));
      FeedBack_index = 0;
      readbyte_flag = 0;
    }
}

void cmd2reader(byte cmd[], uint8_t cmd_size)
{
  uint8_t buffer_len = cmd[1] + 2;
  //
  if (currentMillis - previousMillis_cmd2reader >= 20)
  {
    uart.write(cmd, buffer_len);
    previousMillis_cmd2reader = currentMillis;
    if (cmd[3] != (byte)0x70)
    {
      CmdCounter += 1;
    }
  }
  if (CmdCounter > 3 || cmd[3] == (byte)0x70)
  {
    // CmdSend = false;
    // flag_sendCmd = false;
    readbyte_flag = 0;
    flag_BLE = false;
    CmdCounter = 0;
  }
  if (uart.available())
  {
    // CmdSend = false;
    // flag_sendCmd = false;
    readbyte_flag = 2;
    flag_BLE = false;
    CmdCounter = 0;
  }
}

void ReadFeedBack()
{
  if (!FeedBack_index)
  {
    Serial.print("****Cmd from Reader****\n");
    previousMicros_readStart = currentMicros;
  }
  if (currentMicros - previousMicros_readFeedBack > (unsigned long)8)
  {
    // Serial.print(FeedBack_index);
    // Serial.print("\t");
    // Serial.println(currentMicros);
    if (uart.available())
    {
      FeedBack[FeedBack_index] = uart.read();
      FeedBack_index += 1;
    }
    previousMicros_readFeedBack = currentMicros;
  }
  if (!uart.available())
  {
    if (!flag_NonReading)
    {
      flag_NonReading = true;
      previousMicros_NonReading = currentMicros;
    }
  }
  if ((currentMicros - previousMicros_NonReading > (unsigned long)500 && (!uart.available())))
  {
    for (int i = 0; i < FeedBack_index; i++)
    {
      uart.read();
    }
    if (FeedBack[0] == (byte)0xA0 && FeedBack[2] == (byte)0xFE)
    {
      readbyte_flag = 3;
    }
    else
    {
      readbyte_flag = 0;
    }
    // readbyte_flag = 3;
    flag_NonReading = false;
  }
  if (currentMicros - previousMicros_readStart > 50000)
  {
    FeedBack_index = 0;
    // flag_NonReading = false;
    // memset(FeedBack, 0, sizeof(FeedBack));
    readbyte_flag = 0;
  }
}

void LEDBlink()
{
  if (BLEnotBegin)
  {
    if (currentMillis - previousMillis_LEDBlink >= 1)
    {
      LEDState = !LEDState;
      digitalWrite(LEDR, LEDState);
      previousMillis_LEDBlink = currentMillis;
    }
  }
  else
  {
    unsigned long interval = (isBLEconnected ? 100 : 500);
    if (currentMillis - previousMillis_LEDBlink >= interval)
    {
      LEDState = !LEDState;
      digitalWrite(LEDB, LEDState);
      previousMillis_LEDBlink = currentMillis;
    }
  }
}

Hello,

You forgot to give a size to this array : byte from_BLE[] = {};

Thank You!!!, it works. I don’t know it have a impact

1 Like