Use receiving Bluetooth text to trigger sending bluetooth text

Greetings all,

I am in need of help.

What I am trying to do:

When button is pressed in MIT APP, then send text over BT to Arduino. Arduino does it's thing and when done send a new text over to the APP via BT. When the APP receives this text it uses it to trigger the next event which should be another command going back to the arduino and so forth.

I am able to connect to the BT device, to send text which triggers the arduino code and then send back a text which also works because I can see the incoming string in a Lable box and on the notification pop-up.

I want to use the "done" from the arduino to trigger the "dismissProgressDialog" on the App.

Anyone got some ideas because being able to use the incoming texts to trigger other commands or color changes of arrangements would be the last step to my project.


Thank you all a million who can help!!!

69f059daeae84c541bed2b8cab4a3d2ce4fe9637_2_530x500

Don't do a second read from BlueTooth after the first read, just handle them one at a time as they arrive. Otherwise you lose messages.

Test for 'done' in the process procedure, and close the notifier there if found.

P.S. Make sure you have BlueToothClient Delimiter = 10, as in standard advice:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Hello dlee

It would be more efficient to send only codes that your App could find in a List or using an If - Then' for example 'D' = done.

What Arduino are you using? I ask because you can use two Serial channels, one to send, one to receive - that would lend reliability compared to trying to share one channel (if you have an Uno, only one Serial Port, you can add the Software Serial Library to your Sketch).

Thank you so much, yes, my delimiter byte is set to 10.

I will try this over the weekend!

Thanks!

Hey Chris,

I am using the arduino Mega. How would I use multiple serial channels over one bluetooth HC-05? How would i need to write the code for the arduino and MIT? Could you show me an example or some link that could help me understand?

Thank you!

Hi - your Mega has four Serial Ports so it is perfect for your Project.

A very simple example Sketch:
SendReceiveArduinoMega.txt (742 Bytes)

//Send Receive Data Test (Arduino Mega)

//vars
unsigned long lgUpdateTime;
char val[];

void setup()
{
         Serial.begin(9600);
         Serial1.begin(9600);
         lgUpdateTime = millis();
}

void loop()
{
         //Excute loop every 1 second
	     if(millis() - lgUpdateTime > 1000) 
         {
               lgUpdateTime = millis();
               
               if (Serial1.available() > 0)
               {
                    //Read Data From App
                    val = Serial1.read();
               
                    //Send same Data back to App
                    Serial.print(val)
                    Serial.println() //App 'end of data' ASCII Char 10 
               }
         }
}

Good morning,

sadly this didn't work. Any other ideas?
What happens is, that the "done" command also gets shown in the notification and the process part does not read the "done" and concurs to the Dismissing of the notifications.

Thanks!

You are showing your Progress Dialog in the wrong place.
You should not show it when messages arrive.
You should show it when you send a request, like in my corrected diagram.

this did not work :confused:
I gave up on the notifications for now. What I mainly want it for the incoming text from the arduino to change the text color to green. How would I manage to do this?

Thanks!

I don't understand why this doesn't work.

So:
The outgoing text gets received in the arduino and starts my code - WORKS

The triggered code sends text back to the app and arrives correctly and also is displayed on the lable - WORKS

Using the Label text = specific text to trigger next action - DOESN'T WORK --- WHY? It gets received correctly, I can display it, why can't I get it to trigger something as simple as turning a specific text into a color?

I created a clock to scann every 100ms for text that equals something specific. It doesn't seem to do it though. I am a newbie and selfthough just to mention. But this should work in my brain :smiley:

Here the code:

I don't have your latest .ino sketch to see what you are sending, and I don't have your exported .aia file to check your Clock settings, so the best I can do is guess ...

You are asking for exact comparisons (=) in your text comparisons.
There might be extra text (whitespace) in your messages causing the comparisons to fail .
Try using a text CONTAINS block instead of math '='.
This will force you to change one of your codes not to be a substring of another code, but that's okay.

1 Like

You are setting Background color, not text color.
Which do you want, Background color, or text color?

  • It was the background color I wanted to change.

  • It now works perfectlz, I can compare the incoming text and wait for a specific key-word to trigger a specific action like in my case changing text background color.

Huge thanks to both of you, I could not have contiuned this project without you and Chris.

Thanks!!

Looking at this, the received text was not perfectly the same as the text you compared it with. It might contain some invisible characters such as spaces or some other character. By checking if "text contains" you have eliminated this problem.

Ahh now I get it. Thanks for explaining!!

...or as I suggested, use codes.

A post was split to a new topic: Error 503 not a valid BlueTooth MAC address