receive text from arduino via bluetooth and display in two different text boxes

497 views
Skip to first unread message

francesca cavo

unread,
Sep 6, 2016, 2:00:51 PM9/6/16
to MIT App Inventor Forum
Hello,

I am using MIT app inventor for the first time and I am trying to build an app to receive information from arduino (I am using arduino uno board) via bluetooth (I have a HC-06 Module); I want to check two LED and receive different text if they are turned on or off, but I want to display it in two different text boxes. My app is working while I am checking only one LED per time, if I try to check them at the same time I receive the correct text but displayed  randomly in one of the two text boxes.
I have searched in the forum but I couldn't find anything that could help me, so can anyone help me in this matter? I would really appreciate that.

Regards
Francesca

Abraham Getzler

unread,
Sep 6, 2016, 3:05:20 PM9/6/16
to MIT App Inventor Forum
Download an image of your blocks from the Blocks Editor and post it here?
ABG

francesca cavo

unread,
Sep 7, 2016, 5:20:38 AM9/7/16
to MIT App Inventor Forum
when I send string "check_imp" or "check_press" arduino is checking the LED and returns two different text if LED is turned on or off. I have a total of 4 sentences to put in two different tex boxes, but I don't know why when I am checking both of them at the same time is not working.

Thank you
Francesca
connection.JPG
check_led.JPG

Ghica

unread,
Sep 7, 2016, 8:48:02 AM9/7/16
to MIT App Inventor Forum
What is in global impedance and global pressure after a while? Check with DoIt. Are you receiving any data from your Arduino?
Usually it is not a good idea to receive text immediately after you sent a request because the Arduino should be given time to reply.
Cheers, Ghica.

francesca cavo

unread,
Sep 7, 2016, 9:38:10 AM9/7/16
to MIT App Inventor Forum
No I am not receiving any data, I just want to receive a text line after arduino has checked the status of the two LED; it's working if I check one LED per time, if I try to check them at the same time it's not working properly anymore.
How can I check what is in the two variables? thank you.

Regards
Francesca

Abraham Getzler

unread,
Sep 7, 2016, 2:40:22 PM9/7/16
to MIT App Inventor Forum
Take your sending blocks out of your clock timer.
Put them in separate button click events, temp and pressure, separate buttons.
Keep a global variable named TempOrPress, with value either "temp" or "press".
Set its value appropriately in the two buttons, to let you know what to expect from the next incoming message.

In your clock.timer event, do:

If connected then
   if databytesAvailable > 0 then
      if TempOrPress = "temp" then
         take incoming data and put it into temperature label or variable
      elseif TempOrPress = "press" then
         take incoming data and put it into pressure label or variable
      else announce a bug in your app.
  end if
end if

ABG



francesca cavo

unread,
Sep 7, 2016, 3:12:16 PM9/7/16
to MIT App Inventor Forum
Thank you for your help, I will try and let you know if it works.

Regards
Francesca

francesca cavo

unread,
Sep 8, 2016, 11:50:21 AM9/8/16
to MIT App Inventor Forum
I changed the blocks and now it's working, but it checks the value of the LED only when I press the button; is there a way to say to automatically check the status of the LED once the button is clicked?
Here I attach the new blocks.

Thank you.
Regards
Francesca
connection_and_buttons.JPG
clock_timer_event.JPG

Abraham Getzler

unread,
Sep 8, 2016, 1:41:54 PM9/8/16
to MIT App Inventor Forum
You want to start the two devices automatically repeating once a button has been pressed?

Like a reciprocating engine?

Here's how to get both data streams coming, alternating each cycle ...

Create two procedures, ask_temp and ask_press.
Move the two blocks from each of the two buttons (the SEND and the SET GLOBAL) into the corresponding procedure,
and replace the button contents with a call to the appropriate ask_... procedure.
Nothing has changed, this makes it easier to do the SEND and SET elsewhere.

In the Clock Timer block, you have two branches that handle incoming data, temp and press.
After handling temp, insert a block to call ask_press.
After handling press, insert a block to call ask_temp.

This way, you will ask for the other reading immediately after receiving a new reading.
(I left off stop logic, it will run forever.)

Let us know if it works for you?

ABG

francesca cavo

unread,
Sep 9, 2016, 6:25:20 AM9/9/16
to MIT App Inventor Forum
I have a device and I am measuring the impedance and the pressure, each of these parameters are connected to a LED; when these values go under a threshold the LED change status. I don't want to get the values in my app, just want to check the status of the LED and receive a text line for each status, but I want to check automatically once the button has been pressed, because I need a continous monitoring.

I'll try this way and I'll let you know, thank you very much for your help.

Regards
Francesca

francesca cavo

unread,
Sep 9, 2016, 7:50:39 AM9/9/16
to MIT App Inventor Forum
Now it is working, but I still have one question; what if I want to check at the same time? At the moment, when I press "impedance" button is checking one led, when I press "pressure" button is checking the other one but in my case it could be possible that both parameters change in few minutes, so I would like to check both of them without having to choose between them.
I try to explain better: once the impedance button is pressed check the led, but keep checking it even when I press the pressure button (and the app is checking the other led).
Do you think it's possible?

Here attached the new blocks.
Thank you again!!

