July 30, 2013

Get started on Android

Although the Android Developer site is pretty comprehensive, I found it is also useful for me to learn Android Developement via videos. TheNewBoston is hands down one of the best tutorial sites for technical knowledge, mainly for programming languages such as C, C++, Java and a couple more. If you haven't gone through his video, do check them here.

Nevertheless, I still think that some important aspects of Android development are best exemplified on Android Developer site and you will find you'll be doing frequent visit to the site. Bookmark it! :-)

You also need to know the Android API site. These explain all available APIs and it will be useful for you in order to understand each and every objects and their methods of within Android's Java. If you use Eclipse, right clicking on the methods will redirect you to this site within the editor itself.

Installing Python 2.6 and PyBluez on Rpi (or any Debian-based Linux Distro)

My experiment on running the simple server based on Python 2.6 and PyBluez works well in Windows along with the App I've written. I was able to send data from the Android App and the server was able to receive the data and display it.

Implementing native Bluetooth socket on Rpi sent me to a dead end where I could not figure out why it doesn't work and lack of resources to resolve the problem. Although packaged in within the more updated version of Python (Python 3.3), little documentations have been made on it.

On Rpi, the App shown that connection has been made with the server and data has been successfully sent, but none of those are indicated on the server that I ran. Weird? Yes. I have decided to try writing the server with the older Python 2.6 and PyBluez to test if it works.


Installing Python 2.6 in Linux:
1. Download Python 2.6 from here
2. Go to the containing directory (use "cd" command to navigate).
3. Decompress the .tar.gz by using
    "tar -xzf Python-2.6.6.tar.gz"
4. Go to the decompressed folder "cd Python-2.6.6"
5. Issues these following command
   "./configure
  make
  sudo make install
"


Installing PyBluez in Linux:
1. Download PyBluez from here
2. Go to the containing directory
3. Decompress the .tar.gz by using
    "tar -xzf PyBluez-0.18.tar.gz"
4. Go to the decompressed folder "cd PyBluez-0.18"
5. Still in the shell, run "sudo python2.6 setup.py install

(Note that we run the script using "python2.6" and not "python". "python" will refer to the default installed python which could be Python2.7 or Python 3 which we don't want to use for now. Issuing "python2.6" will ensure that we run our scripts using Python2.6 instead.)

Update: You might get an error that says Python.h is missing. Python.h is a header file written in C++. It is used by gcc to build applications. You need to install a package called python-dev. This package includes header files, a static library and development tools for building Python modules. To install this package, enter: "sudo apt-get install python2.6-dev" targeting our Python2.6You might also need to install bluetooth header (bluetooth.h). Enter "apt-get install libbluetooth-dev". After these two headers are installed, now you can proceed to installing PyBluez.

Update: It seems that writing the server with Python2.6 and PyBluez gave me the same result. My next try will be writing the server on another language. The problem could be on Python's implementation on Bluetooth with Rpi.

July 29, 2013

Raspberry Pi - A Brief Introduction and Basic Setup

Introduction

Raspberry Pi is the small size computer roughly sized as credit card that runs on Linux. The board is relatively very cheap which is $35. The RPi can be used to many things from using is as computer or to make it as small robot. It can be plugged in USB peripherals (keyboards, mice, etc.), output video via HDMI and DVI, or even composite out to an old analogue TV.


Basic Setup

Setting up the RPi is very easy and it will only take in less than 10 minutes to setup and run.

Things needed before start:

- Raspberry Pi
- SD Card(at least 4 gig)
- HDMI supported monitor
- Mouse
- Keyboard
- Power Supply (5V - 1A)
- SD Formatter
- RPi recovery system(NOOBS_v1_2_1.zip)

Setup - RPi:

1. Insert an SD card that is 4GB or greater in size into your computer.
2. Format the SD card so that the Pi can read it by following the steps below.

3. Install the Formatting Tool on your machine.
4. Run the Formatting Tool.

5. Set "FORMAT SIZE ADJUSTMENT" option to "ON" in the "Options" menu.

6. Check that the SD card you inserted matches the one selected by the Tool.

7. Click the “Format” button.

8. Download the NOOBS_v1_2_1.zip from (downloads.raspberrypi.org/recovery).

9. Unzip the RPi recovery system or NOOBS_v1_2_1.zip.

10. Copy the extracted files onto the SD card that you just formatted.

11. Insert the SD card into your Pi.

12. Connect all the connections which is mouse, keyboard and monitor.

13. Finally connect the power supply to the RPi.


Setup - Raspbian

Once boot up for the first time, a menu will prompted(see the image below) you to install one of several operating systems into the free space on the card. The choice means you can boot the Pi with a regular operating system like Raspbian, or with a media-centre specific OS like RaspBMC.




Once Boot up, we now have most low cost computer running.




July 28, 2013

Android forums for inquiries

Over the time, while writing and testing the Android App, I came across some issues which are not very common thus making them harder to be reached from search engines like Google. It's very troublesome, especially when they are related to something new, something that was just recently released or updated. When it comes to that, the best way to battle the issues is by putting my own questions on the webs and wait for them to be answered. Believe or not, you can get few good responses just within one day.

I have gathered some of the forums I normally put up my questions at below:

1. Stackoverflow
2. Android Forums
3. Android Central
4. xda-developers
5. Android.net
6. TalkAndroid Forum
7. AndroidPit

July 26, 2013

Arduino

My first encounter with Arduino was last year, when me and Ifa were working on our Embedded System's assignment. Our task was to design a Bin-to-Dec converter, with 3 7-segment displays. I didn't keep much records on the works done, but I did take this video after we unbox-ed the Arduino.


Uno!

Python 2.6 and PyBluez

My research on Python ended with Python 2.6 and PyBluez. If you haven't noticed, there are newer version of Python (currently on version 3.3) but the later versions are not compatible with PyBluez. My so-far googling skills got me to the conclusion that this is the better solution, albeit temporarily. Python does not have Bluetooth native library before version 3.0 (if I'm not mistaken) and while they have added them now, the documentations were not really thorough. PyBluez, on the contrary, is only supported for Python until version 2.6. Since I have to do some demonstration with Python and Bluetooth, this is the easier solution. I will get back to the Python native Bluetooth library later on.

