This topic is the third lesson of MIT App Inventor IoT (Internet of Things) tutorials. We are going to introduce how to use your Android phone to continuously read Arduino 101’s analog pin(A0) via App Inventor’s BluetoothLE (Bluetooth 4.0 Low Energy)component.
MIT App Inventor will use Arduino 101 as the core dev board of its IoT solutions, you can develop various kinds of interactive project with the kit (planning). You must import BLE component(.aix) as an extension before using it. A screenshot of the actual app execution is shown below. Enjoy~
Arduino 101 is the latest dev board under cooperation between Arduino.cc and Intel, which is named as Genuino 101 out side USA.
This example had used 3 Clock components, and their TimerInterval are found after several tests, you may need to adjust them according to the real situation. Since different protocol, you can not use old BluetoothClient component to talk with Arduino 101.
How to play:
For simplicity, this project connect to your Arduino 101 directly, which means the Bluetooth address is assigned in the code. If your app has to connect from several devices, I think pairing with these devices first and pick them by ListPicker component in App Inventor is a better solution.
How to pair your Arduino 101 and Andorid phone: First you must pair the Bluetooth module with your Android phone. After you connect all the hardware, you can see that the Bluetooth module’s red LED is flashing, means ready for connecting. Then please open your Android phone’s Bluetooth setting page (which should under Settings… , differs on each phone), click the name of your module (something like HC-05). Please enter 1234 (or 0000) when there is a prompt asking the paring key. Done~
There is only BTList which is enabled when you first open the app. Please click BTList, it will show all paired Bluetooth devices on your phone. Please click the name of your module, it should take 1~2 seconds to connect and back the main screen. You can see that the [LED Turn On] button is now clickable and the module’s LED is keep lighted up. Just click the button, the LED on Arduino will light up and the button’s text will become “LED Turn Off”. Click again will turn off the LED and switch back the button text.
Please click the [Disconnect] button when you don’t want to play anymore, this will disconnect the connection of your phone and bluetooth module and the module’s LED will flashing again.
1. Initialize and connect
While initializing(Screen1.Initializing event), BluetoothLE1 component will scan for avalible devices, which means your Arduino 101 should be ready by then.
When you click the ConnectButton (Button_Connect.Click event), BluetoothLE1 component will try to connect the specified device, we put our Arduino 101’s bluetooth address here (98:4F…), please modify this string according to your Arduino 101’s.
2. Connection confirmed
BluetoothLE1.connected event will be called after the connection is established, we show a message on Screen’s titlebar. It’s quite useful for display some shor messages.
3. Clock1 (called every 1 second): read data sent back by Arduino 101
On the other hand, if disconnect variable value is false, then App Inventor will start to combine two variables (data1 and data2) to get real analog pin status. But first you need to enable the Timer event of Clock2 and Clock3.
The reason why combining two variables is because the range of Arduino’s analog pin is 0~1023, so it need two integer variables(data2 and data1) to represent. Finally to show this combined value on Label and Slider, done~
4. Clock2 (called every 0.9 second):
Clock2 will disable the Timer event of itsefl and Clock3’s every 0.9 second, then re-activate the timer event according to the value of disconnect .
Clock3 will control how to read a integer from Arduino 101(BluetoothLE.ReadIntValue) according to status variable(0 or 1). Then assign the result of BluetoothLE.IntGattValue to data variable, if the value of data is within 128~256, then minus 128 from it before assigning to data2.
However, if data is less than 128, them assign data to data1, we will combine it in Clock1.Timer event.
When click Button_Disconnect, we will set disconnect varaible to true, which will trigger BluetoothLE.DisconnectWithAddress)in Clock1.Timer event (STEP3).
Copy the code and paste it onto Arduino 101. Please notice that Arduino 101 has onboard BLE hardware, therefore you don’t need to connect other Bluetooth modules such as HC05.
service_uuid and characteristic_uuid are also specified in the sketch as “19B10011-E8F2-537E-4F6C-D104768A1214″, which must be the same with the string you use in App Inventor.