Processing of data received via Bluetooth

437 views
Skip to first unread message

Felipe Muller

unread,
Aug 14, 2016, 6:39:40 PM8/14/16
to MIT App Inventor Forum
I wanted to deal with the data sent via Bluetooth through my microcontroller , the data is sent in this format [ AD1 : value ] [ AD2 : value ] [ AD3 : value ] [ AD4 : value ] I wanted to do an APP that treats these data and exhibited in separate textbox for example value d1 = 1 textbox , value d2 = textbox 2 = value ad3 textbox 3 ad4 value = textbox 4.se someone can help me thank you.

Abraham Getzler

unread,
Aug 14, 2016, 6:58:06 PM8/14/16
to MIT App Inventor Forum
Are you sending all 4 values in one message, or separately?


If sent together, split the message at ']' the produce this list:
 [ AD1 : value 
 [ AD2 : value 
 [ AD3 : value 
 [ AD4 : value  

then walk thry the list with a for each blockb

for each item in messageList
   if CONTAINS(item, "AD1" THEN
      set textbox1 to item 
  ELSE IF CONTAINS(item, "AD2" THEN
      set textbox2 to item 
  ELSE IF CONTAINS(item, "AD3" THEN
      set textbox3 to item 
  ELSE IF CONTAINS(item, "AD4" THEN
      set textbox4 to item 

If you want to show just the value, take an extra step
and split the text value at ":" and select list item 2 from the resulting list,
at each text box assignment.

My FAQ:

ABG

   

Felipe Muller

unread,
Aug 14, 2016, 7:53:16 PM8/14/16
to MIT App Inventor Forum
The data is sent as well , first given [ AD1 : value ] given second [ AD2 : value ] and so on.
I do not know which block use to do this I am layman in appinvento if you can give me an example so that part, bluetooth connection I 've ever done .

Ghica

unread,
Aug 15, 2016, 4:28:53 AM8/15/16
to MIT App Inventor Forum
Felipe,
It is not clear from your post whether you were able to receive the data at all. Do you have the connection set up? If yes, show your blocks.

Otherwise:
If this is your first App Inventor app, try to learn some App Inventor first:

Learn App Inventor
http://appinventor.mit.edu/explore/ai2/tutorials.html
Taifun's top 5 tips to learn AI2
http://puravidaapps.com/learn.php

Then, search for examples in this forum how you can connect with a BT, there are plenty. Come back here if you have questions.

Then, as ABG indicates, use the text blocks, for example one of the split blocks, to split your text and find your values.
Cheers, Ghica.



Abraham Getzler

unread,
Aug 15, 2016, 12:13:28 PM8/15/16
to MIT App Inventor Forum

Felipe Muller

unread,
Aug 16, 2016, 8:54:13 PM8/16/16
to MIT App Inventor Forum
Bluetooth connection and already have so need to treat data received and exibilos separately in textbox .
a.png

Abraham Getzler

unread,
Aug 16, 2016, 11:01:58 PM8/16/16
to MIT App Inventor Forum
In the Clock.Timer block, you need  TWO  guards on the assignment to the label:

If ...Connected THEN
   IF ....BytesAvailable > 0 THEN
      assign input data to label


The purpose of this is to leave the label untouched if no data is incoming.

ABG

Felipe Muller

unread,
Aug 17, 2016, 6:38:31 AM8/17/16
to MIT App Inventor Forum
But it's not about connecting I want to know .
I need to divide the data I get from the microcontroller via Bluetooth [ AD1 : value1 ] [ AD2 : value2 ] [ AD3 : value3 ] [ AD4 : value4 ] [ AD5 : value5 ] , and display in separate textbox .
Photo attached .
x.png

Abraham Getzler

unread,
Aug 17, 2016, 12:03:16 PM8/17/16
to MIT App Inventor Forum
http://puravidaapps.com/snippets.php#2parse

Use parameters of "AD1 : " and "]" to get AD1 value, etc.

Felipe Muller

unread,
Aug 17, 2016, 7:22:48 PM8/17/16
to MIT App Inventor Forum
Não sei usar esses comandos se puder me dar um exemplo.

Ghica

unread,
Aug 18, 2016, 12:22:19 PM8/18/16
to MIT App Inventor Forum

Here is an example.
For your convenience I also attach an .aia
There are two global variables: datareceived and dataList. Global dataReceived contains the data as you received it from your Arduino, with an example as you gave it.
The global dataList contains a list of key, value pairs where the keys are AD1 ,AD2 and so on, and the values are of course the values you recieved.
There are two procedures, one is splitMessages, that you could use more generally to slit a string with start and end delimiters, the other is just split, to split each message in a key value pair. This one is a bit tricky, because there are blanks before and after the keys, which must be trimmed.
After the key, value list is built, you can easily set the labels by using the look up in pairs list block.
Have fun with it, and promise me to study lists and text blocks. There are enough tutorials about both.
Cheers, Ghica.


split_data.aia

Felipe Muller

unread,
Aug 19, 2016, 8:01:46 PM8/19/16
to MIT App Inventor Forum
I made some modifications to get by bluetooth and does not work , it might help me Ghica
blocks.png

Ghica

unread,
Aug 20, 2016, 9:01:42 AM8/20/16
to MIT App Inventor Forum
Of course it does not work. Think!
You call showData before you are connected to your BT device and once you receive data you never call showData anymore.
move the call to showData to be the last block in the if in the clock.Timer block and try again.
Cheers, Ghica.

Felipe Muller

unread,
Aug 20, 2016, 10:25:56 AM8/20/16
to MIT App Inventor Forum
Now it works but carries the first sequence and does not update values ​​.

Ghica

unread,
Aug 20, 2016, 11:14:57 AM8/20/16
to MIT App Inventor Forum
Are you sure that you get the values more than once?? Or maybe the values do not change very much?
Please show your updated blocks!
To check that really something happens you can click with your right mouse button one one of the get global dataReceived blocks, and choose DoIt from the context menu. What do you see? Do the values really change? Try again after a while.
Cheers, Ghica.

Felipe Muller

unread,
Aug 20, 2016, 11:20:30 AM8/20/16
to MIT App Inventor Forum
Os valores são obtidos só uma vez e não mudam por que será.

Ghica

unread,
Aug 20, 2016, 11:32:17 AM8/20/16
to MIT App Inventor Forum
Please answer my questions, otherwise I cannot make progess on solving your problem.
Cheers, Ghica.

Felipe Muller

unread,
Aug 20, 2016, 11:48:06 AM8/20/16
to MIT App Inventor Forum
Os valores não atualizam ele carrega os valores uma vez só ,testei em outro app e atualiza os valores normal.
outra coisa traduzindo para português não entendo as coisas que você  fala.Desculpa.

Felipe Muller

unread,
Aug 20, 2016, 12:03:57 PM8/20/16
to MIT App Inventor Forum
blocks(3).png

Ghica

unread,
Aug 20, 2016, 12:15:26 PM8/20/16
to MIT App Inventor Forum
Sorry, I cannot answer you in Portugese. I will try again.

Your blocks are fine now, but there may be a timing issue.

1. Try to debug the data that you receive. There are many ways to do this. Easiest is to use DoIt. Click with your right mouse button on a get global dataReceived block. For example in the split procedure. Choose DoIt in the context menu that you get and see what the value is in global dataReceived. Does it change? Do you get complete messages?
2. Instead of using DoIt, you could make a new label in the design and set it to global dataReceived, just after you received the data in the Clock1.Timer event block
3. What is the timer interval for the clock that you set in the designer? And what is the delay that you use in the Arduino?
4. Can you show the sketch (the program) in your Arduino?
Cheers, Ghica.

Felipe Muller

unread,
Aug 20, 2016, 12:42:06 PM8/20/16
to MIT App Inventor Forum
Testo no meu celular como faço para depurar.
Na verdade uso um pic 16f688 e manda em 1000ms e no projeto timer 1000ms
Se eu colocar para aparecer só em um label aparece tudo certinho aumentando.

No pic uso assim:
uart1_write(valor1);
uart1_write(valor2);
delay_ms_(1000);
valor1++;
valor2++;
so para testar.
Simulação.png

Felipe Muller

unread,
Aug 20, 2016, 12:51:03 PM8/20/16
to MIT App Inventor Forum
Tenho outro projeto que recebo em um label e aparece os valores sendo incrementados normal.

Felipe Muller

unread,
Aug 20, 2016, 1:00:42 PM8/20/16
to MIT App Inventor Forum
Não atualiza e da erro.
SC20160820-135629.png
SC20160820-135648.png

Ghica

unread,
Aug 20, 2016, 3:13:03 PM8/20/16
to MIT App Inventor Forum
From your last screenshot I see what the problem is: You get many messages within one timer interval, therefore your list is not a look-up list and you get the error message.
I need some time to think about what the best way is to solve this. Be patient.
Cheers, Ghica.

Felipe Muller

unread,
Aug 20, 2016, 3:44:54 PM8/20/16
to MIT App Inventor Forum
ok Fico no aguardo.Obrigado

Ghica

unread,
Aug 20, 2016, 4:14:15 PM8/20/16
to MIT App Inventor Forum

Thank you for waiting!
The solution needed to be rather different because I could not use a look-up table.

What happens now, is that the string that is received is split into the messages and then for each element in the list, the corresponding label is set. If the same key is found a second time, the previous value is updated.
Some points to note:
  • I renamed lblLabel2 to lblData2, whic fits better with the other names.
  • showData does nothing now, it only calls split. Therefore you could delete showData and call split directly.
  • You do not need global dataList anymore, you can delete it.
  • You can use my .aia via the backpack again. Be careful with the Screen1.Initialize and the call to showData.I
I hope it works now! Cheers, Ghica.
split_data_v02.aia
Message has been deleted
Message has been deleted
Message has been deleted

Felipe Muller

unread,
Aug 20, 2016, 8:18:14 PM8/20/16
to MIT App Inventor Forum
Funciona bem so que da um erro.
Message has been deleted
Message has been deleted

Ghica

unread,
Aug 21, 2016, 1:50:31 PM8/21/16
to MIT App Inventor Forum
Felipe,
I am confused. My translation says: It works well so that's a mistake. Which means: it is a mistake that it works??
But I assume there is no error anymore.
Cheers, Ghica.
Message has been deleted

Felipe Muller

unread,
Aug 21, 2016, 6:56:36 PM8/21/16
to MIT App Inventor Forum
Funciona e recebe os dados certo só que fica aparecendo essa mensagem.
não consigo anexar a foto da mensagem.
Message has been deleted

Ghica

unread,
Aug 22, 2016, 11:59:58 AM8/22/16
to MIT App Inventor Forum


Filipe,
The error message happens bacause sometimes you receive an empty list.
Change your blocks as in the picture.
Cheers, Ghica.


Felipe Muller

unread,
Aug 22, 2016, 9:48:44 PM8/22/16
to MIT App Inventor Forum
Continua dando erro.
blocks(7).png
SC20160822-202618.png
SC20160822-202705.png

Ghica

unread,
Aug 23, 2016, 11:55:49 AM8/23/16
to MIT App Inventor Forum
Felipe,
I think that your device sometimes sends messages in some other form than [ key : value ]
What you could do is test if the list you initialize (keyValuelist) has a length of 2, if not, just skip the message.
If that does not help, it maybe that the Arduino sends messages so fast that App Inventor cannot handle them in time. What you could do is disable the clock in the Clock.Timer event block and enable it again after you processed the messages. But first try the length check.
Cheers, Ghica.
Reply all
Reply to author
Forward
0 new messages