This post will help you to do 2 things, assuming you're on Windows:

1. Installing Python 2.6 and PyBluez
2. Run a simple Bluetooth serial server script

Installing Python 2.6 and PyBluez
1. Get them both here : Python 2.6 PyBluez
2. Run the Python installer first, followed by the PyBluez Installer

Run the Bluetooth serial server
1. Copy the whole code below into a file named BluetoothServer.py
2. Save the file in the same folder where Python was installed previously
3. At this point, turn on your Bluetooth, set for an incomming connection (set a listening port) and pair with the other device (client)
4. Start the Windows command prompt. Navigate to the folder where Python was installed
5. Run the script : >>python BluetoothServer.py
6. Server is active and ready for an incoming connection : "Waiting for connection on RFCOMM Channel 1"

The simple server is based on the PyBluez examples here.

# file: BluetoothServer.py
# desc: Simple Bluetooth Serial Server (Python 2.6 and PyBluez)

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

#change UUID accordingly
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" 

#start the listening server
advertise_service( server_sock, "BluetoothServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
                    )
                   
print "Waiting for connection on RFCOMM channel %d" % port

#accepting connection
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

#catch data and display.
#wait for the client to cut off the connection

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print "received [%s]" % data
except IOError:
    pass

print "client disconnected"

#close sockets 
client_sock.close()
server_sock.close()
print "Server is off"

The Android App (client) for sending in data is discussed separately on another post. The next step is to have this simple server ran on the Raspberry Pi and adding in some output manipulations using the Pi's GPIO pins.

Update: Here is a good read on the same topic. Link

Python 3.3 and Bluetooth native socket

I have posted earlier on how to run a simple serial server using Python 2.6 and PyBluez. Python 3.3 has natively available sockets class written for Bluetooth communication, eliminating the needs for PyBluez. I have not tested it yet myself, but if I'm not mistaken the native Bluetooth socket is only supported on unix based OS. So, if you're on Windows, my guess is best to stay with Python 2.6 and PyBluez.

A sample script for the server is as below:

