App inventor - Arduino Nano33Ble communication issue

Hi,
I’m trying to put on/off the built-in led of Nano33Ble Boards through an App but I0m facing an issue with WriteStrings method.

I’ve the sample sketch “Led” written on the Nano board.
In order to test the sketch I switch on and off the led with LightBlue App. Everything is Ok!

Then I’ve my app in Appinventor as follow:

Trying the app I succed to scan and connect to the arduino Nano but when I push a button in order to write 1 or 0 nothing is received on board (as from serial monitor on Arduino IDE).

Anyone has an idea about it?

Can you post the sketch? And what exactly does the light blue app send to the Nono?
Cheers, Ghica

the sketch is this:

/*
  LED

  This example creates a BLE peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic BLE central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

LightBlue send the value that I set, which is 1 to turn on and 0 to turn off.
I verified in the serial console that these are the value received by the board.

Finally I’ve the app running and values sent.

I changed the method WriteStrings to WriteIntegersWithResponse. (If I use WriteIntegers don’t succed to write)

I’m happy because app is ok but if someone can explain me the reason it would be appreciated.

Thx
Andrea

1 Like

Hello,
I have also had the same problem with an Arduino Nano 33 BLE Sense and would greatly appreciate anyone who can give us an explanation of why this happens.
Thank you

In my case, neither of the WriteStrings or WriteStringsWithResponse or WriteBytes or any of the Write functions seem to “send” the right value. On the other end (Arduino) am trying to print the received value using - switchcharacteristic.value() ; the nrfConnect app seems to deliver the exact values when we use it’s “write” ability to send Byte or Char. So, am guessing the problem is not at the Arduino end. I’ve tried both CurieBLE.h and ArduinoBLE.h and am sure am missing something at the App inventor end. If anyone has a similar problem where their “values” are not reaching the Nano BLE, pls shout.

thank you.

Even though I solved without really understand the problem, I’d like to see your app inventor blocks, could you post it?

absolutely … it is the same one from the examples i borrowed from the gentleman who had it done for HM10 i guess. I’ve tried all the “Write” functions and sending variables and “lists” in various ways but no luck :slight_smile: ARD_HM10_AI2_Single_LED_04 (1).aia (176.5 KB)

once again - kindly note - the nrfConnect app works just fine; am able to send bytes or UINT or an SINT works just fine. The code on the Arduino side is similar to the one pasted above but i’ll just paste the part of code that matters, here.

while (central.connected())
{
  if (switchCharacteristic.written())
  {
    byte Value = 0; 
    Serial.print("swith char value:"); Serial.println(switchCharacteristic.value()); 
    digitalWrite(LED_BUILTIN, HIGH);
    switchCharacteristic.readValue(Value); 
    Serial.print("BLE value using readValue:"); Serial.println(Value); 
    Serial.print("value length="); 
    Serial.println(switchCharacteristic.valueLength());        
   }

thanks.

I had the same problem with the LED Arduino example sketch (on a nano 33 IoT) shown above in that it worked using the nRFConnect and Light Blue android apps. Both said the characteristic was read & write. In AppInventor, if I used the WriteBytes method, it did not work. Further, it returned the result of the 'CanReadCharacteristic' call as FALSE. However, if I changed the WriteBytes call to WriteBytesWithResponse, the 'CanReadCharacteristic' now returns TRUE and the app responds when writing. One more interesting observation was that I ran the following on the Arduino;

  if(switchCharacteristic.canWrite())
    Serial.println("led can be written");
  else
    Serial.println("led canNOT be written");

  byte led_properties = switchCharacteristic.properties();
  if(led_properties & BLERead)
    Serial.println("led is readable");
  if(led_properties & BLEWrite)
    Serial.println("led is writable");
  if(led_properties & BLENotify)
    Serial.println("led is notifiable");
  Serial.print("led_properties value is = ");
  Serial.println(led_properties);

with the following results;
led canNOT be written
led is readable
led is writable
led_properties value = 10 // 0x0a

not sure therefore if it's an appinventor issue or an Arduino library issue.

I came across the same problem, thank you for posting your solution! Works for me too!

Same for me, tried for hours to get it work untill i found this post! Only works if i add writeStringsWithResponse! So probably ai2 bugg?

And the same App Inventor problem for me, along with another snag.

The 'CallbackLED' example in ArduinoBLE library receives data only if:

  1. BluetoothLE.WriteBytesWithResponse() block used, not the more obvious (in context of library example) BluetoothLE.WriteBytes() block.

  2. serviceUuid and characteristicUuid 'sockets' were plugged into text entry blocks, not global variable getter menu blocks.

Correspondents in other forums addressing Arduino/AI2 snags also mention 'hardwiring' Uuid workarounds, which is strange because menu-driven variable getter blocks seem to work OK elsewhere.

Hope this helps somebody.