Programming objects with android

45
Home automation, Digital signage and Smart Object using Android www.udoo.org Maurizio Caporali Luca Pisani Programming things with Android: Mobile&Embedded Firenze

description

Programming Objects with Android Maurizio Caporali - "Mobile & Embedded" - GDG-Firenze

Transcript of Programming objects with android

Page 1: Programming objects with android

Home automation, Digital signage and Smart Object using Android

www.udoo.orgMaurizio Caporali

Luca Pisani

Programming things with Android:

Mobile&Embedded Firenze

Page 2: Programming objects with android

www.udoo.org

Programming things?!?

Douglas Engelbart

Xerox Alto

Page 3: Programming objects with android

www.udoo.org

Developers are changing

Douglas Engelbart

Xerox Alto

Programming Things

Xerox Alto

Programming Desktop Computer

Programming Mobile Device

Prototyping Lab, FabLab, Hackathon

Page 4: Programming objects with android

www.udoo.org

Computer Interaction is changing

Screen interaction

Interactive Things

Douglas Engelbart

Xerox Alto

Page 6: Programming objects with android

www.udoo.org

Programming Things

Electronic Engineer, Mechanical Engineer, Computer

Engineer, ...

Douglas Engelbart

Xerox Alto

It’s a complex activity

Page 7: Programming objects with android

*

Programming Things: rapid prototyping tools

Arduino

Page 8: Programming objects with android

www.udoo.org

Open Source

Douglas Engelbart

Xerox Alto

How to create the interaction

Open Source Software

Linux

Android

Community

Page 9: Programming objects with android

www.udoo.org

Open Hardware

How to create Physical Mock Up quickly

Douglas Engelbart

Xerox Alto

Arduino

CommunityOpen Source Hardware

Raspberry Pi3D Printing

Page 10: Programming objects with android

www.udoo.org

Douglas Engelbart

Xerox Alto

Makers Revolution

Communities make this success real!

Page 11: Programming objects with android

*

Arduino + Android: Android ADK

Page 12: Programming objects with android

*

Applications

Ambient Devices

Home Automation

Smart Cities

Automotive

Urban Furniture

Digital Signage

Robot

Page 13: Programming objects with android

www.udoo.org

What do you need?

+Android Smartphone/Tablet Arduino DUE

or

UDOO+ARM single board computer Arduino DUE

Page 14: Programming objects with android

www.udoo.org

What’s UDOO?

UDOO is a mini PC that could run either

Android or Linux, with an Arduino-

compatible board embedded.

UDOO is a collective effort of a

multidisciplinary team spread between

North America and Europe, with expertise

in interaction design, embedded electronics

and sensor networks.

UDOO is a co-founded project by SECO

(www.seco.com) and Aidilab (www.aidilab.

com).

Page 15: Programming objects with android

www.udoo.org

All-in-one solution

For the first time, a powerful ARM

cortex-A9 CPU for heavy tasks

with great performance, and a

dedicated ARM processor for the

GPIO are brought together in one

single board computer.

Page 16: Programming objects with android

www.udoo.org

Arduino-compatible

UDOO is compatible with all the

sketches, tutorials and resources

available on the Arduino

community as well as all the

shields, sensors and actuators for

Arduino DUE available on the

market.

Page 17: Programming objects with android

www.udoo.org

Open Source and Community

UDOO is a proper open source

project where both hardware

designs and software are open

source.

Community development support

Schematics, 2D/3D drawings,

Kernels and U-Boot sources are

available to the community in order

to improve and extend UDOO

capabilities and functionalities.

Page 18: Programming objects with android

www.udoo.org

Power and flexibility for any kind of projects

Page 20: Programming objects with android

www.udoo.org

Android on UDOO

Android 4.2.2 Jelly Beans runs

smoothly on UDOO giving you all

the features of an Android device.

Apps interface with Arduino-

compatible embedded board

through Accessory Development

Kit (ADK) connection for building

accessories and smart devices

based on Android.

Page 22: Programming objects with android