"""
A simple Python script to receive messages from a client over
Bluetooth using Python sockets (with Python 3.3 or above).
"""

import socket

hostMACAddress = '00:1f:e1:dd:08:3d' 

#The MAC address of a Bluetooth adapter on the server. 
#The server might have multiple Bluetooth adapters.

port = 3 

#3 is an arbitrary choice. It should match the port used by the client.
backlog = 1
size = 1024 


s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)


try:
       client, address = s.accept()
       while 1:
           data = client.recv(size)
           if data:
                   print(data)
                   client.send(data)
except:  
       print("Closing socket")  
       client.close()
       s.close()


The scripts was not written by me. The original page here discussed the difference between Python 2.6+PyBluez and native Bluetooth socket in Python 3.3.

Here's an example of how to use the native Bluetooth socket to send a few command bytes to control the speed and direction of a Bluetooth controlled car.

July 12, 2013

Johara - These guys never bored me!



It started a few months ago when I was doing my intern-ship. I am (no offence to Alex) always being picked to follow my boss around for meetings and other out-of-office works, which normally required me to be there at the office very early in the morning. He basically introduced me to the Johara Pagi Era session, saying he listens to them every day. It's aired around 715 AM in the morning on ERA.fm.

A good laugh to start a day.

They actually have a page archiving all their shows. Just perfect :D

Johara Pagi Era Session on ERA.fm

July 9, 2013

Python 3.3 and Bluetooth native socket

I have posted earlier on how to run a simple serial server using Python 2.6 and PyBluez. Python 3.3 has natively available sockets class written for Bluetooth communication, eliminating the needs for PyBluez. I have not tested it yet myself, but if I'm not mistaken the native Bluetooth socket is only supported on unix based OS. So, if you're on Windows, my guess is best to stay with Python 2.6 and PyBluez.

A sample script for the server is as below:

"""
A simple Python script to receive messages from a client over
Bluetooth using Python sockets (with Python 3.3 or above).
"""

import socket

hostMACAddress = '00:1f:e1:dd:08:3d' 

#The MAC address of a Bluetooth adapter on the server. 
#The server might have multiple Bluetooth adapters.

port = 3 

#3 is an arbitrary choice. It should match the port used by the client.
backlog = 1
size = 1024 


s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)


try:
       client, address = s.accept()
       while 1:
           data = client.recv(size)
           if data:
                   print(data)
                   client.send(data)
except:  
       print("Closing socket")  
       client.close()
       s.close()


The scripts was not written by me. The original page here discussed the difference between Python 2.6+PyBluez and native Bluetooth socket in Python 3.3.

Here's an example of how to use the native Bluetooth socket to send a few command bytes to control the speed and direction of a Bluetooth controlled car.

July 5, 2013

Raspberry GPIO Introduction

Here's a good video to get started with Raspberry Pi's GPIO. It includes the installation procedures, among others. The scripts were pretty simple, but it's a good start nevertheless.



Here's a video on using the Pi's GPIO for a Keypad based Access Control. 





July 4, 2013

Python 2.6 and PyBluez

My research on Python ended with Python 2.6 and PyBluez. If you haven't noticed, there are newer version of Python (currently on version 3.3) but the later versions are not compatible with PyBluez. My so-far googling skills got me to the conclusion that this is the better solution, albeit temporarily. Python does not have Bluetooth native library before version 3.0 (if I'm not mistaken) and while they have added them now, the documentations were not really thorough. PyBluez, on the contrary, is only supported for Python until version 2.6. Since I have to do some demonstration with Python and Bluetooth, this is the easier solution. I will get back to the Python native Bluetooth library later on.

This post will help you to do 2 things, assuming you're on Windows:

1. Installing Python 2.6 and PyBluez
2. Run a simple Bluetooth serial server script

Installing Python 2.6 and PyBluez
1. Get them both here : Python 2.6 PyBluez
2. Run the Python installer first, followed by the PyBluez Installer

Run the Bluetooth serial server
1. Copy the whole code below into a file named BluetoothServer.py
2. Save the file in the same folder where Python was installed previously
3. At this point, turn on your Bluetooth, set for an incomming connection (set a listening port) and pair with the other device (client)
4. Start the Windows command prompt. Navigate to the folder where Python was installed
5. Run the script : >>python BluetoothServer.py
6. Server is active and ready for an incoming connection : "Waiting for connection on RFCOMM Channel 1"

