Monday, June 3, 2013

Communication between Android and Arduino Due

This example demonstrate how to send String as byte[] between Android and Arduino Due, with ADK.

Communication between Android and Arduino Due
Communication between Android and Arduino Due

- In Android side (code will be available in next post):
User enter text on EditText on low part of the Android App and tap on the "Send to Arduino" button, the input text will be converted to byte[] and sent out. Any data received will be displayed on upper TextView.

- In Arduino side:
The data received from Android ADK will be sent back to Android.


#include "variant.h"
#include <stdio.h>
#include <adk.h>
#include <stdlib.h>

 
// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "AndroidADKCommunication"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino-er";

// Make up anything you want for these
char versionNumber[] = "0.1";
char serialNumber[] = "1";
char url[] = "https://sites.google.com/site/arduinosite/exercise/androidadkcommunication/AndroidADKCommunication_0.1.apk";
 
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
 
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
int analogInputA0 = A0;   //ADC input
int analogOutput0 = DAC0;  //DAC output
int analogOutput1 = DAC1;  //DAC output

int varA0;
uint8_t adkVarA0[4];
 
void setup() {
    Serial.begin(9600);
    cpu_irq_enable();
    
    analogReadResolution(12);  //set ADC resolution to 12 bits
   
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    delay(2000);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        delay(250);
        digitalWrite(led, LOW);
        delay(250);
    }
}
 
#define RCVSIZE 128
 
void loop() {

    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
   
    Usb.Task();
     
    if (adk.isReady()){
        digitalWrite(led, HIGH);
         
        adk.read(&nbread, RCVSIZE, buf);
        if (nbread > 0){
            adk.write(nbread, buf);
        }
         
    }else{
        digitalWrite(led, LOW);
    }    
}

Download>>

Related: Android side code for Communication between Android and Arduino Due

No comments:

Post a Comment