www.udoo.org

Program Arduino App for UDOO

To program UDOO under Android we need the standard

tools:

● Eclipse + ADT plugin

● Android SDK Tools

● Android Platform-tools

We also need:● Arduino IDE 1.5.4● UDOO patch for Arduino IDE (bossac programmer)

Page 23: Programming objects with android

www.udoo.org

UDOO Overview

● Android 4.2.2 runs on Freescale i.Mx6 (dual or quad core - 1 GB)

● UDOO is equipped with micro Atmel SAM3X, the same of Arduino Due

● They are connected through bus USB OTG

● We program the two CPUs separately using Android SDK and Arduino IDE

Page 24: Programming objects with android

www.udoo.org

UDOO Overview OTG - bus

● To allow the communication between Android and the SAM3X the ADK protocol is needed. A set of libraries that authorize Android to communicate with an external accessory.

● It's basically a bidirectional stream of data between the two parts

● Usually to communicate, install applications and debug the ADB protocol is used through the USB OTG port on the android device.

● Also the microcontroller communicates using the OTG bus

Page 25: Programming objects with android

www.udoo.org

UDOO Overview OTG - bus switch

The i.Mx6 processor can be connected with:● the external USB port (debug app Android)● the SAM3X microcontroller (usage)It’s possible to switch via software between the 2 options

Page 26: Programming objects with android

www.udoo.org

Programming Arduino

We need to program SAM3X microcontroller with Arduino IDE downloadable from arduino.cc

The Sketches have a standard structure, we only need to add a library for OTG communication.

We program the SAM3X through microUSB serial port.

Page 27: Programming objects with android

www.udoo.org

Programming Overview

Page 28: Programming objects with android

www.udoo.org

AndroidManifest.xml

● Include a <uses-feature> element that declares that your application uses the accessory feature.

<uses-feature android:name="android.hardware.usb.accessory" />

● Declare min(12) e target(17) SDK version

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" />

Page 29: Programming objects with android

www.udoo.org

AndroidManifest.xml

● If you want your application to be notified of an attached USB accessory, specify an <intent-filter> and<meta-data>

<activity

……

<intent-filter>

<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />

</intent-filter>

<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"

android:resource="@xml/accessory_filter" />

</activity>

Page 30: Programming objects with android

www.udoo.org

accessory_filter.xml

In the XML resource file, declare <usb-accessory> elements for the accessories that you want to filter. Each<usb-accessory> can have the following attributes:● manufacturer● model● version

<?xml version="1.0" encoding="utf-8"?>

<resources>

<usb-accessory manufacturer="Aidilab" model="UDOO_ADK" version="1.0" />

</resources>

Page 31: Programming objects with android

www.udoo.org

Activity.javaThe package you need to import is android.hardware.usb. This contain the classes to support the accessory mode

import android.hardware.usb.UsbAccessory;

import android.hardware.usb.UsbManager;

Here is a summary of the relevant code for handling USB connections:

mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE);

...

UsbAccessory acc = (UsbAccessory)intent.getParcelableExtra(UsbManager.

EXTRA_ACCESSORY);

Page 32: Programming objects with android

www.udoo.org

Activity.javaTo explicitly obtain permission, first create a broadcast receiver. This receiver listens for the intent that gets broadcast when you call requestPermission(). The call to requestPermission() displays a dialog to the user asking for permission to connect to the accessory

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (ACTION_USB_PERMISSION.equals(action)) {

synchronized (this) {

UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

if (intent.getBooleanExtra(

UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

openAccessory(accessory);

} else {

Log.d(TAG, "permission denied for accessory "+ accessory);

}

mPermissionRequestPending = false;

}

}

Page 33: Programming objects with android

www.udoo.org

Activity.java

To register the broadcast receiver, put this in your onCreate() method in your activity:

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent

(ACTION_USB_PERMISSION), 0);

IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);

registerReceiver(mUsbReceiver, filter);

To display the dialog that asks users for permission to connect to the accessory, call the requestPermission() method:

