Designing and developing embedded systems is part of my job and a hobby. As such, I have always wanted to get a working serial link between an android device and some custom embedded hardware. The main reason being that the phone gives the project in question access to mobile network. Some of the main communication gateway options would be SMS, WiFi, Bluetooth and the mobile internet. the phone also has good enough processing power, ample and extendable memory(SD card), some have a set of sensors and a camera may come in handy. The Android API is attractive as it bundles some applications classes that quicken UI development, which isn’t really worth investing too much effort in.
To do this, we will need:
- A fully charged rooted phone (I used u8150)
- USB host kernel sources info & download or a bootable image info and download
- Externally powered usb bus
- USB extender hub(optional) – for connecting multiple devices
- USB to serial converter(common: arkmicro, pl2303, ftdi, etc..)
- USB storage device (optional)
- Android apps
For a complete post on compiling the kernel from sources or how to flash a bootable image, checkout Mjanja’s blog
You will need the drivers as loadable modules so the .config file needs to be edited accordingly, you can use
# make menuconfig
Inside your kernel folder. then navigate to the respective driver and use M to set it to be compiled as a module. For more on configuration, read this
For a ready to flash bootable usb host mode image, check out sven's port with modules
Before you flash the phone, and if you dont have an SD card adapter, place all the modules on the sdcard in a folder. you will load them from there.
Ok, now that we have the new kernel flashed and reboot the phone according to Mjanja's post, we need to do a few things before we can test our hardware. First of all, plug in the USB bus to power and connect the USB hub together with your phones USB cable. I had a two port USB bus with a two wire connection to power. I extracted it from an old PC motherboard. Connect all your devices to the hub, i.e USB to serial, USB keyboard, USB mass storage, etc.
Here is a summary of the hardware:
You will need to load our modules systematically. For USB host mode, start with "usbcore.ko", then the host "ehci-hcd.ko", then load module for any drivers we want. For serial, we need usbserial.ko first, then the module for your specific USB to serial chip. I had a pl2303 so i loaded "pl2303.ko". Run the following codes line by line.
$su #cd /mnt/sdacrd/modules/ #insmod usbcore.ko #insmod ehci-hcd.ko #insmod usbserial.ko #insmod pl2303.ko
1) If you want to use a terminal similar to the one on your linux PC, download bash executable, place it in /system/bin/ , give it executable permissions with "chmod 777 /system/bin/bash". Then you will be able to run bash in terminal as follows
$su #bash bash-4.1# cd /mnt/sdcard/modules bash-4.1# insmod usbcore.ko
and so on... The main advantage of bash here is keeping history of commands so by using the up key on joystick, you can retrieve the previous command instead of typing everything on the tiny software keyboard.
2) Better yet, you can use a normal PC USB keyboard. Load the modules usbcore.ko, then ehci-hcd.ko, then usbhid.ko for human interface devices.
if you then plug in the keyboard on one of the hub ports, it will work.
3) To check which USB to serial chip you have, plug it onto a PC and go to terminal, run the command
4) After you load the first two modules, the phone should start charging through the bus. This is proof that the modules are loaded and a good sign its gonna work. you can use
#lsmod to list the loaded modules
If you list the devices after inserting usbserial.ko and pl2303.ko(or any other USB to serial module):
bash-4.1# ls /dev ttyUSB0 usbdev1.2 1-1 usbdev1.1 usb1 . . etc...
you will see the enumerated usb devices, including a "ttyUSB0". If you can see this, then you are just a few steps away from serial communication. To wrap it up on the serial port, we need to make it R/W
#chmod 777 /dev/ttyUSB0
For the hardware part, if you dont have a working prototype board, just short the tx and rx wires of the USB to serial, then you have a loopback test ready. I use this to test with one end before worrying about the other device/circuit.
I love python's simplicity
Now to the scripting part. this is the easiest for me. You'll need to install the scripting layer, the python for android apk, run it and install the latest python scripts. you will need the examples for a tutorial of how to use the android API in python. Then install the SL4A.apk and run it. I figured it runs as a service that starts python scripts. It lists the sample scripts added earlier and allows you to edit, save and execute them among other things. Back to the python for android app, go to loadmodule, load the pyserial.egg and install it. It must have been placed in the "downloads" folder in the sd card. Then to create a new script, you will need a text editor. Create a new file and save it as serio.py (whatever happens, dont save it as serial.py, that's the library's name and its a jealous one - will just import itself in the code below). add the following code in that file
import serial ser = serial.Serial("/dev/ttyUSB0") str = "hi" print str ser.write("hello") str = ser.read(5) print str ser.close()
The default settings for the port are: baudrate: 9600, parity:none, databits: 8, stop bits:1, so i did not have to configure anything. If you have a board with different serial settings, please read the pyserial documentation
go to options -> save and run, the output should be
dlopen libpython2.6.so hi hello
Next: The microcontroller side code and a sensible serial communication protocol.