Hello everyone,
I'm working on a project with Adafruit 32u4 feather LE trying to send some data from the board to a receiver (i usedpi3 here).
The coding I used on 32u4 LE is the adafruit bluefruitLE nrf51 library and the key function should be ble.print() to send data. However in my project I need to send data from over 10 sensors and ble.print() will send my data in the format of string, I used ble.write() instead to send bytes, minimising the size of data.
On the pi3 side, I used a noble related library (https://www.npmjs.com/package/nrfuart) and it could read data I wanted. However, the problem is that sometimes the data packet is separated into two.
I have tried forcing all packets into 20 bytes and added a .flush() after writing.
For example, on the 32u4le side, generally, my code is something like: (the following is some testing code but I believe it makes sense)
unsigned long data1 = 4294967295; (ff in hex)
unsigned long data2 = 4294967295;
unsigned long data3 = 4294967295;
unsigned long data4 = 4294967295;
unsigned long data5 = 4294967295;
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
byte buf[20];
buf[0] = data1 & 255;
buf[1] = (data1 >> 8) & 255;
buf[2] = (data1 >> 16) & 255;
buf[3] = (data1 >> 24) & 255;
buf[4] = data2 & 255;
buf[5] = (data2 >> 8) & 255;
buf[6] = (data2 >> 16) & 255;
buf[7] = (data2 >> 24) & 255;
buf[8] = data3 & 255;
buf[9] = (data3 >> 8) & 255;
buf[10] = (data3 >> 16) & 255;
buf[11] = (data3 >> 24) & 255;
buf[12] = data4 & 255;
buf[13] = (data4 >> 8) & 255;
buf[14] = (data4 >> 16) & 255;
buf[15] = (data4 >> 24) & 255;
buf[16] = data5 & 255;
buf[17] = (data5 >> 8) & 255;
buf[18] = (data5 >> 16) & 255;
buf[19] = (data5 >> 24) & 255;
ble.write(buf, sizeof(buf));
//Serial.flush();
ble.flush(); //(both tried)
}
On the pi3, side the coding is:
nrfuart.discoverById(id, function(ble_uart) {
console.log('discovered ' + ble_uart.id);
ble_uart.connectAndSetup(function() {
ble_uart.on('data',function(data) {
console.log(data);
});
});
});
Ideally, on the pi3 side, the output should be something like:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff (20 bytes)
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff (20 bytes)
...
however, sometimes a single data sample is separated into two packets like:
ff ff ff ff ff ff ff ff ff ff ff (12 bytes)
ff ff ff ff ff ff ff ff (8 bytes)
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff (20 bytes)
...
From the result, I feel like that sometimes the data is sent before 20 bytes of data are collected.
I believe my code did do .write() only once with the 20 bytes data and .flush() after that as shown in the above coding. I also avoid using delay() wondering if that may affect the output. Furthermore, I have tried Serial.write() the bytes out and it seems that there is no packet separation if it's printed on Serial.
It looks like that the problem arises when the data is in the nrf51 module before collected by the BLE module on PI3.
Actually, I have received all the data I want without data lost but I still feel bit unsure if this data packet separation will make some problems in my project sometime later.
It will be really helpful if anyone experienced has any idea about what leads to this problem!
Thanks a lot!
Below is the nrf51 library configuration file if needed:
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define VERBOSE_MODE true // If set to 'true' enables debug output
// SOFTWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins that will be used for 'SW' serial.
// You should use this option if you are connecting the UART Friend to an UNO
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial!
#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial!
#define BLUEFRUIT_UART_CTS_PIN -1 // Required for software serial!
#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused
// HARDWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the HW serial port you are using. Uncomment
// this line if you are connecting the BLE to Leonardo/Micro or Flora
// ----------------------------------------------------------------------------------------------
#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1
#define BLUEFRUIT_HWSERIAL_NAME Serial1
#endif
// SHARED UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following sets the optional Mode pin, its recommended but not required
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused
// SHARED SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused
// SOFTWARE SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for SW SPI communication.
// This should be used with nRF51822 based Bluefruit LE modules that use SPI
// (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_SCK 13
#define BLUEFRUIT_SPI_MISO 12
#define BLUEFRUIT_SPI_MOSI 11