mUsbManager.requestPermission(accessory,mPermissionIntent);

Page 34: Programming objects with android

www.udoo.org

Activity.java

To communicate with the accessory you need a file descriptor to set up input and output streams to read and write data:

private void openAccessory(UsbAccessory accessory) {

mFileDescriptor = mUsbManager.openAccessory(accessory);

if (mFileDescriptor != null) {

mAccessory = accessory;

FileDescriptor fd = mFileDescriptor.getFileDescriptor();

mInputStream = new FileInputStream(fd);

mOutputStream = new FileOutputStream(fd);

}

}

Page 35: Programming objects with android

www.udoo.org

Activity.java

We use the output and input stream to send and receive messages to the Arduino processors of UDOO.

byte[] message = new byte[1];

message[0] = (byte)1;

mOutputStream.write(message);

...

byte[] buffer = new byte[4];

running = true;

ret = mInputStream.read(buffer);

Page 36: Programming objects with android

www.udoo.org

Programming Arduino

To program SAM3X we need to download Arduino IDE based on Processing project.

Arduino programming language is based on C / C++ with specific objects that provides main arduino’s functions.

It provides an interface with the microcontroller low level C programming language.

It allows to avoid “unfriendly” low level difficulties (write or read registers, manage signals timings, non objects).

Page 37: Programming objects with android

www.udoo.org

Programming Arduino

Arduino sketches have a defined structure based on two main functions:- void setup()- void loop()

Setup method is executed at the beginning of the sketch, and it’s used to initialize objects.

Loop method is executed continuously after first setup execution. It contains the core of the code.

Page 38: Programming objects with android

www.udoo.org

Programming Arduino

Example for blinkin LED: int led = 13;

void setup() {

pinMode(led, OUTPUT);

}

void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Page 39: Programming objects with android

www.udoo.org

Arduino .ino

The accessory code must make a few calls to initialize USB connectivity, including setting the accessory identification strings:

char descriptionName[] = "UDOOAndroidADKDemo";

char modelName[] = "UDOO_ADK";

char manufacturerName[] = "Aidilab";

char versionNumber[] = "1.0";

char serialNumber[] = "1";

char url[] = "http://www.udoo.org";

USBHost Usb;

ADK adk(&Usb, manufacturerName, modelName, descriptionName, versionNumber, url, serialNumber);

Page 40: Programming objects with android

www.udoo.org

Arduino .ino

read data from Android App into buffer:

void loop(){

uint8_t bufRead[BUFSIZE];

uint32_t nbread = 0;

uint8_t bufWrite[1];

if (adk.isReady()) {

adk.read(&nbread, BUFSIZE, buf); // read data into buf array

if (nbread > 0) {

... // do stuff

Page 41: Programming objects with android

www.udoo.org

Arduino .ino

write data to Android App:

void loop(){

uint8_t bufWrite[BUFSIZE];

...

...

adk.write(sizeof(bufWrite), (uint8_t *)bufWrite);

Page 42: Programming objects with android

www.udoo.org

Android ADK useful links

● Android ADK Documentation

http://developer.android.

com/guide/topics/connectivity/usb/accessory.html

http://developer.android.com/tools/adk/adk2.html

● Toolkit to help beginners to be up and running with ADK 2012:

https://github.com/palazzem/adk-toolkitby Emanuele Palazzetti (GDG Perugia)

Page 43: Programming objects with android

www.udoo.org

Arduino Resources & Community

Downloads:http://arduino.cc/en/Main/Softwarehttp://www.udoo.org/downloads/

Resources:http://arduino.cc/en/Tutorial/HomePagehttp://arduino.cc/en/Reference/HomePage

Community:http://forum.arduino.cc/more… more.. and more

Page 44: Programming objects with android

www.udoo.org

Projects

Page 45: Programming objects with android

www.udoo.org

Community: www.udoo.org

• Informative website

• Forum

• Tutorials

• Projects

• Wiki

• Support

• Faq