Turn your (Linux-) Desktop / Arduino Yún / Raspberry Pi into an USB Android Accessory: How to use the Android Accessory Protocol with pyusb

py_acc

It has been a while since the post where we explained how to Turn your Linux computer into a huge Android USB Accessory. In the former post, the process of creating a C-application to communicate with your Android device has been discussed. Today, we would like to pick up on the same topic, this time however showing how communication can be established with the “pyusb” library using Python.

Since devices like the Arduino Yún or the Raspberry Pi offer a fully implemented USB stack (based on the Linux Kernel and libusb) it becomes increasingly interesting to use Python for this task.

Continue reading “Turn your (Linux-) Desktop / Arduino Yún / Raspberry Pi into an USB Android Accessory: How to use the Android Accessory Protocol with pyusb”

Home Automation with Android and Arduino Yún

The Arduino Yún is a WLAN capable development board featuring an ATMega microcontroller, as well as a separate chip running a small Linux distribution, making it a perfect candidate for home automation projects like in the picture below! This basic tutorial will show you how to communicate wirelessly between your Arduino Yún and an Android device. Schematics and components for dimming a high power led are also available at the end of this post.

Dimming a high power led wirelessly from an Android device
Dimming a high power led wirelessly from an Android device

Continue reading “Home Automation with Android and Arduino Yún”

Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone

Intro

In the past two posts we have explained the basics of USB communication with the Arduino Uno (also applicable for Arduino Mega).

Galaxy Nexus to Arduino UNO via USB

In this post we’ll put everything together and show you how to communicate between your Android application and the Arduino using nothing but the Android USB host API. Remember, this approach has nothing to do with Android ADK! Unlike Android ADK, your Android device will act as the USB host, while your Arduino board will act as the USB device.

For the following application to work, you will require an Android device that supports USB host mode as well as the USB host API. Most Android 3.1+ tablets will suffice (some may require an USB OTG adapter). Also, the Galaxy Nexus has host mode enabled and matches the requirements (you will need an USB OTG adapter however).

This example consists of two parts:

  • The Android application that makes use of the USB API
    A simple Android app that let’s you regulate the brightness of an LED on the Arduino using a slider. It also features a button to “enumerate” the USB device.
  • Firmware for the Arduino that does some serial I/O with the Android app
    Very basic firmware for the Arduino. An interrupt is generated when a new byte is received. The received data controls the brightness of the Arduino’s on-board LED.
    (implemented via usleep-style software pwm in the main loop).

The Arduino firmware

In the main loop the firmware asserts and clears the LED pin of the Arduino (PB5). Here is a shortened excerpt:

int main(void) {
	//initialization
	initIO();
	uart_init();
	sei();

	uint8_t i = 0;
	volatile uint8_t pause;

	for(;;){//this is the main loop
		pause = data;
		PORTB |= (1 << LED);
		for(i = 0; i < pause; i++)
			_delay_us(10);
		PORTB &= ~(1 << LED);
		for(i = 0; i < 255-pause; i++)
			_delay_us(10);
	}
}

During a period of 2550[us], the LED is asserted for a duration of pause*10 [us] and cleared for (255-pause)*10[us]. Simply put, this is a very simple software PWM.

During that time, “data” and consequently “pause” may be changed within an interrupt routine form the serial USART port. This happens when the Android side sends data to the Arduino. The interrupt routine is extremely basic:

ISR(USART_RX_vect) {//attention to the name and argument here, won't work otherwise
	data = UDR0;//UDR0 needs to be read
}

The RX data has to be read in the ISR (interrupt service routine) from UDR0; have a look at the Atmega328P reference manual for further details. Since we are doing no multi-buffering shenanigans the handling is extremely simple (no need to call cli() or anything).

The rest of the code is initialization of the I/O pins and UART functionality. Download the complete example here: led_pwm.c

Controlled LED on the UNO by the firmware

 

The Android app

The Android application uses the basic knowledge of the preceding blog post Arduino USB transfers. During USB initialization, the Arduino USB serial converter is set up and after that, communication is done using the bulk IN endpoint of the very same serial converter.

With both the aforementioned firmware installed your Arduino board and the Android application installed on your phone or tablet, you will be able to control the brightness of the Arduino Uno’s built-in LED with a slider on your Android device. Again, please note that this will only work with devices that actually support both USB host mode (hardware, kernel requirement) as well as the Android USB host API (Android OS requirement).

The source code is available here: UsbController.tar.gz*
* You may need to change the PID value in UsbControllerActivity.java on line 38, if you have an Arduino Uno Rev3 or higher. You can check the VID/PID value with ‘lsusb’ after connecting the Arduino to your computer.

Many parts of the code are probably familiar to Android SW engineers. The most interesting section is in the class UsbController where the Arduino device is set up and communication is initiated. So let’s have a closer look at the inner class UsbRunnable within UsbController:

private class UsbRunnable implements Runnable {
	private final UsbDevice mDevice;

	UsbRunnable(UsbDevice dev) {
		mDevice = dev;
	}

