last week I have bought an OTG cable so I can interface my Android devices (Galaxy Nexus, Xoom) with the Arduino board.
My example is to turn on and off the LEDs located on Arduino by an Android device!
What we need:
- Arduino Uno board
- Android devices (version 3.0 or higher)
- Micro USB OTG to USB Adapter (thanks to Amazon!)
- Arduino Uno Communicator (avaiable on playstore)
- 3x leds
- 3x resistors
- 4x cable connection strips
- Breadboard
So make the connection as shown in the picture:
Now we have to load this code in Arduino:
/* * Arduino LEDs Control * */ int val = 0; // variable to store the data from the serial port int ledPin1 = 2; // LED connected to digital pin 2 int ledPin2 = 3; // LED connected to digital pin 3 int ledPin3 = 4; // LED connected to digital pin 4 int ledPin4 = 5; // LED connected to digital pin 5 int ledPin5 = 6; // LED connected to digital pin 6 void setup() { pinMode(ledPin1,OUTPUT); // declare the LED's pin as output pinMode(ledPin2,OUTPUT); // declare the LED's pin as output pinMode(ledPin3,OUTPUT); // declare the LED's pin as output pinMode(ledPin4,OUTPUT); // declare the LED's pin as output pinMode(ledPin5,OUTPUT); // declare the LED's pin as output Serial.begin(9600); // connect to the serial port } void loop () { val = Serial.read(); // read the serial port if (val !=-1){ if (val=='1'){ digitalWrite(ledPin1,HIGH); } else if (val=='A'){ digitalWrite(ledPin1,LOW); } if (val=='2'){ digitalWrite(ledPin2,HIGH); } else if (val=='B'){ digitalWrite(ledPin2,LOW); } if (val=='3'){ digitalWrite(ledPin3,HIGH); } else if (val=='C'){ digitalWrite(ledPin3,LOW); } if (val=='4'){ digitalWrite(ledPin4,HIGH); } else if (val=='D'){ digitalWrite(ledPin4,LOW); } if (val=='5'){ digitalWrite(ledPin5,HIGH); } else if (val=='E'){ digitalWrite(ledPin5,LOW); } //Serial.println(); } }
This code is really easy to understand: if you get the string "1" turns ON the LED on pin 2, if you get the string "2" turns ON the LED on pin 3 ... and so on. If you get the string "A" turns OFF the LED on pin 2, if you get the string "B" turns OFF the LED on pin 3... and so on.
And this is the code for the Android app:
package com.spinettaro.arduinoledscontrol; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; import android.widget.Toast; public class MainActivity extends Activity implements OnCheckedChangeListener { // set in an array the id of every switch private final static Integer[] ids = { R.id.switch1, R.id.switch2, R.id.switch3, R.id.switch4, R.id.switch5 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerLedChangeListener(ids); } //register all onCheckedChangeListener for all Switch private void registerLedChangeListener(Integer... ids) { for (int i = 0; i < ids.length; i++) { Integer id = ids[i]; Switch led = (Switch) findViewById(id); led.setOnCheckedChangeListener(this); } } public void loop(View v) { try { // gone sendString("1"); Thread.sleep(50); sendString("A"); Thread.sleep(50); sendString("2"); Thread.sleep(50); sendString("B"); Thread.sleep(50); sendString("3"); Thread.sleep(50); sendString("C"); Thread.sleep(50); // return sendString("3"); Thread.sleep(50); sendString("C"); Thread.sleep(50); sendString("2"); Thread.sleep(50); sendString("B"); Thread.sleep(50); sendString("1"); Thread.sleep(50); sendString("A"); Thread.sleep(50); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.getId()) { case R.id.switch1: if (isChecked) sendString("1"); else sendString("A"); return; case R.id.switch2: if (isChecked) sendString("2"); else sendString("B"); break; case R.id.switch3: if (isChecked) sendString("3"); else sendString("C"); break; default: break; } } private void sendString(String toSend) { Intent i = new Intent("primavera.arduino.intent.action.SEND_DATA"); i.putExtra("primavera.arduino.intent.extra.DATA", toSend.getBytes()); sendBroadcast(i); } }
Here there's the WOW effect! Because thanks to the IPC Android system I can communicate with Arduino through the use of broadcast intent, because the part of interfacing with the ADK was handled by Arduino Uno Communicator. To send data to Arduino from your application it's only necessary broadcasting an intent with action "primavera.arduino.intent.action.SEND_DATA". Add the data to be sent as byte array extra "primavera.arduino.intent.extra.DATA".
And this is the video of the demo:
Here you can find the full source code for Android application.
hello!
ReplyDeleteWhat is the program used to create the android application?
This ap is created uner windows or under android?
Thanks
This app was created under Windows, using Eclipse IDE with ADT. If you need anything else let me know... :)
Deletehi
ReplyDeletenice work, can you send me full android source(with xml file)?
novendeni@gmail.com
thank's
Hi yo_gie, I just updated the post with the link to download the code for Android. This is the link: http://www.mediafire.com/download.php?w7aao02b45l212h
DeleteHi,
ReplyDeleteCan you show me how do I add TCP/IP coding in Android. I need Android to be the client and arduino+Xbee WiFi as Server to received command.
thanks for your share :) but your also uploaded android code is doesnt open. your file crashed or die. do you check for me ?
ReplyDeleteDo you have instructions on how to receive characters that the Arduino Uno Communicator app has gotten from the Arduino within my own app?
ReplyDeleteYou can use a Broadcast Receiver: " Let your own Android application receive data from Arduino by listening to the "primavera.arduino.intent.action.DATA_RECEIVED" intent. This intent will contain the "primavera.arduino.intent.extra.DATA" byte array with the received data. Call getByteArrayExtra("primavera.arduino.intent.extra.DATA") to retreive the data ". Text taken from the official page project https://github.com/jeppsson/Arduino-Communicator
DeleteThis comment has been removed by the author.
ReplyDeleteyou can integrate the usb host comunication cod in your app.. just see the arduino uno comunicator for full code example. in this way you have not the needing to use an external app to use your one ;-)
ReplyDelete