Hm-10 sending string - Problems -

Hi!
This is my first post! I’m using MitApp for a personal project! It’s a great tool! :star_struck:
Since some days I have problems with received data from my Arduino (with an HM-10 module) to my App.
The format of string sending from Arduino is like this:
Bxx.x xx.x 0 0
And:
Axx.x xx.x
Where X are numbers and both are ending with ‘\n’ at the end of string.
I have noticed that the string with B, some times appears A at his end…
I’m very confused… what happens? Why does this happen sometimes and not always?
On the other hand, I think I don’t understand the block .StringsReceived…
How the strings received should they be handled?
Is the time between receptions of string important? I have added a time between them near to (delay 50).
Any suggestion?
Thanks in advance!!

Hello Javier

We can’t help without seeing your App Inventor Blocks (right-mouse menu in the blocks work area) and Arduino Script.

For good measure, also include your source code of the sketch.

I can’t locate the prior discussions of this technique, so I will summarize it here from memory
(based on discussions with Gerard Bucas, as I recall) …

BLe chops up its data stream into short packets. The When Strings Received event receives a list of packets, that have to be reassembled sequentially (by text JOIN) into a global buffer variable (text), which should be scanned for the \n message delimiter.
For each item in the received list of string packets, JOIN it onto the end of the global buffer.
If a \n is found in the buffer, split it at the first \n, process the first part as a complete message, then set the buffer to the contents of the second part.

Hi ABG!
Thanks for your reply!
I'm going to post the most relevant part of the code (in Arduino):

//Definitions
float Ti;                           // Temp
float Hi;                               // RH
float Te;                           // Temp
float He;                               // RH
int K=273;

float Min_Value;

unsigned long previousMillis_EM = 0;            //
const long intervaloEM = 100;                   //refresh time

unsigned long previousMillis_B1 = 0;           //
const long intervaloB1 = 155;                  //refresh time

//Block inside of void loop()

  if (millis() - previousMillis_EM >= intervaloEM){            //condition for  send string from Arduino to MitApp 
    previousMillis_EM = millis();
    HM10.println(String("A"+String(Te-K, 1)+" "+String(He, 1)+'\n')); //"Axx.x xx.x" 10 < 18
  }

  if (millis() - previousMillis_B1 >= intervaloB1){         //condition for send string from Arduino to MitApp 
    previousMillis_B1 = millis();
    HM10.println(String("B"+String(Ti-K, 1)+" "+String(Min_Value, 1)+" "+String(flag_state_T)+" "+String(flag_state_H)+'\n')); //"Bxx.x xx.x 0 0" 14 < 18
  }

The code in MitApp is:

Also I saw that if I change the value of "intervaloEM" to the "intervaloB1", in the string with A, some times appears B at his end…So the value of the time between them is important.

Do you have some sample of that??

I used the the next example " Arduino Sketch. Example Project Part 3: Complex control codes" in http://www.martyncurrey.com/arduino-hm-10-and-app-inventor-2/ on how to receive that strings
ARD_HM-10_AI2_03_03-584x159
But I don't used the block "for each item in list" because doesn't work for me...
Ok, if you need more data, please tell me! :grin:
Greetings!

See this long thread from the old Google board ...
https://groups.google.com/d/msg/mitappinventortest/fIbcpyqm4bI/bhkSBIHiAwAJ

The reassembly technique evolved as the thread grew.

Regarding your StringsReceived blocks, I can comment better on it with a version of it I can drag into the Blocks Editor ...

Please download and post each of those event block(s)/procedures here ...
(sample video)

Please export your project and post it here.

Hi ABG!!
Wow the thread is very interesting! I can see that many people have that problem. I do not understand how there is no standard tool? I imagine that many developers need to work with long chains, right?
On the other hand, I think that the official information http://iot.appinventor.mit.edu/#/bluetoothle/bluetoothleintro about block .StringsReceived is insufficient. I have wasted a lot of time trying to understand what the problem was, but luckily there is a forum for these problems!
I add the block’s images:

blocks(2)

blocks(3)

And the project BLE_Test.aia:
BLE_Test.aia (177.6 KB)

Thanks ABG!! :+1:

Here is a revamped framework, which you can customize in the handleA and handleB procedures and extend further with your own message types.
I do not advise touching the other parts, because they require a knowledge of lists that I have not seen in your code yet.
BLE_Test_ABG.aia (179.2 KB) handle

tail

All blocks are draggable into the Blocks Editor.

WooW ABG! Thanks so much! you are very kind! :+1:
The code works nicely! I’m going to use this criteria for the rest of the code!
Thanks so much ABG!
Greetings