Sunday, July 19, 2015

Serial communication between Arduino and Android

I wanted to establish communication between an Arduino Uno board and an Android device for a project work. Initially I tried usb-serial-for-android library and reached to some level. I was able to read some amount of bytes from the Arduino board. However due to some reason, my Android device disconnects from the Arduno serial connection after a while. In order to try that, I followed the steps mentioned here. It's sad that I spent a whole day trying to make it work but finally failed. Perhaps this library actually works but due to some mistake I made, it's still not ready to work. Anyway, I'm leaving it to try later some other day if I find time.

It took me some time to realize that there are other libraries available to achieve the same thing. Since I ran out of time, I didn't get a chance to try them from scratch but I decided to try some sample apps they provide. These guys in Physicaloid provides an interesting library and sample projects which actually worked for me. Their PhysicaloidLibrary includes sample project in this place and some of those are in google play already.

I installed their USB Serial Monitor Lite app in google play into my Galaxy S3 device which runs Android 4.4.4. Then I installed following simple Arduino sketch into an Arduino Uno board. Connecting the two devices with the help of a USB OTG cable showed me that Arduino board responds to the messages from Android phone by blinking the LED at pin 13 and also by sending reply data.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static int counter = 0;
int led = 13;

void setup() {
        Serial.begin(115200);
        pinMode(led, OUTPUT);     
}

void loop() {
  
  digitalWrite(led, LOW);

  if (Serial.peek() != -1) {
          
          digitalWrite(led, HIGH);
        
        do {
                byte message = (byte) Serial.read();
                //Serial.print("Received: ");
                
                if(message == (byte) '1'){
                        Serial.println("1");
                } else {
                        Serial.println("-");
                }                                                         
        } while (Serial.peek() != -1);     
        
  }
  delay(1000);
}


I think I should look into the source code of this app which can be found here in order to adapt it into my work requirements.

No comments:

Post a Comment