Skip to first unread message

Pierre TESSIER

unread,
Apr 3, 2017, 5:36:28 AM4/3/17
to MIT App Inventor Forum
Hello,
Just a simple question about the possibility to include a pause in a clock timer.
Effectively i send a command to arduino et i must wait the response before continuing.
Is there a command or a method to do that. Or a method to include a time to pause.
I looked at the forum but i found nothing about this question. Perhaps i have noot seen.
Thanks if someone can give me information
Pierre

Taifun

unread,
Apr 3, 2017, 10:27:42 AM4/3/17
to MIT App Inventor Forum

Ghica

unread,
Apr 3, 2017, 10:59:03 AM4/3/17
to MIT App Inventor Forum
Use a Clock. At each Clock.Timer event, test if you have recieved your message, and if yes, process it.
If you need more help, post your blocks and your Arduino sketch.
Cheers, Ghica.

Pierre TESSIER

unread,
Apr 4, 2017, 6:20:26 AM4/4/17
to MIT App Inventor Forum
Thank you both.
I join blocks and skech arduino to see.
So, because you propose me i join concerned blocks and sketch arduino.
First thing it's ok if i ask a single mesure. Ex.: if sms =31, the return is 33 (because for test i simulate variable t1=33).
Also what i am doing, because i use 2 arduino:
         -i send the SMS received to serial1 of the  Domo BT arduino and this arduino resend, by serial3, to Domo hangar. 
         - it's why i prefer adjust a delay between call send and get response on android app
 Summary:    sms send to serial1 of the first arduino (arduino Domo BT)>>>resend by serial3 to second arduino (Domo hangar)  >> received on serial3 of Domo hangar

If you have time thank you to say me your remarks and see what i can do to include a pause between send byte et get response or another solution.
Have a good day and thank you again

 
 
android prog.doc
Domo_BT_android_essai_temp_ok_220317.ino
Domo_hangar_essais_240317.ino

Ghica

unread,
Apr 4, 2017, 10:15:29 AM4/4/17
to MIT App Inventor Forum
If I understand this well:

1. The AI app sends a request to Domo_hangar
2. Domo_hangar send a message to Domo_BT
3. Domo_BT returns a message to Domo_hangar
4. Domo_BT returns a message to the AI app.

You did not include the code for connecting to Domo_hangar, but I assume it is working.

Before calling Receive text, you should test whether there are any bytes to receive.

If you run your app (using the Companion), what do you see in rep_arduino? Is it what you expect?

You might want to do some buffering to be able to receive complete messages, or make sure that you get a complete message by using -1 as the number of bytes to receive.

