Good day
I am sending messages from a microcontroller to an android app. The messages have a format "ACTUALTEMP 35", "SETPOINTTEMP 45". I would like to write the information to it's specific label. How do I extract the information and then send it to the correct label?
I am assuming you are receiving one complete line at a time.
If not, read ...
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.
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.
Once you have a message, you can run it through a CSV to LIST block, to get a list of 2 items:
- ACTUALTEMP 35
- SETPOINTTEMP 45
For each reading in that list
set local readingParts to (split reading at blank)
set local readingName to select item 1 of readingParts
set local readingValue to select item 2 of readingParts
if readingName = 'ACTUALTEMP' then
set labelACTUALTEMP.Text to readingValue
if readingName = 'SETPOINTTEMP' then
set labelSETPOINTTEMP.Text to readingValue
end for each
Hi, Thank you for the advice. I have built the program and it works well. I will attach what I have so far. Could you please have a look at it to check for any possible issues that I am not thinking of and also I would like to know if you have any suggestions on how I can improve.
Capstone_Project.aia (3.4 KB)
New Text Document.txt (997 Bytes)
What microcontroller are you using?
Hi, I am using a Nucleo F446RE
Your code:
//This is the format that data is being sent in
sprintf(str, "ACTUALTEMP %d\n",actual_temp); //send characters to a string
bt.write(str, strlen(str)); //write contents of a string to the UART
ThisThread::sleep_for(100ms);
sprintf(str, "SETPOINTTEMP %d\n",setpoint_temp); //send characters to a string
bt.write(str, strlen(str)); //write contents of a string to the UART
ThisThread::sleep_for(100ms);
sprintf(str, "HEATINGOUTPUT %.1f\n",heating_output); //send characters to a string
bt.write(str, strlen(str)); //write contents of a string to the UART
ThisThread::sleep_for(100ms);
sprintf(str, "COOLINGOUTPUT %.1f\n",cooling_output); //send characters to a string
bt.write(str, strlen(str)); //write contents of a string to the UART
ThisThread::sleep_for(100ms);
Your Clock Timer:
I was worried when I saw that '\n' between the reading label and the variable value in the sprintf commands, then I remembered that in sprintf the '\n' gets added to the transmission string AFTER the variable value, so that's okay. False alarm.
Your AI2 Delimiter is set properly in the Designer, and your Clock Timer is plenty fast at 10 ms.
Your project looks fine, as far as I can see.