Difficulty sending more than one variable from a Microchip microcontroller using BLE (HM-10) and MIT APP inventor

I am having difficulty sending multiple variables from a Microchip PIC microcontroller using BLE (HM-10) only one value is shown. Any help is appreciated.

I have used the following forum link to send two strings to an Android APP:

Get data from dht22 with hm10 - MIT App Inventor Help - MIT App Inventor Community

and I get the following error when I run my software...
Runtime Error
Select list item: Attempt to get item number 2 of a list of length 1: ["150 "]
Note: You will not see another error reported for 5 seconds.

Here is the project:
Swirl_Help.aia (198.3 KB)

Here is the microchip code:
printf("%d\n",PanTempSetpoint);
printf("|");
printf("%d\n",PanTempSetpoint);
printf("\r");

I see five problems so far:

  • You send the same variable twice
  • you do not force them to arrive together in the same message
  • You do not take into account that stringValues is a list and fail to select item 1 from that list
  • You assume that your incoming message will have a '|' in it and text on both sides
  • You fail to take into account that length of list would be less than 2. Add an if/then test.

Thank you for your help...
I believe both values are now being written to the same label
I received the following error

Runtime Error

Select list item: Attempt to get item number 2 of a list of length 1: ["["150 "]"]
Note: You will not see another error reported for 5 seconds.

Microchip code:
printf("%d\n",PanTempSetpoint);
printf("|");
printf("%d\n",ProbeTempSetpoint);
printf("\r");

See attached project

Swirl_fix2.aia (198.2 KB)

Try this (note, BLE normally uses a different method of notation, the method that you have in your App Code, but your microprocessor code is like the Serial code used with Classic Bluetooth, which does not use Characteristics etc):

printf("%d",PanTempSetpoint);
printf("|");
printf("%d",ProbeTempSetpoint);
printf("\n");

In the App Block code you are registering for strings but the microprocessor code is sending floats (%d).

Upload the microprocessor file.

If you continue with the strings approach, this would show what is arriving.


Swirl_fix2 (1).aia (198.6 KB)

There wouldn't need to be a split, just separate the values with a comma in the microprocessor code, but we really need to see the microprocessor file to know what is happening.

Thank you
Microcontroller code for Chris:
main.txt (15.6 KB)
The %d designation is an integer
Also Chris... the values do report an actual true value in the label ... the challenge here is that both values appear rapidly one over the other overwriting themselves in the same label

ABG:
the string label is showing the exact content of LblTime as described above for Chris

... so you could try my code above.

I'm away from my PC for a few hours now.

Chris... OK... I didn't see the changes that you made.I tried the print statements that you sent now... There were some bothersome 's that are now missing, but both values are still printing in the same label

image

Try removing one of the \n from those two places where it appears in your printf calls.

Those might be splitting your messages into two consecutive messages.

Remove the one before the "|".

ABG...
That did not solve it...
I decided to simply write text:
printf("Pan");
printf("|");
printf("Probe");

This did seem to split the to strings into the correc labels... is this a hint?

That's useful. So try printf on the numbers, without any \n at all, or that \r.

Didn't help..

Just trying more things....

This did the same thing (overwriting string/number in the first label)

PanTempSetpoint=123;
ProbeTempSetpoint=456;
printf("Pan");
//printf("%d",PanTempSetpoint);
printf("|");
printf("%d",ProbeTempSetpoint);

This separated the values into the proper labels:
PanTempSetpoint=123;
ProbeTempSetpoint=456;
//printf("Pan");
printf("%d",PanTempSetpoint);
printf("|");
//printf("%d",ProbeTempSetpoint);
printf("probe");
//printf("\r");

Also, this presented the number|text properly in StringLabel that you added.

Try adding a println() after the last printf.

That should send a Line Feed, and hopefully trigger a send.

So that code nearly matches my suggestion, except that the last line needs to indicate the end of the data and we do not send labels, only the values because of the MTU limit (Maximum Transmission Unit, though depending on the devices, the MTU can be enlarged).

Points of interest:

For Classic Bluetooth Projects I like to use the bar char (|) as the value separator because of it's prominence and the fact that it isn't likely to be the member of a value, even if the value is a string.

However, the BLE stream blocks expect a comma (,). If a comma is used, there is no need to split the data into a List, it already is a List.

Big caveat!! The microchip Code is not using BLE.

The values your code collects are from sensors? Surely then the sensor values are floats?

EDIT: I see from the Microchip code that all values are integer, so this is what App Inventor BLE expects:

      printf("%d",PanTempSetpoint);
      printf(","); 
      printf("%d",ProbeTempSetpoint);
      printf("\0"); null

This results in a string of values. e.g.
PanTempSetpoint=123;
ProbeTempSetpoint=456;

String output like this: 123,456

Received like this:

Snap1

or better:

You might consider building a new App Inventor project using Classic BT Blocks if the BLE Blocks fail when the App is run as an APK, but it looks like you have proved it to be OK.

Android requires certain permissions (Google's security measures) depending on the Android version of your device. You can find advice about that on this forum. Suffice to say, no permissions, no cigar.

2 Likes

ABG
tried printf("\r\n"); at the end (carriage return/linefeed) still writing over in same label

Chris..
See "or better:" Blue "length of list list" doesn't connect to ... "if" in the block

also, I have two Android phones Android 14, Android 12... both behave the same with this APP

There is a Math "=" Block that "length of list" is connected to.

Even when run as an APK?

From Evan, regarding Bluetooth Classic permissions:

"for Android versions prior to 12 we are required to declare BLUETOOTH and BLUETOOTH_ADMIN permissions, but on Android 12+ we need BLUETOOTH_SCAN + BLUETOOTH_CONNECT permissions for clients and BLUETOOTH_ADVERTISE permission for the server component."

So BT Classic is similar to BLE except BLE also requires the FINE LOCATION permission.

Chris:
Android 12 will only run in APK... AI companion will not work...
All permissions are allowed for APP on phone
label1 shows values over written just like they were previously in the label..but.. values dont show up at all in label 2,3