Project problem: Tilt detector using Arduino UNO, Tilt switch, HC-05, and App Inventor

It is a tilt detector that beeps in real life with 3 tilts (for a seizure project); and I want it to display data to the App, possibly with sound and notification as well.

The problem occurs in the app itself (I probably have wrong coding). HC-05 is working as intended (as tested in Bluetooth Serial Monitor.

It says Error 515, and when I press connect device, opens the Listpicker, I see the HC-05, but I cannot press it. Once I do, it shows a black screen, then it comes back to the app.





Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

Also, the New line character in AI2 is \n, not /n.

Here is a simple BlueTooth text receiver sample, for single value per line:
blocks
initialize global message to


Re your Arduino Sketch, your send message function is trying to send sentences via bluetooth, but a bluetooth packet payload size is only 20 bytes. What we do is to send a single character to the App, for example:

a = "Alert: Multiple tilts detected! Buzzer Activated! Abandon Ship!"

So the App receives 'a', looks that up in a block list and presents the message to the App User.

1 Like

Concerning the connection. If there are no other Bluetooth devices in range, then your device of interest will be found and listed by your code. However, if there is more than one device in range, the App needs a tiny bit more time to collect all of the device addresses available, so it is common practise to put the Address And Names block in a Clock Timer.

Once a device is selected, any follow-up functions should check that Bluetooth is still connected before code execution.

You are using a List Picker? I'm wondering if a single element (row or line) is an issue for the picker. Shouldn't be.......

Depending on the Android version of your Smartphone, your App may need to first ask for some communications permissions. Search the forum for "Bluetooth Permissions". Almost certainly Location will be required, so in the first instance you can drag-drop a Location Sensor into the designer (it will trigger a permission request if required but other than that is not used by the App).

Here is a basic example file, not up to date with permissions for the latest Android devices but used by many as a template: When you give a component a name, always retain the App Inventor name as a prefix, e.g. Label_ReceivedValue - that way others can understand your code more easily and therefore it is easier to help you.

BT_Basic_Setup_ReceiveOne.aia (7.6 KB)

ProfessorCad: Tips & Tricks Bluetooth
https://www.professorcad.co.uk/appinventortips#BluetoothFailure Bluetooth Failure

1 Like

Thank you so much! I’ve tried the method by Sir ChrisWard, but there appears to be a problem- I tried the sample aia, but even when it’s paired in my phone, it’s now showing Error 507.

507 happens when you send without first testing if you are connected.