	@Override
	public void run() {//here the main USB functionality is implemented
		UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
		if (!conn.claimInterface(mDevice.getInterface(1), true)) {
			return;
		}
		// Arduino USB serial converter setup
		conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
		conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
				0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

		UsbEndpoint epIN = null;
		UsbEndpoint epOUT = null;

		UsbInterface usbIf = mDevice.getInterface(1);
		for (int i = 0; i < usbIf.getEndpointCount(); i++) {
			if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
				if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
					epIN = usbIf.getEndpoint(i);
				else
					epOUT = usbIf.getEndpoint(i);
			}
		}

		for (;;) {// this is the main loop for transferring
			synchronized (sSendLock) {//ok there should be a OUT queue, no guarantee that the byte is sent actually
				try {
					sSendLock.wait();
				} catch (InterruptedException e) {
					if (mStop) {
						mConnectionHandler.onUsbStopped();
						return;
					}
					e.printStackTrace();
				}
			}
			conn.bulkTransfer(epOUT, new byte[] { mData }, 1, 0);

			if (mStop) {
				mConnectionHandler.onUsbStopped();
				return;
			}
		}
	}
}

After the USB interface has been claimed the Arduino USB serial converter is initialized by issuing the following control transfers:

conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
				0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

The first call sets the control line state, the second call sets the line encoding (9600, 8N1).
For communication, an additional thread is used to send data without blocking the Activity’s main UI thread. By notifying sSendLock of the UsbController the data will be transferred. After submission, the thread will go into “wait” again. This way, even if submission takes more time than expected, the Activity’s main thread will not be blocked and hence the app will not become unresponsive.

Screenshot of the Android App

Also note that in the Android Manifest none of the XML-style device filters are needed, since enumeration happens by the user in the app when pressing the “enumerate” button. Device filters – and therefore automatic activity launch when connecting the Arduino – are not used in this example in order to make the code simpler to comprehend. However, this could be easily implemented with a few lines of additional code.

For developing this example we have used a Galaxy Nexus Phone with an USB-OTG adapter cable. It has also been successfully tested with an Android Tablet, the Acer Iconia Tab A500, this tablet does not need any additional adapter cables.

This post concludes the 3-Part Arduino USB communication series. Feel free to post any questions or feedback/ideas in the comments section or contact us via E-Mail (http://www.nexus-computing.ch).

All code you find in this post can be used under GPL for your own projects.

The following section “About Android and USB Host” again concludes why USB-Host is becoming more and more important for mobile devices and points out main differences between Android ADK (Android Accessory Development Kit) and the Android USB Host API.

EDIT: control transfers for FTDI equiped Arduinos
Since we got requested a lot if the FTDI Usb-Serial converter will work too, here ist the control transfer code that needs to be exchanged. No warranties though 🙂

conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);

Source Links

Android App: UsbController.tar.gz*
Arduino main: led_pwm.c

* You may need to change the PID value in UsbControllerActivity.java on line 38, if you have an Arduino Uno Rev3 or higher. You can check the VID/PID value with ‘lsusb’ after connecting the Arduino to your computer.

About Android and USB Host

Lately it has become more and more popular to use tablets or mobile phones to communicate with the outside world over USB. After all, many devices now feature a full USB Host, either with a USB-OTG converter or even with a full sized USB Type A interface. Also, the future promises even more host availability on mobile phones. This opens up an entire range of new possibilities for already existing hardware as well as newly designed hardware for phones.

Not too long ago, Android received its own USB API. It was at the Google I/O in summer 2011 when the “Android Open Accessory Development Kit” was announced and released. However it lacked of a few crucial points. The API implied that there is an external USB host, acting as a “master” if you will.
On the one hand, this means that an already existing USB device mode gadget cannot work with your Android device. Therefore, if a manufacturer wanted to support Android phones it was necessary to create new hardware as well as new firmware. Secondly, the new hardware had to be designed to power itself and also deliver power the Android device; this implied that mobile gadgets require their own power source. And secondly, there was another issue: only devices running Android 2.3.4+ were shipped with the new API. Even then, it was up to the manufacturer to actually include the required stack in the OS.

But wait, there is another way to communicate over USB. At the same time (Google I/O) the Android USB Host API has been published for Android 3.1+. Starting with the second half of 2011, Android devices appeared which supported USB OTG (although devices with Android version < 3.1 were not supporting the API). With an appropriate adapter it has become possible for Android to act as USB Host. This meant that already present USB hardware has become compatible with the OS. As long as the kernel on the Android device supported the USB standard driver of the hardware (mass storage, input, etc.), Android would be able to use it and therefore open up a new range of extra devices compatible with the system.

However, there are many devices that have not been “compatible” from the beginning. For instance, let’s say your common RFID reader. It most likely uses a USB-serial port and probably comes with a Linux or Windows driver as well as some software. Most Android tablets will come without the usb-serial driver for your RFID reader however. Therefore, if you want to load your driver you will need to root your tablet, determine the version of your current kernel, find the kernel sources online, hope that everything compiles to have your driver ready and then load it onto your tablet. In the end, when you finally have your kernel driver running, you will be required to write C code as well as some JNI glue to communicate with your Activity or Service in Android. All in all, this approach is not very straightforward.

