Date type mismatch when trying to replace "Serial.readBytes"

Trying to feed my own variable to be sent via bleuart (BLE UART) instead of using the data received in the HW serial buffer. If I rewrite the code from: bleuart.write( buf, count ); to => bleuart.write(“12345678”); it works. But when I try to replace the “12345678” string with a variable int (ex.) unsigned long test = 12345678; or with byte test = 12345678; it does not work. Then I tried to convert it to String and String() and same problems.

This is part of the “Blueart” example code from Adafruit in Arduino for the nRF52 BLE boards in Peripherals. It works fine. It reads the hardware Serial port and sends the data via BLE (ble uart) to the mobile phone. The mobile phone sends back data via BLE (ble uart) and the BLE sends the received data to the HW serial port.

This is part of the working original code:

//part of the working code

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

 // Configure and Start BLE Uart Service
  bleuart.begin();
}


void loop()
{
  // Forward data from HW Serial to BLEUART
  while (Serial.available())
  {
    // Delay to wait for enough input, since we have a limited transmission buffer
    delay(2);

    uint8_t buf[64];
    int count = Serial.readBytes(buf, sizeof(buf));
    bleuart.write( buf, count );
  }

  // Forward from BLEUART to HW Serial
  while ( bleuart.available() )
  {
    uint8_t ch;
    ch = (uint8_t) bleuart.read();
    Serial.write(ch);
  }
}

This is the modified code where I feed my own variable to the blueart instead of reading it from the serial:

//part of the what I tried

byte test =  12345678;
//  tried also:
// String test = String"12345678";
//String (test) = "12345678";
//String (test) =  12345678;

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

 // Configure and Start BLE Uart Service
  bleuart.begin();
}


void loop()
{
  // Forward data from HW Serial to BLEUART
  while (Serial.available())
  {
    // Delay to wait for enough input, since we have a limited transmission buffer
    delay(2);

    uint8_t buf[64];
    int count = test.getBytes(buf, sizeof(buf));
    bleuart.write( buf, count );
  }

  // Forward from BLEUART to HW Serial
  while ( bleuart.available() )
  {
    uint8_t ch;
    ch = (uint8_t) bleuart.read();
    Serial.write(ch);
  }
}

Here is the entire code:

#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>

// BLE Service
BLEDfu  bledfu;  // OTA DFU service
BLEDis  bledis;  // device information
BLEUart bleuart; // uart over ble
BLEBas  blebas;  // battery

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

#if CFG_DEBUG
  // Blocking wait for connection when debug mode is enabled via IDE
  while ( !Serial ) yield();
#endif
  
  Serial.println("Bluefruit52 BLEUART Example");
  Serial.println("---------------------------\n");

  // Setup the BLE LED to be enabled on CONNECT
  // Note: This is actually the default behaviour, but provided
  // here in case you want to control this LED manually via PIN 19
  Bluefruit.autoConnLed(true);

  // Config the peripheral connection with maximum bandwidth 
  // more SRAM required by SoftDevice
  // Note: All config***() function must be called before begin()
  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);

  Bluefruit.begin();
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values
  Bluefruit.setName("Bluefruit52");
  //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

  // Configure and Start Device Information Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  // Configure and Start BLE Uart Service
  bleuart.begin();

  // Start BLE Battery Service
  blebas.begin();
  blebas.write(100);

  // Set up and start advertising
  startAdv();

  Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
  Serial.println("Once connected, enter character(s) that you wish to send");
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.addName();
  
  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   * 
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html   
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds  
}

void loop()
{
  // Forward data from HW Serial to BLEUART
  while (Serial.available())
  {
    // Delay to wait for enough input, since we have a limited transmission buffer
    delay(2);

    uint8_t buf[64];
    int count = Serial.readBytes(buf, sizeof(buf));
    bleuart.write( buf, count );
  }

  // Forward from BLEUART to HW Serial
  while ( bleuart.available() )
  {
    uint8_t ch;
    ch = (uint8_t) bleuart.read();
    Serial.write(ch);
  }
}

// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char central_name[32] = { 0 };
  connection->getPeerName(central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

/**
 * Callback invoked when a connection is dropped
 * @param conn_handle connection where this event happens
 * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
 */
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}

"12345678" is s string
12345678 is an integer

It seems that the function needs a string as a parameter

Could you convert the integer into a string I wonder ?
The itoa() function perhaps ?

There is a difference between a String and a string. A String is an object of the String class. A string is a null terminated character array. The terms are not interchangeable. If a function wants a string it will not work with a String.

byte test =  12345678;

A byte can hold numbers from 0 to 255.

Arduino data types.

Thank you gents! It seems to work with BLE in UART mode. :slight_smile:
Will play around later to make it dynamic size.

The changes:

// declare String:
String stringThree;

//Set up:
  stringThree = String();


//in the void loop: 

  // adding a variable long integer to a String:
  stringThree =  millis();
char charBuf[20];
stringThree.toCharArray(charBuf, 20);
 bleuart.write(charBuf);

Why are you using a String just to convert it to string? Does not make much sense.

Are you aware of the pitfalls of using the String class? See the evils of Strings.

To do it without the String class using the itoa() function like UKHeliBob suggested.

// declare String:
unsigned long stringThree;

//in the void loop:
  stringThree =  millis();
  char charBuf[20];
  itoa(stringThree, charBuf, 10); 
  bleuart.write(charBuf);

groundFungus:
To do it without the String class using the itoa() function like UKHeliBob suggested.

Might also want to look at utoa() for unsigned int, ltoa() for long, and ultoa() for unsigned long.

Thank you groundFungus and **david_2018 ** I need to digest the link you sent about The Evils of Strings but at first look it seems that they are a memory hog. I setup the code based on the Arduino example, but since I am still not completely clear about Strings and Characters, so I took the easiest route.

Thank you for the sample code to both of you, I need to digest and understand further so I can use it next time in the most efficient way. Yes, my variable for sending via blueart is an unsigned long integer with 6-8 digits, positive only.