How to send a value/ text through BluetoothLE to Arduino

Hi all,

[If this ought to be a new thread, please just let me know - but it seemed to follow on logically enough from the topic in hand...]

I'm working on a very similar problem, and having issues with the WriteBytesWithResponse block. It works fine with sending a single byte (with the "values" input set to either a number or a textbox input consisting solely of numbers).

However, it does not appear to work with lists of values as the help text claims it ought to:

  • with a list of numbers, only the first byte is sent (and the value received by the Arduino has length:1)
  • with a list of textbox entries I get the following error:
    image

For completeness, here's a set of different tests I've tried, and the results:


Works - one byte sent, one byte received.


Works - one byte sent, one byte received.



Doesn't work - only one byte sent, one byte received - should be four.



Doesn't work - throws error as above.

Any pointers to what I might be doing wrong would be much appreciated.

Cheers,
Jo

Experiment 4 is doomed to fail because the initialization of the list happened before you had a chance to enter numbers into the text boxes.

Experiment 3 might work if you feed the values directly from the make list block, without the global variable. Check the tool tip for the WriteBytesWithResponse block for examples.

Thanks for your pointers, ABG.

Experiment 3b:


Does exactly the same as Experiment 3: the first byte comes through okay, the others don't (i.e. I get 5 0 0 0 instead of 5 6 7 8).

I've also done a bit or experimenting with the .BytesWritten block, which is giving the correct results, which is making me wonder it's more likely that I've done something daft at the Arduino end - the relevant block of code is here, where bytesInBuffer is a 4-element byte array:

//if bytes received, print them out on serial monitor
  if (receiveBytes.written()) {
    Serial.println(receiveBytes.valueLength());
    receiveBytes.readValue(bytesInBuffer, sizeof(bytesInBuffer));
    for (int i = 0; i < sizeof(bytesInBuffer); i++) {
      Serial.print(bytesInBuffer[i]);
      Serial.print(" ");
    }
    Serial.println();
  }

This returns 1 (rather than the expected 4) for the valueLength, and [5 0 0 0] rather than [5 6 7 8] for the contents of bytesInBuffer. Any suggestions here much appreciated.

Experiment 3c:
I also tried repeating 3/3b with the .WriteBytes (without response) block which also, interestingly, returns the correct result to bytesWritten, but I get nothing at all at the Arduino end.

Experiment 4b:


Gives the same error as Experiment 4.

So, a few questions (which I have searched for fairly extensively without much joy so far - please point me towards other answers/reading if these are answered elsewhere):

  1. What is the difference between the WriteBytes and WriteBytesWithResponse blocks? Both seem to return a response to bytesWritten, even though this is only detailed for the latter block in the documentation. However, these blocks give different results at the Arduino end.
  2. Is WriteBytesWithResponse instead expecting an external response? If so, what?
  3. Is there a way to cast a number stored as text to a byte? (My (possibly faulty) understanding was that AI2 is supposed to handle casts like that by itself...?) Or do I need to find a way of sending it in another form and translate it at the receiving end?

Again, any pointers gratefully received.

Experiment 4b will also fail because the write bytes block expects an integer, not a string. The extension does not convert string to integer, you got this information in error. To convert a string to an integer, use the math block multiplication and multiply each text box by 1. This will convert a string to an integer.
@ewpatton Could you do that if we set the textbox to NumbersOnly, then the textbox would return numeric values ​​instead of a string?

Thank you @Patryk_F, that's helpful.

In case it is helpful to anyone else, another issue I discovered on the Arduino side, is that I had it set to initialise a BLEByteCharacteristic (which is only good for sending a single byte at a time) instead of a BLECharacteristic. To send 4 bytes, I had to set it up like this:
BLECharacteristic receiveBytes("(UUID)", BLERead | BLEWrite, 4);
I now have a series of 4 bytes transmitting correctly using the setup from Experiment 3/3b above.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.