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.


Arduino + BLE Demo: HeartBeat Detector


Massachusetts Institute of Technology
MIT App Inventor Project, CSAIL
6.IoT Seminar
Fall 2015
Arduino + BLE Demo: HeartBeat Detector


Goals:
You will learn how to
add a sensor to an Arduino board
program the Arduino to read data from the sensor
program the Arduino to send the sensor data to the mobile phone


Prerequisites:
Arduino IDE is installed with the libraries
  ⚬ BLE SDK for Arduino
  ⚬ RBL-nRF8001
Arduino Board + BLE shield
Arduino KY-039 Detect the heartbeat module


Exploration:
Connect the Arduino and the sensor in the following way-




Load the HeartBeatDetector.ino file into the Arduino Board with the Arduino IDE
Install the HeartbeatDetector.apk on your phone and start playing with it
  ⚬ FYI - If you are not using the board number 2, you need to load the aia, change the BLE address, and then build the new apk


Challenges:
Try to understand and explain to yourself the code in the HeartBeatDetector.ino
Load the HeartbeatDetector.aia into the App Inventor BLE instance - http://128.52.185.189:8888/
Try to understand and explain to yourself the code in the HeartbeatDetector.aia
Replace the heartbeat sensor with the temperature sensor  and make something.
Show us what you’ve made in the next class --- optional, but strongly encouraged

OBSOLETE: The FirebaseDB Component (Experimental)


March 2016:  This document is obsolete.  It has been superseded by Brief introduction to cloud data and the FirebaseDB component

Note:  The Firebase Component is still experimental.  The implementation may change and your apps may stop working.

The FirebaseDB component implements a shared key value store very similar to TinyWebDB. As its name implies the backend of the FirebaseDB is the Firebase database offered by http://www.firebase.com, which is now part of Google.

Compared the TinyWebDB the FirebaseDB component is backed by a secure, robust database. You do not need to run your own service to use it, we are currently providing a “public” service [NOTE: For the test server this is my personal developer account, so be gentle :-) -Jeff]. We also plan to add support for people providing their own Firebase accounts, but this isn’t working yet.

Like TinyWebDB, you query the database via the “GetValue” and the “StoreValue” blocks. To fetch the value of a “tag” you call “GetValue.” When the value is ready, you get a “GotValue” event which provides the value. There is no callback event for StoreValue.

FirebaseDB adds an additional event, the “DataChanged” event. Whenever one of your tags is changed, both by the currently running app or another copy anywhere else on the planet, you will get a “DataChanged” event.

You do not need your own firebase account, MIT is providing access to a shared account. Even though this account is shared, each developer has their own variable space which is separate from other users. Using this shared account you can create multiple applications that share variables, but two differently developers cannot share variables between apps that each owns separately.

You can also create your own Firebase account. However we do not recommend that you do so at this time as we have not completed support for private Firebase accounts.
Setting up the Firebase Designer properties
Here is what the Firebase Designer Properties look like:

chatbox.png

The FirebaseToken property will automatically be setup for you. Do not edit it. It is the access control token that permits your application to access your variables on the server. If you do mistakenly edit it, and variables stop working, you should set it to an empty string (select all of the text in the textbox and delete it). Now reload your project and a new token will be generated automatically.

The FirebaseURL points to the account used at Firebase.  If you have your own Firebase account, you configure its URL here. Otherwise leave it at the “DEFAULT” value which causes the shared account to be used.

The last property, the “ProjectBucket” defaults to the name of the project. You can change this. If two projects share the same ProjectBucket, then they share variables as long as both projects belong to the same MIT App Inventor account.
FirebaseDB Blocks [Work in Progress]

Unauthenticate

If you are having difficulty with the Companion and you are switching between different Firebase accounts, you may need to use this function to clear internal Firebase caches. You can just use the "Do It" function on this block in the blocks editor.

**

Experimental Components - App Inventor for Android


Table of Contents

FirebaseDB

Non-visible component that communicates with Firebase to store and retrieve information.

Properties

DeveloperBucket (designer only)
FirebaseToken (designer only)
FirebaseURL
Gets the URL for this FirebaseDB.
Persist (designer only)
If true, variables will retain their values when off-line and the App exits. Values will be uploaded to Firebase the next time the App is run while connected to the network. This is useful for applications which will gather data while not connected to the network. Note: AppendValue and RemoveFirst will not work correctly when off-line, they require a network connection.

Note: If you set Persist on any Firebase component, on any screen, it makes all Firebase components on all screens persistent. This is a limitation of the low level Firebase library. Also be aware that if you want to set persist to true, you should do so before connecting the Companion for incremental development.
ProjectBucket
Gets the ProjectBucket for this FirebaseDB.

Events

DataChanged(text tag, any value)
Indicates that the data in the Firebase has changed. Launches an event with the tag and value that have been updated.
FirebaseError(text message)
Indicates that the communication with the Firebase signaled an error.
FirstRemoved(any value)
Event triggered by the "RemoveFirst" function. The argument "value" is the object that was the first in the list, and which is now removed.
GotValue(text tag, any value)
Indicates that a GetValue request has succeeded.
TagList(list value)
Event triggered when we have received the list of known tags. Used with the "GetTagList" Function.

Methods

AppendValue(text tag, any valueToAdd)
Append a value to the end of a list atomically. If two devices use this function simultaneously, both will be appended and no data lost.
GetTagList()
Get the list of tags for this application. When complete a "TagList" event will be triggered with the list of known tags.
GetValue(text tag, any valueIfTagNotThere)
GetValue asks Firebase to get the value stored under the given tag. It will pass valueIfTagNotThere to GotValue if there is no value stored under the tag.
RemoveFirst(text tag)
Return the first element of a list and atomically remove it. If two devices use this function simultaneously, one will get the first element and the the other will get the second element, or an error if there is no available element. When the element is available, the "FirstRemoved" event will be triggered.
ClearTag(text tag)
Remove the tag from Firebase
StoreValue(text tag, any valueToStore)
Asks Firebase to store the given value under the given tag.
If you are having difficulty with the Companion and you are switching between different Firebase accounts, you may need to use this function to clear internal Firebase caches. You can just use the "Do It" function on this block in the blocks editor. Note: You should not normally need to use this block as part of an application.