Mkr nano ble 33

Hello, I want to make an application with arduino mkr nano ble 33. I tried to use ChrisWard mit app inventor application, I modified UUIDs, but doesn't working. The bluetooth is connecting with the board, but doesn't receive anything.

#include <ArduinoBLE.h> 


BLEService test("181A"); 
BLEShortCharacteristic testC("2A6E", BLERead); 
void setup() {
  
  Serial.begin(9600); // initialize serial communication
  while (!Serial);

  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  BLE.setDeviceName("");
  BLE.setLocalName("Send data Test");
  test.addCharacteristic(testC);
  BLE.addService(test);
  BLE.setAdvertisedService(test);
  
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");

  
}

void loop() {
  BLE.poll();
  
}

void blePeripheralConnectHandler(BLEDevice central) {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print("Connected event, central: "); Serial.println(central.address());

  float temperature = 15.67;
  short t = convertFloatToShort(temperature);
  testC.writeValue(t);
  Serial.print("Temperature = "); Serial.println(temperature);

  float humidity = 134.67;
  short h = convertFloatToShort(humidity);
  testC.writeValue(h);
  Serial.print("Humidity = "); Serial.println(humidity);
}

void blePeripheralDisconnectHandler(BLEDevice central) {
  digitalWrite(LED_BUILTIN, LOW);
  Serial.print("Disconnected event, central: "); Serial.println(central.address());
}


short convertFloatToShort(float x) {
  x = x * 100;
  if (x < -32768) {
    return -32768;
  }
  if (x > 32767) {
    return 32767;
  }
  return (short)round(x);
}

BLE_RECEIVE_STRINGS_1.aia (194.7 KB)

In nrf the board receive data.

Is it using the exact same Sketch? Nrf is only receiving one value, a temperature, as a float where your intent appears to be to receive two shorts, temperature and humidity?

I see the Sketch is not being used to measure temperature or Humidity yet, just using test values for each, but the blePeripheralConnectHandler() is not within the main loop?

it's for test, I don't understand what you want to say ... if you can help me...

Hello mts

I could ask a thousand more questions - it would be better if you could describe the goal of your Project. Remember, we cannot see your hardware setup and we cannot read your mind, so what seems obvious to you may not be obvious to us.

  1. To me, it looks like you want to send 2 values to the App periodically. Is that correct? Not correct? Tell us.

Where did you find this Sketch? It seems to have many contradictions.

Is correct. I want to send 2 values to the App periodically, and I tried to create another Sketch.

#include <ArduinoBLE.h>
#include <DFRobot_DHT11.h>
DFRobot_DHT11 DHT;
#define DHT11_PIN 2

BLEService sensors("181A"); 
BLEShortCharacteristic currentTemperature("2A6E", BLERead | BLENotify);
BLEUnsignedShortCharacteristic currentHumidity("2A6F", BLERead | BLENotify); 

int oldreadT = 0;  // prima valoare 
int oldreadH = 0;  // prima valoare
long previousMillis = 0;  // last time the battery level was checked, in ms

void setup() {
  Serial.begin(9600); // initializam comunicatia seriala
  while (!Serial);

  pinMode(LED_BUILTIN, OUTPUT); // folosim ledul de pe placa sa vedem cand e conecat si cand nu

// se porneste blueetooth ul si urmeaza partea de configuratii - vezi pagina oficiala ce inseamna fiecare
  if (!BLE.begin()) {
    Serial.println("BLE nu functioneaza!");
    while (1);
  }
   DHT.read(DHT11_PIN);
   
  BLE.setDeviceName("");
  BLE.setLocalName("DisIBLE");
  
  BLE.setAdvertisedService(sensors); // add the service UUID
  sensors.addCharacteristic(currentTemperature);
  sensors.addCharacteristic(currentHumidity);
  BLE.addService(sensors);

  currentTemperature.writeValue(oldreadT);
  currentHumidity.writeValue(oldreadH);
  
 /* Start advertising BLE.  It will start continuously transmitting BLE
     advertising packets and will be visible to remote BLE central devices
     until it receives a new connection */

  // start advertising
  BLE.advertise();
  
  Serial.println("BLE activ, se asteapta conexiunea.......");
}

void loop() {
  // asteapta conexiunea
  BLEDevice central = BLE.central();

  // daca e conectat la alt dispozitiv se executa
  if (central) {
    Serial.print("Conectat la dispozitivul cu adresa MAC: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);

    // masuram la fiecare 200ms
    // atat timp cat exista conexiune
    while (central.connected()) {
      long currentMillis = millis();
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateP();
      }
    }
    // cand nu mai e conexiune, se deconecteaza si afiseaza
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Deconectat de la adresa MAC: ");
    Serial.println(central.address());
  }
}

void updateP() {
  // citim valoarea parametrilor

 
  float temp1 = DHT.temperature;
  oldreadT=oldreadT+1;
  temp1=temp1+oldreadT;
  float hum1 = DHT.humidity;
  oldreadH=oldreadH+1;
  hum1=hum1+oldreadH;
  short temp = convertFloatToShort(temp1);
  short hum = convertFloatToShort(hum1);   
  Serial.print("\n\nValoarea parametriilor masurata este: \n"); 
  Serial.print("temparatura "); 
  Serial.print(temp1);
  Serial.print("\n umiditatea "); 
  Serial.print(hum1);
  currentTemperature.writeValue(temp);
  currentHumidity.writeValue(hum);
  delay(500);
  
}

short convertFloatToShort(float x) {
  x = x * 100;
  if (x < -32768) {
    return -32768;
  }
  if (x > 32767) {
    return 32767;
  }
  return (short)round(x);
}

Hello mts

BLE_RECEIVE_SHORTS.aia (192.4 KB)

If you search the forum, you will see we have had a lot of issues with the Nano 33, it's built-in BLE should be easy to use but actually the 33 has very few compatible libraries. So, get ready for your hair to go grey.

Does your new Sketch compile without errors being reported?

The App Inventor Project you have used is waiting to receive text strings, but your Sketch is sending shorts. Clearly that needs to change (see attached).

Also, the two writeValue() functions in the Sketch need a small delay(20) between them, to give the App time to process the first before the second arrives.