Hi all,
I have a bit of a mystery that I hope someone can solve.
I have an arduino connected to an accelerometer to capture motion data, via an HC-05 module. The AI app polls this at intervals (say 5 seconds) and the arduino replies with the highest value since the last poll. It appears to work ok, but I have noticed something that is incorrect - whenever I get a reply from the arduino, it is the 'previous' value. Tricky to explain..
I'm monitoring the accel value on the laptop via the arduino serial monitor.
The first time that the AI app polls this, I appear to get no reply, although the arduino has generated a value which can be seen in the serial monitor
The next time it polls, I do get a reply - but it is the value generated by the previous poll, and the new, different value may be seen in the serial monitor. The AI app is always one poll behind - unless I put in a 1000mS delay between sending the poll command ('S') and then receiving.
The arduino code is ok, as I have used a bluetooth serial monitor on my phone to manually send an 'S' and get an immediate, correct reply.
Also - if I make a note of the last value generated by the arduino, then reset it or reupload the code - the value returned by the first poll is that last value - which should not have been present in the arduino - it's as if it is lurking in a buffer in the AI system - or something!!
Here are the blocks I'm using - these have the 1000mS delay to make it work correctly - if I were to try to receive text immediately after sending the 'S' (83), it would receive that incorrect, previous value.
Thanks for any advice..
Hi
Without checking your code (as we are about to switch our power off for maintenance) , try removing the Serial Monitor and just work with the App.
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.
Many thanks for the helpful replies. Firstly, I tried removing all of the serial monitor calls from the arduino code but that did not help.
I then set the delimiter for the AI bluetooth to 10, changed the arduino code to send the line ending
( from BTserial.print (msgToSend); to BTserial.println (msgToSend)
In AI, I then set Clock1 to 'enabled' after pressing the SEND button, with an interval of 50mS.
In the Clock1 block, it checks to see if a message has been received, and if so, disable the timer.
This seems to have done the trick, many thanks. I've added the blocks below.