Regards
Francesca
capture1.JPG
clock_timer_capture.JPG

Abraham Getzler

unread,
Sep 9, 2016, 10:40:01 AM9/9/16
to MIT App Inventor Forum
You missed a critical part of my scheme to rapidly alternate between readings.
As an engine, you are running on only one cylinder at a time.
This will let your engine run on two cylinders...

Switch the calls to ask_imp and to ask_press in the timer IF/THEN/ELSE tree.

This will start a two handed juggling act between press and temp.

ABG

francesca cavo

unread,
Sep 9, 2016, 12:10:42 PM9/9/16
to MIT App Inventor Forum
YES it's working!!! At the end I removed one button, I start with one check and then it is monitoring both of them.

Thank you very much for your help!!!

Kind regards
Francesca

Abraham Getzler

unread,
Sep 9, 2016, 12:34:57 PM9/9/16
to MIT App Inventor Forum
Could you post your completed blocks and sketch?

We get this knd of question a lot, and helps to have a working example.

ABG

francesca cavo

unread,
Sep 9, 2016, 12:57:17 PM9/9/16
to MIT App Inventor Forum
Yes, I just have the last question; in each text box I receive two sentences (when LED is on or off) and I would like to send a sound (alarm) only when one of them appears. How can I do this?
Thank you again.

Francesca

Abraham Getzler

unread,
Sep 9, 2016, 4:26:25 PM9/9/16
to MIT App Inventor Forum
What is it you don't know how to do?

  1. to test a text value?
  2. to combine logic true/false?
  3. to sound an alarm?
ABG

francesca cavo

unread,
Sep 9, 2016, 5:10:35 PM9/9/16
to MIT App Inventor Forum
I don't know how to test a text value and to sound an alarm either; I would like to sound an alarm just when I receive one text value. For example, when I receive the text "Device not inserted" I want to sound the alarm, otherwise no.
Thank you.

Francesca

Abraham Getzler

unread,
Sep 9, 2016, 5:21:48 PM9/9/16
to MIT App Inventor Forum
You are already testing your incoming messages for two values.
Just use the blue mutator to add an extra ELSEIF branch to the decision tree in your clock routine,
testing for that third value.

To play a sound, you will need a short sound file, easy to get over the Internet for free.
Upload it into the Media drawer.
There are two possible sound components to choose from, look around in the Designer.
The Player is for longer sound files.

ABG

Abraham Getzler

unread,
Sep 9, 2016, 5:38:23 PM9/9/16
to MIT App Inventor Forum
Whoops, I looked at your clock timer event and that part of previous post doesn't match what I said.

You will need a new global variable, called IncomingMessage
(because we don't know yet what the message means.)

In the timer event, inside the two if statements that confirm that you are connected and have data available,
set global IncomingMessage to BTIncomingText(bytesAvailabletoReceive)
(From here down, use the global variable instead of asking for the incoming text, you already have it)
if global IncomingMessage = "Device not inserted" THEN
  sound the alarm
;;;same blocks you have now, but using global IncomingMessage ...
IF imp_press = "Press" then
  set textbox_press  to global IncomingMessage
  call ask_temp
elseif imp_press = "Temp" then
  set textbox_Temp to global IncomingMessage
  call ask_press
else
  error


ABG

francesca cavo

unread,
Sep 10, 2016, 8:07:32 AM9/10/16
to MIT App Inventor Forum
I modified my clock.timer event by adding a new global variable; everything is still working except the alarm. 
What have I missed?
Here attached the blocks.
Thank you

Francesca
capture.JPG

Abraham Getzler

unread,
Sep 10, 2016, 6:35:56 PM9/10/16
to MIT App Inventor Forum
The trick here is to make sure you are comparing text EXACTLY to what the other side is sending.
Where is that?
ABG

francesca cavo

unread,
Sep 11, 2016, 1:12:28 PM9/11/16
to MIT App Inventor Forum
I tried with "compare texts" block, but it's still not working. Could you help me to understand my mistake?
Thank you.

Francesca

Abraham Getzler

unread,
Sep 11, 2016, 3:30:58 PM9/11/16
to MIT App Inventor Forum
Post your sketch here.
No way to diagnose without it.
ABG

francesca cavo

unread,
Sep 12, 2016, 6:30:17 AM9/12/16
to MIT App Inventor Forum
Here attached blocks and sketch. val and val2 are reading the status of the two LED.
Thank you

Francesca
capture2.JPG
sketch.JPG

Ghica

unread,
Sep 12, 2016, 8:48:57 AM9/12/16
to MIT App Inventor Forum
You test for "Device NOT inserted", but the Arduino attaches two !! to it, therefore the texts never compares equal. You could use the contains text block instead.
Cheers, Ghica.

Abraham Getzler

unread,
Sep 12, 2016, 12:00:37 PM9/12/16
to MIT App Inventor Forum
Also beware of the Blocks Editor's tendency to eat trailing blanks in text blocks.
That can kill a text comparison.
ABG

francesca cavo

unread,
Sep 13, 2016, 7:23:06 AM9/13/16
to MIT App Inventor Forum
Here attached the final blocks, everything is now working!! 
Thank you all for your help!

Regards
Francesca
final_blocks.JPG
Reply all
Reply to author
Forward
0 new messages