Writing your own USB soft driver

There is a very elegant solution to aforementioned problem. It requires far less skills in hacking and porting than the mentioned approach. However, you will require some advanced knowledge in Android programming as well as some USB know-how.
You can write your own “soft driver” in Android. Since the USB Host API has been released, it is now possible to communicate with any USB device using the most commonly seen USB transfers (control, interrupt, bulk). In the end, your result will be portable across all Android devices that have USB host enabled and have Android version 3.1+. Moreover, this solution does NOT require root access to the tablet or phone. It is currently the only viable solution that does not require the user to have any know-how of rooting/hacking the device and risk losing warranty in the process.
The above Android application uses exactly this approach. It represents a very basic soft driver for the Arduino’s on-board USB-to-serial converter.

Programming the Arduino with Eclipse on Linux

This is the first of a series of three posts. The ultimate goal is to setup a communication interface between an Arduino Uno/Mega board and an Android tablet over USB. Everything will be as user-friendly as possible, i.e. no root will be required on your Android tablet. The following posts Arduino USB transfers and Android USB Host + Arduino: How to communicate without rooting your Android Tablet or Phone conclude the series.

The Arduino Uno is a popular and affordable hardware platform which comes with its own IDE and core libraries for programming. If you like the Arduino IDE you may skip this post, however, if you feel more comfortable developing in Eclipse this might help you out.
We are using Ubuntu 11.10 (oneiric ocelot) and Eclipse 3.6.1 (helios), although this should not play an important role (as it turns out, Eclipse Indigo might need a few tweaks, check the end of this post). Also, setting up Eclipse for programming the Arduino Uno Atmega328P is pretty straightforward. Nevertheless, here is a quick how-to for the Arduino Uno (Arduino Mega 2560 below):

  1. Install avrdude and AVR libraries:
    sudo apt-get install gcc-avr avr-libc avrdude
  2. Start Eclipse and install CDT Plugin (C/C++ Development Tools):
    • Help -> Install New Software…
    • Work with: (your current Eclipse version)
      i.e. “Helios – http://download.eclipse.org/releases/helios”
    • Download the “pending” software package (don’t worry, download starts automatically 😉 )
    • Choose “Programming Languages” and select “C/C++ Development Tools”
    • Accept and continue by restarting Eclipse
  3. Install AVR Eclipse Plugin:
    • Help -> Install New Software…
    • Add new repository: http://avr-eclipse.sourceforge.net/updatesite/
    • Re-download the “pending” software package, download will be faster since it is probably cached 😀
    • Download AVR Eclipse Plugin and restart Eclipse
  4. Create new C project named “BlinkBlink”:
    • Project Type AVR Cross Target Application (Empty Project, AVR-GCC Toolchain)
    • Click next…
    • Untick “Debug” (in Debug mode, no hex-files are generated and avrdude can’t flash the device)
    • Click Advanced settings…
    • AVR -> AVRDude -> Programmer configuration…
    • Create a new programmer and name it “Arduino Uno”. Make sure this newly created programmer configuration is selected for the current project.
      • Programmer Hardware: Arduino
      • Override default port: /dev/ttyACM0 or similar
      • Override default baudrate: 115200
    • AVR -> Target Hardware:
      • MCU Type: ATmega328P (or load from MCU)
      • MCU Clock Frequency: 16000000 (default external clock source of Arduino Uno)
  5. Click Apply and OK to leave the properties window and click Finish to create the new project in the workspace.
  6. Create a new source file main.c and copy the contents from this link. Make sure to save main.c before proceeding (File -> Save).
  7. Project -> Build Project
  8. Click on the AVR Button within Eclipse to upload the generated hex file from BlinkBlink/Release/BlinkBlink.hex.
    Your Arduino Uno’s LED should be blinking on and off repeatedly. If somehow it doesn’t work, right-click your Project and select Properties. Make sure all AVR and Programmer settings are active as mentioned above.

For the Arduino Mega 2560 you should choose Atmel STK500 Version 2.x firmware as Programmer Hardware, and ATMega2560 as target hardware, the rest is the same as with the Arduino Uno. Also, if you are using the above source file for testing, you should change the definition of LED from PB5 to PB7, since the LED on the Arduino Mega is on Pin7 of Port B.

Update: if you are using Eclipse Indigo, some specific AVR symbols such as DDRB (data direction register of port b) may not be recognized.
To solve this problem go to preferences…

  • C/C++
  • Language Mappings
  • Add the following mappings:
    • Content Type: C Header File / Language: GNU C
    • Content Type: C Source File / Language: GNU C
  • Make sure to click on Apply afterwards… also you may need to restart eclipse after adding the mappings

Your Language Mapping preference window should look like the following screenshot:

If it still does not work, also try adding this line at the beginning of your main.c source file:
#include <avr/iom128.h>