Go Down

Topic: Connecting Nano 33 BLE to custom App  (Read 117 times) previous topic - next topic

reraybon

I'm trying to connect an HC-05 module to our nano 33 BLE board to connect and send data to a custom made app. I tried using ArduinoBLE and was able to send data to a BLE premade app (LightBlue) but when I tried to connect over the BT settings on an android phone the MCU thinks its connected and start to send data but the phone will sit there and try to connect but never finish. I used another AVR based Arduino connected to an HC-05 and was able to establish a connect to the custom app through it. I tried to repeat this with the Nano 33 BLE but the SoftwareSerial library does not work since its not an AVR based MCU. Does anyone know how to connect an HC-05 module to the nano 33 ble or connect to an android device over Arduino BLE? Attached is the ArduinoBLE code I used and is almost the same to the example battery monitor code and following it is the code to connect to the HC-05. Thanks!

Code: [Select]
#include<ArduinoBLE.h>
BLEService Bloodpressure  ("0x1810"); //create Blood pressure service
BLEByteCharacteristic Bloodpressurefeature ("0x2A49",BLERead | BLENotify) ; //BLE characteristic includes movment, cuff fiting etc.
BLEByteCharacteristic Bloodpressuremeasurment ("0x2A35",BLERead | BLENotify); //BLE characteristic that include monitor values (Systolic, Diastolic user ID etc) can maybe modify to work for PTT
BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
int oldBPLevel = 0;
long previousMillis = 0;  // last time the battery level was checked, in ms

void setup(){
  Serial.begin (9600); //9600 baud
   while(!Serial);
   pinMode(LED_BUILTIN, OUTPUT); //initialize built in LED to see if working
  setupBLE();
  }
 
void loop(void){
 
 BLEDevice phoneBle = BLE.central();
 Serial.println("BLE device active waiting for connections..."); //serial print that
  while(1){
    digitalWrite(LED_BUILTIN, LOW);//while waiting for connection set LED low
    phoneBle = BLE.central();
    //if connecected to device
    if (phoneBle) {
      Serial.println("Connected to central device of address: ");
      // print the central's BT address:
      Serial.println(phoneBle.address());
      // turn on the LED to indicate the connection:
      digitalWrite(LED_BUILTIN, HIGH);

      //inside this while loop, send data via changeing characteristic values
      while (phoneBle.connected()) {
          UpdateBP();
        }
       }
    }
}
void setupBLE(void){
   
   if(!BLE.begin()){ //if failed to begin
     Serial.println("Failed to start BLE");
     while(1);
     }
   BLE.setDeviceName("NT BP Monitor"); //set device name when discovered
   BLE.setLocalName("BloodPressureMonitor"); //set Local name
   BLE.setAdvertisedService(Bloodpressure); //sets service to advertise later
   //add charateristics to service
   Bloodpressure.addCharacteristic(Bloodpressurefeature);
   Bloodpressure.addCharacteristic(Bloodpressuremeasurment);
   Bloodpressure.addCharacteristic(ledCharacteristic);
   //adds service to BLE device
   BLE.addService(Bloodpressure);
   //set initial values for characteristics
   ledCharacteristic.writeValue(0);
   Bloodpressurefeature.writeValue(0);
   Bloodpressuremeasurment.writeValue(oldBPLevel);
  BLE.advertise(); //starts to advertise
  Serial.println("Set up complete!");
 }
 
 void UpdateBP(void){
   //Sends current BP values to Central device
   int Bsdata = analogRead(A0);
   Bloodpressuremeasurment.writeValue(Bsdata); //writes the BS data to Bloodpressuremeasurment
   Serial.print("data sent to BP measurment is:");
   Serial.println(Bsdata);
 }


And here's the code for the HC-05 connection.
Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial MyBlue(0, 1); // RX | TX
int flag = 0;
int LED = 8;
void setup()
{   
 Serial.begin(9600);
 MyBlue.begin(9600);
 pinMode(LED, OUTPUT);
 Serial.println("Ready to connect\nDefualt password is 1234 or 000");
}
void loop()
{
 if (MyBlue.available())
   flag = MyBlue.read();
 if (flag == 1)
 {
   digitalWrite(LED, HIGH);
   Serial.println("LED On");
 }
 else if (flag == 0)
 {
   digitalWrite(LED, HIGH);
   Serial.println("LED Off");
 }
}

Klaus_K

I copied your sketch and succesfully connected my iPhone. I can see the data updating in an app called BLE scanner.

Quote
I tried to connect over the BT settings on an android phone the MCU thinks its connected and start to send data
This is not how BLE works. When you update a characteristic, the data is just stored. The client/central device (in this case your Android phone) will read the data when it wants. You can notify the client that the data has changed but the client can ignore that.
Your Android phone must have connected to your module, otherwise you would not see the "send data". Maybe the phone does not like your update rate.

Quote
the SoftwareSerial library does not work since its not an AVR based MCU. Does anyone know how to connect an HC-05 module to the nano 33 ble
You can connect the HC-05 module to a serial port. e.g. Pin 16/17 (TX/RX) and just use Serial1 instead. So Serial goes to your Serial Monitor, Serial1 goes to your HC-05.

Go Up