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
- terminal emulator
- Python for android.apk
- SL4A(scripting layer for android)
- Pyserial download
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:
- A usb hub with four ports
- A two port usb bus with a power connection on one side
- A homemade usb cable
- folded usb cable
- The cable connectors soldered and fixed nicely with hot plastic bonding
- the bottom side of the connectors
- a charger for huawei ideos phone
- connection of bus to source
- The connection between the powered usb bus and the usb to serial device
- powered bus connected to the phone
- connected usb bus with usb hub for connecting multiple devices to the phone
- A keyboard input device conneceted to the phone
- connected powered bus via PC usb
- z80 microcontroller board with a serial UART
- the z80 microcontroller
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
Tips:
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 $lsusb
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.















I am a technologist, love linux and android. I am into physics, embedded systems, and mobile and web technologies.
Awesome, dude. I’ve almost needed UART before when porting kernels to new devices, but never knew we could get the hardware here (leave alone how to use it).
btw, if you wrap your longer code blocks in a pre and a code (in that order), they’ll be set aside as blocks. The code block alone is better suited for inline monospace stuff.
UART is like the best form of raw communication you could hack in the worst case scenario. Am hoping this opens a way for micro-controller boot loaders for hardware hacking from the phone without a PC. like u wanna program hardware from the android device…hehe, could be nice tool for troubleshooting.
I checked out the “pre” tag, nice!! I am learning a lot lately.
Hi karibe, its great to see this post. I have one question, how to communicate with our own android application..? You implemented through pyserial with correct baudrate etc etc, Please guide me regarding this…? Currently I am doing it with android adk..
Hi Vinod, I do not know much about the ADK, i just thought that having a usb host enabled kernel makes it easy and trivial to add a serial port and as for the apk app, read this and of course there is this method.
Hi, very good work.It make your appreciate the power of open source especially the linux kernel.I’m eagerly waiting for the embedded board firmware, uhondoo uko hapo!!.
Also have tried it with the actel smartfusion SoC. I managed to get linux in it, which will make everything much simpler. Also consider the kinetics(freescale) board.
Right sir, open source is great.
So you mean the FPGA? thats nice, we will have to catch up on that, though am travelling…
Freescale kinetic boards bored me x10. They sold us the software so expensive, we will need to build a bootloader first then an eclipse plugin. Thats too much work considering we got LPCexpresso micros, which are also arm-based, with a Jtag and a full linux based Eclipse IDE, with drivers and everything. I’d rather invest my time there, don’t you agree? btw there were 15 freescale modules
Hi, It is posible to do the same on others andriods terminals like the Nexus 7? I mean, use usb host mode on andriod with pyserial.
Hi meldron, seems like Nexus7 supports USB host mode by default,according to this review. So if that is the case, jump the custom kernel stuff and do the rest. Besides that, I am not sure what you want to do exactly.
Hi:
I am using SL4A,but I can not install pyserial on the tablet. The regular pyserial is not suitable for the linux under android. How do you install the pyserial?
Get the compatible one, pyserial.egg
here
I have done it,3ks!
alright, glad to help