2017년 7월 23일 일요일

App Inventor BLE Component Demo 1: Turning a light on and off


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

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.
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
      
Please design the screen like this:
            


5. Build the App ---- Blocks
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:



The address is Bluetooth Address, you can get it from your device provider or you can use this app to get the Bluetooth Address.(Click Start Scanning. The device list will be shown. The data format in each row is {BluetoothAddress DeviceName Rssi}. The format of Bluetooth Address will be xx:xx:xx:xx:xx:xx.)
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:



After connecting tovice, we can write some data to the BLE device. In this app, we can write integer 0 and 1 in order to turn on and off the light. When you want to write to a BLE device, you need to specify the Service UUID and Characteristic UUID, we can do something like this:




So as the TurnOffButton:



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.



6. Arduino Programming
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:
The RedBearLab shield and the Arduino UNO should be connected like this:



Then you need to plugin the Dupon Line(Male to Female) and connect it to the relay:


The plugs will be looked like this:









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.
Connect the wire like this:



3. Plug in the lamp:
(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.


댓글 없음:

댓글 쓰기