Char arrays and BLE

I need to send almost a thousand 3 digit numbers through the BLE module on an Arduino Uno Wifi Rev 2. Since the BLE module is limited to 512 Bytes per BLE characteristic. it needs to be separated into multiple characteristic to fit all the data.

The plan is to make a char array of 512 characters. 3 digits plus a comma for separation makes for 128 points of data per array.

To keep things simple, I am working in the RawDataAdvertising Example sketch for the ArduinoBLE Library.
Here is my code:

#include <ArduinoBLE.h>

BLEService myService("fff0");
char stringValue[] = "nothing written yet"; 
BLECharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast,stringValue);

// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
const uint8_t completeRawAdvertisingData[] = {0x02,0x01,0x06,0x09,0xff,0x01,0x01,0x00,0x01,0x02,0x03,0x04,0x05};   

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!BLE.begin()) {
    Serial.println("failed to initialize BLE!");
    while (1);
  }

  myService.addCharacteristic(myCharacteristic);
  BLE.addService(myService);

  // Build advertising data packet
  BLEAdvertisingData advData;
  // If a packet has a raw data parameter, then all the other parameters of the packet will be ignored
  advData.setRawData(completeRawAdvertisingData, sizeof(completeRawAdvertisingData));  
  // Copy set parameters in the actual advertising packet
  BLE.setAdvertisingData(advData);
  
  // Build scan response data packet
  BLEAdvertisingData scanData;
  scanData.setLocalName("Test advertising raw data");
  // Copy set parameters in the actual scan response packet
  BLE.setScanResponseData(scanData);

   //section of interest
  char crazyBigArr[512] = "";    //create array
  for(int i = 0; i < 512; i++){
     int theoRead = 111 * (i%10);      //simulate the reading from loadsensor, not perfect, but good enough 
     Serial.print(theoRead);
     Serial.print(","); 
     crazyBigArr[i] = (char)(theoRead/100); //isolate first digit
     crazyBigArr[i+1] = (char)((theoRead%100)/10);   // isolate second digit
     crazyBigArr[i+2] =  (char)(theoRead%10);    //isolate third digit
     crazyBigArr[i+3] = ',';  //comma for separation
     i+=3;      
  }
  
  crazyBigArr[512] = '\n';
  Serial.println();
  Serial.print("big Arr:");
  Serial.println(crazyBigArr);
  myCharacteristic.writeValue(crazyBigArr);
  Serial.println("big data made");
  BLE.advertise();

  Serial.println("advertising ...");
}

void loop() {
  BLE.poll();
}

I think the first issue that I'm having is that the char array is not getting written with my digits the way I want to.
previously, I tried to use a String and then convert to char array with .toCharArray(), but it caused the arduino to boot loop.
Currently the output on the serial monitor looks like this:

As you can see, the char array doesn't get filled. I'm sure there is an easy explanation, I could use an extra set of eyes here.
Your input is much appreciated!!!

BLE is a really poor choice for that.

If you need to send numbers, why do you decide to use chars?
As i see, your data is an array of bytes,
If you send it as is, each number poses only one byte and, in addition - you dont need the commas. That is in 512 bytes you can to transmit 512 points of data - 4 times more than in chars

that's not ASCII for the digits

you need to do

crazyBigArr[I]   = '0' + (theoRead/100);        //isolate first digit
crazyBigArr[i+1] = '0' + ((theoRead%100)/10);   // isolate second digit
crazyBigArr[i+2] = '0' + (theoRead%10);         //isolate third digit
crazyBigArr[i+3] = ',';  //comma for separation

BTW it's not a best practice to mess with the index of the loop in the loop, just do
for(int i = 0; i < 512; i+=4){
(and you might overflow if the limit is not a multiple of 4)

but really not sure why you want to go through an ASCII representation

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.