The simple server is based on the PyBluez examples here.

# file: BluetoothServer.py
# desc: Simple Bluetooth Serial Server (Python 2.6 and PyBluez)

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

#change UUID accordingly
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" 

#start the listening server
advertise_service( server_sock, "BluetoothServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
                    )
                   
print "Waiting for connection on RFCOMM channel %d" % port

#accepting connection
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

#catch data and display.
#wait for the client to cut off the connection

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print "received [%s]" % data
except IOError:
    pass

print "client disconnected"

#close sockets 
client_sock.close()
server_sock.close()
print "Server is off"

The Android App (client) for sending in data is discussed separately on another post. The next step is to have this simple server ran on the Raspberry Pi and adding in some output manipulations using the Pi's GPIO pins.

Update: Here is a good read on the same topic. Link

July 3, 2013

Learning Android for FYP

I am now into my 2nd last Trimester in MMU, where I have begun my final year project (FYP). Me and my friend, Jeeva are partners, currently under the supervision of Mr. Cheong (a great lecturer, one of the best I've known). Our project? A phone based Access Control system, based on UniKey.



UniKey is an Access Control system which replaces traditional standard lock and keys with your smart phone. They haven't actually launched their product yet. UniKey makes use of the Bluetooth technology (low cost, low power, widely supported, readily available) to communicate with the electronic door operator. Initially, our project consist of 2 essential parts:

  1. A phone App to that store the 'key' and response to the electronic door operator via Bluetooth. Plus, some key managements as an added feature. Android platform as a start.
  2. The electronic door operator which communicates with the phone App via Bluetooth and responsible for the control that locks/unlocks the door. The development will be done using the popular Raspberry Pi before we move on to construct our custom circuit board.
We might actually want to enhance further when we have some basics working. I am in charge of the software development part of the project while Jeeva will do most of the hardware part. The Iphone App for the Unikey looks like below (they haven't developed an Android App yet);


This week marks the 3rd week of our project development stage and I am still at the stage of "learning" and "figuring out". I had my first touch on Android App a year ago while helping a friend on his project, but those were merely introductory. Android Bluetooth API is something very new to me and with the lack of hardwares to test, I ended up progessing pretty slow. For a start, I found out after 2 weeks that most Bluetooth adapters on Samsung phones are not reliable which really is a headache because I only have Samsung devices available (Note 2, Tab). To summarize, these are what I have done in the past 2 weeks and what should I be doing in the next few weeks. This is based on MMU Trimester calendar, where I started the project on Week 2, not on Week 1.

  1. Week 2 : We were briefed on the project. I was assigned to write a 10 page of Literature Review and  to write a simple Android App to get me familiar with Eclipse and Android GUI. Simple Android App with basic GUI is completed.
  2. Week 3 : Demonstrate the simple App to Mr. Cheong. Next, I was assigned with a new task to write another App to simply send a file to another phone via Bluetooth. I went as far as to able to discover other available Bluetooth device, but unable to pair and hence, unable to connect. On the other hand, Literature Review Draft 1 is submitted. Mr. Cheong added a few points of suggestion to add into the Literature Review.
  3. Week 4 : The App is able to connect and pair, but crashed at run time a few seconds afterwards. Meeting with Mr. Cheong went well although I wasn't able to demonstrate a fully functional app. He asked for the submission of the edited Literature Review during the next meeting. I was asked to write a fully functional App that is able to send a file to a Bluetooth device (this time, probably a computer with a Bluetooth dongle) as well as some simple server program (written in C or Python) which can read the file.
As I am not really familiar with Android development as yet, this is quite a tough project to work on. As Mr. Cheong described, I should be doing head banging most of the time while working on the project. However, I am pretty excited. I love challenges. :-)

By the way, our work will not entirely be published here as they are belong to MMU, per their policy. In fact, I will not be posting any of the works here. However, I have promised myself I will keep posting something on the progress of the project and probably some interesting tutorials and articles that I found useful.

I need that practice in writing.