the code is just a simple mixture of the bluefruit ble_multi example and some code to get the IMU data to send and format it. its being run on a xiao seeed studio nRF52840 sense. the code is just as follows:
#include <bluefruit.h>
#include “LSM6DS3.h”
#include “Wire.h”
#define MAX_PRPH_CONNECTION 2
uint8_t connection_count = 0;
#define CONVERT_G_TO_MS2 9.80665f
#define FREQUENCY_HZ 100
#define INTERVAL_MS (1000 / (FREQUENCY_HZ + 1))
static unsigned long last_interval_ms = 0;
//Create a instance of class LSM6DS3
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
// BLE Service
BLEDfu bledfu; // OTA DFU service
BLEDis bledis; // device information
BLEUart bleuart; // uart over ble
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
if (myIMU.begin() != 0) {
Serial.println(“Device error”);
} else {
Serial.println(“Device OK!”);
}
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);
// Initialize Bluefruit with max concurrent connections as Peripheral = 2, Central = 0
Bluefruit.begin(MAX_PRPH_CONNECTION, 0);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
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();
// 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
-
Technical Q&A QA1931: Using the correct Bluetooth LE Advertising and Connection Parameters for a stable connection
*/
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
}
// print a string to Serial Uart and all connected BLE Uart
void printAll(uint8_t* buf, int count)
{
Serial.write(buf, count);
size_t sent;
// Send to all connected centrals
for (uint8_t conn_hdl=0; conn_hdl < MAX_PRPH_CONNECTION; conn_hdl++)
{
sent = bleuart.write(conn_hdl, buf, count);
//Serial.println(sent);
}
}
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, “%%%d.%df”, width, prec);
sprintf(sout, fmt, val);
return sout;
}
void loop()
{
uint8_t buf[20];
uint8_t bufacc[21] = “”;
char accbuf[21] = “”;
uint8_t bufgyr[21] = “”;
char gyrbuf[21] = “”;
char fbuf[10];
int count;
float accX;
float accY;
float accZ;
float gyrX;
float gyrY;
float gyrZ;
if (millis() > last_interval_ms + INTERVAL_MS) {
last_interval_ms = millis();
accX = myIMU.readFloatAccelX() * CONVERT_G_TO_MS2;
accY =myIMU.readFloatAccelY() * CONVERT_G_TO_MS2;
accZ = myIMU.readFloatAccelZ() * CONVERT_G_TO_MS2;
gyrX =myIMU.readFloatGyroX();
gyrY =myIMU.readFloatGyroY();
gyrZ = myIMU.readFloatGyroZ();
dtostrf(accX, 5, 4, fbuf);
strcat(accbuf, fbuf);
strcat(accbuf, "\t");
dtostrf(accY, 5, 4, fbuf);
strcat(accbuf, fbuf);
strcat(accbuf, "\t");
dtostrf(accZ, 5, 4, fbuf);
strcat(accbuf, fbuf);
strcat(accbuf, "\t\0");
dtostrf(gyrX, 3, 2, fbuf);
strcat(gyrbuf, fbuf);
strcat(gyrbuf, "\t");
dtostrf(gyrY, 3, 2, fbuf);
strcat(gyrbuf, fbuf);
strcat(gyrbuf, "\t");
dtostrf(gyrZ, 3, 2, fbuf);
strcat(gyrbuf, fbuf);
strcat(gyrbuf, "\n\0");
for (int i = 0; i < sizeof(accbuf); i++){
bufacc[i] = uint8_t(accbuf[i]);
}
for (int i = 0; i < sizeof(accbuf); i++){
bufgyr[i] = uint8_t(gyrbuf[i]);
}
count = sizeof(bufacc);
// count = sizeof(buf);
printAll(bufacc, count);
bleuart.print(“\t”);
count = sizeof(bufgyr);
printAll(bufgyr, count);
bleuart.println("");
// Forward from BLEUART to HW Serial
if ( bleuart.available() )
{
count = bleuart.read(buf, sizeof(buf));
printAll(buf, count);
}
}
}
// 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);
connection_count++;
Serial.print("Connection count: ");
Serial.println(connection_count);
// Keep advertising if not reaching max
if (connection_count < MAX_PRPH_CONNECTION)
{
Serial.println(“Keep advertising”);
Bluefruit.Advertising.start(0);
}
}
/**
- 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);
connection_count–;
}
the code seems to work, as i am able to connect to bluetooth and send it to putty, however, when i try to connect the data forwarder, there is no error message, but it tries to connect to the cable on com 5 (which is only conected at the moment for testing purposes) and doesnt even acknowledge the virtual port of com 9. my supervising teacher and i have decided to atempt to use puTTy’s logs, converted to CSV, in order to upload the data we need. Sorry for bothering your team and thanks for the help.