text ReceiveText(number numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.
It may be necessary to adapt the delimiter byte value, but I believe that the default is usually ok.
Cheers, Ghica.

Ghica

unread,
Apr 4, 2017, 12:30:02 PM4/4/17
to MIT App Inventor Forum
Please do not reply to me privately!
You wrote:
Thank you for your answer.
For one of the read to do (ex: Temp t1) i send 51 to Domo BT that sees that this code is to redirect to Domo hangar.
Domo hangar executes 51 and return to Domo BT and this last send the response to AI app.
If i ask only 1 read it's ok (code 31); if i ask many reads (4 in my case) i read nothing (t1, t2, t3 t4).
What you recommend is very interesting, i don't know at this time how to do because i don't see the blocks which you speak , i have a review to do.
Too i can't put the android app waiting response all the time because if there is a trouble on an arduino card my app is blocked.
Thanks
Pierre
>>

What blocks are you talking about that you cannot find? All BluetoothClient blocks are described here: http://ai2.appinventor.mit.edu/reference/components/connectivity.html#BluetoothClient
And, an App Inventor app cannot run in the background. Therefore it must be active and you must connect to your BT device every time you start the app.

You might want to look at some Bluetooth tutorials for which you can find links in the FAQ: https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!category-topic/mitappinventortest/2cd6Uz__xA0
Cheers, Ghica.

Pierre TESSIER

unread,
Apr 5, 2017, 8:11:36 AM4/5/17
to MIT App Inventor Forum
Ok Ghica,
Thank you for your explanations.
So i have to see all the possibilities of the BluetoothClient blocks and clock timer blocks.
At this subject: is there a possibility during execution of a block timer event to redirect to another clock ?.
Ex.:when  clock5 timer event do instruction 1, instruction 2, go to clock timer 6, and when instructions of clock6 timer finished, come back to ckock5 event.
I think no but it's for confirmation. If i put clock6 timer false during execution of clock6 event  i think it's effective at the end of the instructions.
To confirm only. Thanks
Salutations

Ghica

unread,
Apr 5, 2017, 1:29:41 PM4/5/17
to MIT App Inventor Forum
Pierre,
It seems to me that you need to review how a clock timer works. I just cannot imagine that you would need 6 clocks in an app. The idea of an event block, such as the clock.Timer event block is that the events occur asynchronously. Like if you would set 6 alarm clocks, they would sound alarms at six different times, depending on the time you had set for each.

If you have common code to execute, create a procedure and call the procedure from wherever you need it.
Cheers, Ghica

Pierre TESSIER

unread,
Apr 8, 2017, 7:15:27 PM4/8/17
to MIT App Inventor Forum
Hello Taifun,
I have problems with timer events.
I want do a wait and it's not working.
Is it possible with payment to do me a wait ?.
I join you the code . It's to intercalate a procedure between BluetoothClent Send Number and BlueTooth Client ReceiveText. In DomoDisplay Timer.
To know that if i code to read only one variable from arduino it's ok.
It will be too a first step to learn Timer event blocks.
Thank you to say me.
Have a good day
Pierre
Scan Taifun.doc

Taifun

unread,
Apr 8, 2017, 8:04:04 PM4/8/17
to MIT App Inventor Forum
why do you use a clock component to send something?
currently you are sending something, which is dependent on some textbox text?
what about using a button click event instead?

and for the receiving part, there you should use a clock component, see also 

the marked blocks are all the same, you could simplify that, use them only once, keep them in the clock timer event and move the sending part into a button click event...
Taifun


Pierre TESSIER

unread,
Apr 9, 2017, 7:57:40 AM4/9/17
to MIT App Inventor Forum
why do you use a clock component to send something?
For the other messages i use these blocks with a clock and it's working ok but with a only one request (see blocks joined ).

currently you are sending something, which is dependent on some textbox text?
what about using a button click event instead?
I send after receiving a SMS

the marked blocks are all the same, you could simplify that, use them only once, keep them in the clock timer event and move the sending part into a button click event...
For the blocks i have globatRep1Domo, globalRep2Domo, ..... but it's true i can arrange.
This will be to do after it will be ok for the solution to read many variables on arduino and send a SMS to see all these variables together. This after receiving , in my example, the SMS with the number 35.
Arduino sends back temp1, temp2,... but impossible to capture the values in android: i have temp1=0 or nothing and tle same for the other variables.

When android received SMS  number "35" from any phone , then it sends 51, then 52,..... to arduino;  arduino returns temp1 for 51, temp2 for 52,...  I group (or rather i try to group) temp1, temp2,... and return a SMS whith all these values to the calling phone.
Pierre



Receive and send SMS.doc

Taifun

unread,
Apr 9, 2017, 10:58:09 AM4/9/17
to MIT App Inventor Forum
currently you are sending something, which is dependent on some textbox text?
what about using a button click event instead?
I send after receiving a SMS
in this case you should use the Texting.MessageReceived event

For the blocks i have globatRep1Domo, globalRep2Domo, ..... but it's true i can arrange.
you also can set these values in the Texting.MessageReceived event

Taifun

Pierre TESSIER

unread,
Apr 9, 2017, 12:39:47 PM4/9/17
to MIT App Inventor Forum
in this case you should use the Texting.MessageReceived event
Ok, it's a mistake from me . I have to use, effectively, this event.

For the blocks i have globatRep1Domo, globalRep2Domo, ..... but it's true i can arrange.
you also can set these values in the Texting.MessageReceived event
Ok but like i send many values to arduino for the same SMS received (35) i have to pause between sending 51...52....to arduino and there i have a problem.
I must send 51 - wait for receiving response - after response arduino send 52-  and so on
Like it is impossible to synchronize timing between android and arduino (and i think it's not the solution) i prefer test if i have received a number from arduino and, if yes , sending the following : 52 or 53....
If response not received after a given time i put response to 0 or null.
Pierre




Taifun

unread,
Apr 9, 2017, 1:09:54 PM4/9/17
to MIT App Inventor Forum
I must send 51 - wait for receiving response - after response arduino send 52-  and so on
this is an asynchronous logic, see the file by file example how to do it

note: that example downloads several files from the internet like this: download a file, after file has been downloaded and there are more files to download then download next file, after that file has been downloaded and there are more files to download, then download next file, etc.

this is something different, but the asynchronous logic is the same you need for your app

Taifun

Ghica

unread,
Apr 9, 2017, 3:07:49 PM4/9/17
to MIT App Inventor Forum

@Taifun,
I respectfully disagree. Because there is no when BT-reponse received event, you have to use a timer.
@Pierre,
Look at my blocks for an example of how it could work. What is missing is that you do not want to increase the number you send forever, so maybe you have to add a check for the highest number and when you have received a response for that, stop sending.
Cheers, Ghica

Ghica

unread,
Apr 9, 2017, 3:58:48 PM4/9/17
to MIT App Inventor Forum
Maybe I should add a little explanation.

The Clock.Timer event occurs every second (or whatever other interval you set it).

When that happens, you check whether there is a message to receive.
  If yes, receive it and send the next message
  If no, keep waiting until the next clock.Timer event

The check in the beginning if VarDomoCycle = 0 is there, because you should send the first message without waiting for a message from the Arduino.

Something else you could add to do something if the Arduino does not respond: count the number of timer events and if you have more that, say, 10, give up.
But I think this should not happen, because you should code your Arduino to always respond and maybe do a check to see if the BluetoothClient is still connected.
Cheers, Ghica.

Pierre TESSIER

unread,
Apr 9, 2017, 4:10:47 PM4/9/17
to MIT App Inventor Forum
Ok Ghica
But i need a pause between BluetoothClient1 Send and BluetoothClient1 Receive.
Because arduino takes a while before returning his response (between 1 to 2 sec).
And if i do BluetoothClient Receive immediately the arduino response that is read will be always 0. 
My problem is that i have not seen how to do a timer between Send to arduino and Receive from arduino
Pierre

Ghica

unread,
Apr 9, 2017, 4:19:39 PM4/9/17
to MIT App Inventor Forum
Pierre, look better.
You do first a receive and then a send in the clock.timer processing (except for the first send). But, if there is nothing to receive, you do nothing, which means you wait until the next clock event. And then check again if there is something to receive. If not, wait, if yes receive and then send. And so on.
The effect is exactly what you want.
Cheers, Ghica.

Pierre TESSIER

unread,
Apr 9, 2017, 5:28:49 PM4/9/17
to MIT App Inventor Forum
Thank you Ghica but il seems ( if i have well understood) there is a problem because i can continue to send in the SendNext procedure whitout receiving in the BT Receive if BytesAvailableToReceive is always = 0 when the timer is reading. If the coincidence makes that when i read the result is 0 i never read the arduino response or i read the response to another send.
Because what i wish is that i send "51" to arduino i don't want to send someting else as long as i have not the response for 51.
When i got the response to 51 then i send 52 but only at this condition.
It's to clarify my problematic
Pierre



Ghica

unread,
Apr 9, 2017, 6:17:33 PM4/9/17
to MIT App Inventor Forum
Pierre,
BytesAvailabletoReceive > 0 means that you have received a response from the Arduino. And again, in my example, you will not send anything if you did not receive something before.
The trick is that the Clock will go off repeatedly and you only continue sending if you have received a response, else you wait until the next clock tick, or the next, or the next...
Look again, until you understand it.

Look at what happens:

-- Timer goes of
    If VarDomoCycle is 0 you send the first message and you set VarDomoCycle to 1.
   
-- Timer goes off again
    VarDomoCycle is > 0 now
    Therefore you test if there is a message returned from arduino (BytesAvaialbletoReceive >0)
    if not, do nothing

-- Timer goes off again
    VarDomoCycle is still  > 0
    Therefore you test if there is a message returned from arduino (BytesAvaialbletoReceive >0)
    if yes, receive the message and send the next one. Set VarDomoCycle to 2

-- Timer goes off again ....

What is so difficult about it?
Cheers, Ghica.
   
   

Pierre TESSIER

unread,
Apr 9, 2017, 6:39:39 PM4/9/17
to MIT App Inventor Forum
Ghica,
I was watching all your explanations and , at last, i was understanding your method.
Your last publication, very detailed, confirm that.
It's ok now and i thank you for your patience and all your informations.
Thank you again, i have learned a lot of things : Solution of my problem and the way that you work with AI.
Pierre

Ghica

unread,
Apr 10, 2017, 1:00:27 AM4/10/17
to MIT App Inventor Forum
Great! Cheers, Ghica.

Pierre TESSIER

unread,
Apr 10, 2017, 12:01:23 PM4/10/17
to MIT App Inventor Forum
It's working now with a little problem i have to correct.
When i mesure the second variable i have the first + the second
First mesure : 33 ok;  second mesure =3322  (i wait 22);  third mesure =2218 (i wait 18) and so on.
My question: is there a possibility to clear reception after call ReceiveText
I have see nothing about that.

BT Receive.doc

Ghica

unread,
Apr 10, 2017, 12:56:09 PM4/10/17
to MIT App Inventor Forum
I cannot tell from your picture. If you receive all available text, the message is cleared, but you could have asked for two things, or your Arduino sketch sends more than it should, or you should make your clock timer interval shorter, or?
Cheers, Ghica

Pierre TESSIER

unread,
Apr 10, 2017, 2:31:15 PM4/10/17
to MIT App Inventor Forum
Ghia,
Once again you are right. I thought that, maybe, the available text was cleared when another message was coming or after a delay.
Your response is clear with this explanation:  If you receive all available text, the message is cleared.
Now it will work.
Thanks
Pierre

Reply all
Reply to author
Forward
0 new messages