1

I have the BLE Module Adafruit ItsyBitsy nRF52840 Express and I want to send sensor values from a Hallsensor to my computer. I was able to have the right data checking with the Serial monitor.

int adcin = A1;
int adcvalue = 0;
float mv_per_lsb = 1200.0F/4096.0F; // 10-bit ADC with 3.6V input range
double freq = 100;
uint32_t lastMicros = 0;  //variable to track sampling frequency 
double microseconds = 1000000.0;
double interval = microseconds/freq;

void setup() 
{
  Serial.begin(115200);
  // Set the analog reference to 3.0V (default = 3.6V)
  analogReference(AR_INTERNAL_1_2);
  // Set the resolution to 12-bit (0..4095)
  analogReadResolution(12); // Can be 8, 10, 12 or 14
  // Let the ADC settle
  delay(1);
}

void loop() {

  // Get a fresh ADC value after intervall (1/freq)
  if (micros()-lastMicros >= interval)
  {
    adcvalue = analogRead(adcin);
    lastMicros = micros();
    
    // Display the results
    Serial.print(lastMicros);
    Serial.print(" [");
    Serial.print((float)adcvalue * mv_per_lsb);
    Serial.println(" mV]");
  } 
}

Then I tried to send these values through BLE connectin to my computer. So I knew I had to create a service and a characteristic for the GATT profile. But using the guide from adafruit was not really helpful. Right know I saw that with the following code I was able to see the module on my computer and read the characteristic I made. But it seems to just give me random Hex values rather than my sensor data.

#include <bluefruit.h>

BLEService        myService = BLEService(0x187A);
BLECharacteristic myChar = BLECharacteristic(0x184D);

BLEDis bledis;    // DIS (Device Information Service) helper class instance

int adcin = A1;
int adcvalue = 0;
float mv_per_lsb = 1200.0F/4096.0F; // 10-bit ADC with 3.6V input range
double freq = 100;
uint32_t lastMicros = 0;  //variable to track sampling frequency 
double microseconds = 1000000.0;
double interval = microseconds/freq;
float volt = 0;

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

  //Setup for data acquisition
  // Set the analog reference to 3.0V (default = 3.6V)
  analogReference(AR_INTERNAL_1_2);
  // Set the resolution to 12-bit (0..4095)
  analogReadResolution(12); // Can be 8, 10, 12 or 14
  // Let the ADC settle
  delay(1);

  Serial.println("BLE data acquisition");
  Serial.println("-----------------------\n");

  // Initialise the Bluefruit module
  Serial.println("Initialise the Bluefruit nRF52 module");
  Bluefruit.begin();

  // Set the advertised device name (keep it short!)
  Serial.println("Setting Device Name to 'Adafruit_BLE'");
  Bluefruit.setName("Adafruit_BLE");

  // Set the connect/disconnect callback handlers
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // Configure and Start the Device Information Service
  Serial.println("Configuring the Device Information Service");
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit");
  bledis.begin();

  // Setup the BLEService and BLECharacteristic classes
  Serial.println("Configuring the custom Service");
  setupSer();

  // Setup the advertising packet(s)
  Serial.println("Setting up the advertising payload(s)");
  startAdv();

  Serial.println("Ready");
  Serial.println("\nAdvertising");
}

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

  // Include custom Service UUID
  Bluefruit.Advertising.addService(myService);

  // Include Name
  Bluefruit.Advertising.addName();

  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 setupSer(void)
{
  myService.begin();
  
  myChar.setProperties(CHR_PROPS_READ);
  myChar.setPermission(SECMODE_OPEN, SECMODE_OPEN);
  myChar.setFixedLen(4);
  myChar.begin();
}

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);
}


void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

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


void loop()
{
  // Get a fresh ADC value after intervall (1/freq)
  if (micros()-lastMicros >= interval)
  {
    adcvalue = analogRead(adcin);
    lastMicros = micros();
    volt = (float)adcvalue * mv_per_lsb;
    // Display the results
    Serial.print(adcvalue);
    Serial.print(" [");
    Serial.print(volt);
    Serial.println(" mV]");
    myChar.write16(volt);
  } 
}

I really got confusing with how to write my sensor values to the characteristic.

Does anyone know how I can achieve that? Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.