Software Requirements
1. App Inventor with BLE Component
2. Android 4.3 or higher
Hardware Requirements
1. A lamp with bulb
2. Arduino UNO
3. USB Type A-B Cable
4. RedBearLab BLE Shield
5. A single relay model: SRD-05VDC-SL-C
6. Dupon Line Male to Female
7. Bolt cutter suitable for cutting the lamp charger wire
8. Insulated rubber tape
Software setup
1. Getting Started
Connect to the App Inventor web site and start a new project named LightControl. Download the button picture on your computer. Connect your phone to the computer.
2. Introduction
You will build an App to control a lamp. You can connect to the RedBearLab Shield. Click the button to send the data to it, which will then control a relay to enable or disable the light. You can also use speech to control the lamp.
You will learn:
a. How to scan BLE devices
b. How to connect a BLE device through its Bluetooth Address.
c. How to write to a BLE device through its UUID
d. How to convert your speech to text with App Inventor
a. How to scan BLE devices
b. How to connect a BLE device through its Bluetooth Address.
c. How to write to a BLE device through its UUID
d. How to convert your speech to text with App Inventor
3. Basic Concepts
Before building up the software parts, there are a few concepts that you need to know about:
Bluetooth Address:
A Bluetooth Address is a 6-byte unique identifier. When you do the Bluetooth device scanning, you will receive the packets that contain Bluetooth Address. If you want to connect to a specific device, you need to get its advertising packets first. And you need this Bluetooth address which is contained in the advertising packets to connect to the device
UUID:
UUID refers to an identification string to identify the data types found in the Generic Attributes Protocol (GATT). In this app, you need a Service UUID and a Characteristic UUID. A Service UUID is used to identify a service which is a collection of characteristics and relationships to other services that encapsulate the behavior of part of a device. A Characteristic UUID is used to define attribute types that contain a single logical value. All in all, a UUID identifies the data type.
4. Build the App ---- Designer View:
In this part you will learn how to design the UI for LightControl App.
In this part you will learn how to design the UI for LightControl App.
Component Type
|
Palette Group
|
Name
|
Purpose
|
Component Settings
|
Button
|
User Interface
|
ConnectButton
|
Connect to the device by using Bluetooth Address
|
FontSize: 14
Width: Fill Parent
|
Button
|
User Interface
|
TurnOnButton
|
Send the data to BLE device to turn on the light
|
FontSize: 14
Height: 20% Width: 20%
|
Button
|
User Interface
|
TurnOffButton
|
Send the data to BLE device to turn off the light
|
FontSize: 14
Height: 20% Width: 20%
|
Button
|
User Interface
|
VoiceButton
|
Enable the voice control
|
Height: 100%
Width: 100%
Image: sound.png
|
Label
|
User Interface
|
VoiceLabel
|
Tell user the button function
|
FontSize: 20
Text: voice control
|
Label
|
User Interface
|
LightStatusLabel
|
Tell user the light status
|
FontSize: 16
Text: Light Status
|
HorizntalArrangement
|
Layout
|
HorizontalArrangement1
|
Hold the TurnOnButton and TurnOffButton
|
AlignHorizontal:left
AlightVertical:Top
Height: 20%
Width: Fill Parent
|
VerticalArrangement
|
Layout
|
VerticalArrangement1
|
Hold the VoiceButton, VoiceLabel, LightStatusLabel
|
AlignHorizontal: Center
AlightVertical: Center
Width: Fill Parent
|
SpeechRecognizer
|
Media
|
SpeechRecognizer1
|
Convert user’s Speech to text
| |
BLE
|
Connectivity
|
BLE1
|
Scan, Connect, Send data to BLE Device
|
If you want to connect to the RedBearLab Shield, you need to scan it first. You can use a button to trigger the StartScanning function. But a better way is to trigger the function once the app is opened:
Hold on for a while in order to make sure the device is found. Once the app is found you can connect to the device by clicking the ConnectButton and the block should be like this:
In order to know the connection status, we can set the text of LightStatusLabel to be: “Connected! You can control your light now.” Simply make blocks like this:
We can enable the voice control by using SpeechRecognizor. Get your voice and convert it to text, when a “Turn On” is detected, the light will turn on and when a ”Turnoff” is detected, the light will turn off.
Arduino, RedBearLab BLE shield and a relay are the actual parts to control the light on and off. The BLE shield is used to receive the signal from your phone, and the arduino is responsible for sending a signal (HIGH or LOW) to the relay. Relay will control the light on and off. You need to program your Arduino using this arduino program. For more information about using Arduino UNO please visit the website: https://www.arduino.cc/en/Guide/HomePage and visit this website to get to know how to use BLE shield with arduino.
You can open the Serial Monitor to get to know more about the status of the board.
7. Hardware Setup
1. Set up the arduino board and BLE shield:
2. Cut the fire line of the lamp into two:
Warning: Dangerous! Please make 100% sure that you are not connecting the lamp with socket when cutting the wire. After cutting the wire please use the insulated rubber tape to protect the wire.
(Warning: Please be sure that you have a rubber glove to protect you, make sure that you plug in the wire in the right way, make sure sure that you do not touch the wire.)
8. Test your app
After all, you can open your app for testing. Make sure that you are not too far from the Arduino and BLE Shield. Open the app and the scanning will be triggered immediately. Hold on for around 5 seconds, and click the ConnectButton. When you see that Text:
“Connected! You can control your light now.” You can then click the button to turn on and off the light. Also, try to use Voice control, simply speak: Turn on or Turn off.