Exercise 17 - Interface & Appliction Programming

This week we will be exploring App Inventor. I will creating an Andriod app to control my Attiny 85 via bluetooth. I begin to look into tutorials online and was able to program it.

 

What I need?

1. Bluetooth module
2. RGB LED
3. Andriod phone
4. Internet connection (for google voice)
5. Cables
6. Attiny 85 board

 

Bluetooth module HC-06

THe HC-06 is a class 2 Bluetooth module designed for transparent wireless serial communication. It is pre-configured as a slave Bluetooth device. Once it is paired to a master Bluetooth device such as PC, smart phones and tablet, its operation becomes transparent to the user. No user code specific to the Bluetooth module is needed at all in the user microcontroller program. The data sheet is here. The cost of one HC-06 module is only US$2.73 and it make no sense to make your own.

 

The HC-06 use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. It acts as a serial port through which data can be send and receive. Range is about 10 meters.

 

How to connect?

Setting up the HC-06 is as easy as ABC. All you need to know is the pin configuration. The HC-06 has 6 pins: wakeup, VCC, GND, TXD, RXD and State. I will only deal with 4 pins, which are VCC, GND, TXD and RXD.



The bluetooth pairing key is 1234

Steps in designing

I am not doing a tutorial for every steps but these are the basic steps.

1. Go to http://ai2.appinventor.mit.edu

2. Create all the necessary account and password.

3. Go to Project and start a new project

4. In the Design screen, you will see the depiction of an Andriod device screen.

5. Start by drag n drop user interface onto the screen.

6. Go to the blocks and add in the program logic.

7. Save the project.

8. Go to build app and select provide qr code for apk

9. Upload apk to Andriod phone to try,

My application

I want my app to control a RGB LED via bluetooth. I have the option to use the button or my voice to give command.

Screen design

 

My program

App Inventor 2

1. First I want to check if bluetooth is turn on when the program loads. If it is not, there will be a message to remind the user.

2. When the user click pair device, the list of bluetooth available will be displayed.

3. If it is the first time pairing, the phone will prompt for password. The default is 1234.

4. The disconnect button is to disconnect the bluetooth.

5. The status will show either connect to disconnected.

6. The Red, Green, Blue and power off buttons will send the respective commands when pressed.

7. Adding voice

Attiny 85 programing

I modified from the previous program that I did during the output week. I have to set up the serial communication for the bluetooth and the code to activate the commands.

#include <SoftwareSerial.h>
String voice; //value sent over via bluetooth
int redPin= 3;
int greenPin= 4;
int bluePin= 0;
int rx=2;
int tx=1;
SoftwareSerial mySerial(rx,tx);
void setup() {
mySerial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
}
void loop() {
while (mySerial.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = mySerial.read(); //Conduct a serial read
// if (c == '#') {break;} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
}
if (voice.length() > 0) {
mySerial.println(voice);
//----------Turn On One-By-One----------//
if(voice == "red") {setColor(255, 0, 0);}
else if(voice == "green") {setColor(0, 255, 0);}
else if(voice == "blue") {setColor(0, 0, 255);}
else if(voice == "power off") {setColor(0, 0, 0);}
//-----------------------------------------------------------------------//
voice="";}} //Reset the variable after initiating
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

Result

Interfacing python with ATtiny

I found a very good tutorial at https://petrimaki.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/ I followed the tutorial and was able to capture what was being sent to the ATtiny.
Download Python and Pyserial.

After installing Python, unzip pyserial to the Lib folder. For me, it is c:\Python27\Lib\pyserial-2.6\

key in python setup.py install.

My program is to monitor what is being sent to the ATtiny.

import serial
import time
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
try:
print ser.readline()
time.sleep(1)
except ser.SerialTimeoutException:
print('Data could not be read')
time.sleep(1)

Result

 

 

Download

Attiny ino

App Inventor 2 aia

Apk

My Thoughts

App Inventor 2 is easy to use. A simple andriod program can easily be build in a few minutes. The speed recognition is dependent of the accent of the speaker. Sometimes the detection is not accurate. I would like to explore with other sensors when I have the time. Now I need to start my final project.

 

References

http://randomnerdtutorials.com/how-to-use-app-inventor-with-arduino/
http://www.instructables.com/id/Connect-Arduino-Uno-to-Android-via-Bluetooth/step4/App-Inventor-Code-Introduction/
http://forum.arduino.cc/index.php/topic,148853.msg1118346.html#msg1118346