[Solved] BLE WriteIntValue to Arduino 101

Good Morning to all,

I have come across Thunkable looking to re-create an already working App I created with MIT App inventor 2 to work with iOS phones.

So far,I have not much trouble and the App is working with a little exception.

The App is a control App for some electronics I have assembled using an Arduino 101 board.

The working of the app is quite simple, it scans looking for BLE devices, then allows the user to connect to the selected device (the Arduino 101 Board)

After this the user can press some buttons that transmit text and the board reads it and do thing, e.g. If you press the “Test” button it will send letter “T” over Bluetooth and the Board will show in the PC serial the settings currently on the board. When the “Default” button is pressed, it will send a “D” and the board will restore its default settings.

This part is working without any problem, here are the block groups comparison between MIT App Inventor and Thunkable:


Another thing the user can do is use a slider in order to select at which angle the board should do some things when tilted, again let me show you the block groups comparison between MIT App Inventor and Thunkable:
Int_MIT_APP

As You will notice, the main difference (I see) is that MIT Inventor BLE module has a way to send the data as an integer value and that eased my code in the Arduino side for interpretation, but Cross Platform does not seems to have it.

My question is the following, I have seen while searching that for the Thunkable Classic, the BLE extension seems to have more BLE options as can be seen in these topics I searched:
When looking for 101 connectivity
When looking for general BLE usage

And I was wondering if these kind of option will be included in Cross Platform.

In case they are not expected, I guess I will have to short out how Thunkable sends the data (My guess is that it is send as text/string) and will need some help on how to transform it to an Integer on the board side, since I am a beginner in programming, any help (sorry for asking for help for Arduino here) will be welcomed.

Thanks for your time reading, regards, Manuel.

Good Morning to all,

I have solved the problem on the Arduino side by converting the string to an appropriate Integer.

Manuel.

1 Like

Thank you so much for the update @manuel.gonzalez19764 - would you mind editing your solution post above to include a code snippet of how you did this, for the benefit of the next person who needs to do this?

Thanks!

Good Morning to all,

First, sorry for the late reply.

On the arduino side, previously I have declared the characteristic that will read the value as a number with and “BLEUnsignedIntCharacteristic” as follows:

BLEUnsignedIntCharacteristic  Numero("1209", BLERead | BLEWrite  );

This does not change at all. Then I defined the event handler in the usual manner, this does not change either:

Numero.setEventHandler(BLEWritten, NumeroWritten);

Is when defining the function for the handler that I change the code from:

    void NumeroWritten(BLECentral& central, BLECharacteristic& characteristic) {
    // Si la central escribe un nuevo valor, entonces...
    Serial.print("Characteristic event, written: ");

    if (characteristic.value()) {       //null pointer check
      numero = *characteristic.value();  //set state to be the value written from the phone/tablet to the Arduino 101
      Serial.println(int(numero));      //print out the integer to the serial monitor
    }
  }

To this:

void NumeroWritten(BLECentral& central, BLECharacteristic& characteristic) {
  // Si la central escribe un nuevo valor, entonces...
  Serial.print("Characteristic event, written: ");

  if (characteristic.value()) {       //null pointer check
    //numero = *characteristic.value();  //I have commented this line and keep it to keep a track of the change.
    const unsigned char *data = characteristic.value(); //this line defines an intermediate variable that in the next line will be converted to an integer.
    numero=atoi((char *)data); //this line convertss the intermediate variable to an integer.

    Serial.println(int(numero));      //print out the integer to the serial monitor
  }
}

I am quite sure that there might be more elegant or better ways to do this, but this one is the one I found and works for me.

Thanks, Manuel.

3 Likes