30 Nov 2013 Physical Android Notifier
 |  Category: Electronics  | Tags: ,

I wanted to create a simple notifier for my Android phone. Something that would blink whenever I have a missed call, or sms or whatsapp notification.

The hardware part was easy:

  1. Funduino Mini Pro
  2. Common Anode RGB LED
  3. Switch
  4. Bluetooth Breakout Board

Schematic:

Android Notifier

The Arduino software was also easy:

/*

  ClickButton: https://code.google.com/p/clickbutton/
  SerialCommand: https://github.com/kroimon/Arduino-SerialCommand

*/

#include 
#include 
#include 

#define IDLE 0
#define REDLED 11
#define GREENLED 10
#define BLUELED 9
#define SWITCHPIN 12

SerialCommand sCmd;     // The SerialCommand object
ClickButton button1(SWITCHPIN, LOW, CLICKBTN_PULLUP);
boolean wa, sms, missed, ledState;
Color cur_color = Color(1,1,1);
float hue = 0;

void setup()  
{
  Serial.begin(9600); 
  // Setup callbacks for SerialCommand commands
  sCmd.addCommand("MISSED", missedCall);
  sCmd.addCommand("SMS", smsOn);
  sCmd.addCommand("WHATSAPP", whatsapp);
  sCmd.addCommand("MISSEDOFF", missedOff);
  sCmd.addCommand("SMSOFF", smsOff);
  sCmd.addCommand("WAOFF", waOff);
  pinMode(REDLED, OUTPUT);
  digitalWrite(REDLED, HIGH);
  pinMode(BLUELED, OUTPUT);
  digitalWrite(BLUELED, HIGH);
  pinMode(GREENLED, OUTPUT);
  digitalWrite(GREENLED, HIGH);
  wa = 0;
  sms = 0;
  missed = 0;
  ledState = 0;
}

void loop()
{
  sCmd.readSerial();
  button1.Update();
  if(button1.clicks >= 1) ledState = !ledState;
  if(ledState) rainbow();
  if(!ledState) {
  for(int i=0;i<missed;i++){
    digitalWrite(REDLED,LOW);
    delay(300);
    digitalWrite(REDLED,HIGH);
    delay(300);
  }
  for(int i=0;i<sms;i++){
    digitalWrite(GREENLED,LOW);
    delay(300);
    digitalWrite(GREENLED,HIGH);
    delay(300);
  }
  for(int i=0;i<wa;i++){
     digitalWrite(BLUELED,LOW);
     delay(300);
     digitalWrite(BLUELED,HIGH);
     delay(300);
   }
   }
 }
void missedCall() {     missed += 1; } 
void smsOn() {     sms += 1; } 
void whatsapp() {     wa += 1; } 
void missedOff() {     missed = 0; } 
void smsOff() {    sms = 0; } 
void waOff() {     wa = 0; } 

void rainbow(){   hue += 0.06;   if ( hue >=1 ) hue = 0;
  float sat = 1.0;
  float val = 0.4;
  cur_color.convert_hcl_to_rgb(hue,sat,val);
  display_color(cur_color);
  delay(20);
}

void display_color(Color c){
  analogWrite(REDLED, 255-c.red);
  analogWrite(GREENLED, 255-c.green);
  analogWrite(BLUELED, 255-c.blue);
  delay(100);
}

It was the Android part that simply killed me… I wanted a simple application, that would send a serial command to the arduino whenever there was a missed call. Thought of writing an android app (how hard could it be)… realised very soon that “very” if you don’t know how…

Then tried looking for existing apps which could help, helpful people suggested Amarino, however I couldn’t get it to work on my mobile – it kept crashing. Long story short – landed up on a google groups page where Jonathan Ulmer talked about SL4A and Tasker – he had also provided code for the same. This looked like the easiest option for a beginner programmer like me.

There were a few hiccups here as my phone did not support IPv6 and the default version of SL4A uses IPv6; again google came to my rescue and I was able to install the correct version – it’s called sl4a_r6x

The next step was to install python through sl4a and load the following python code:

#!/usr/bin/python
# coding=utf-8

import android
import time

droid = android.Android()

uuid='00001101-0000-1000-8000-00805F9B34FB' #couldn't get bluetooth to work without this uuid..
mac='20:13:05:xx:xx:xx' #enter your bluetooth module mac address

connId=droid.bluetoothConnect(uuid,mac).result #connect to bluetooth module mac specified above
command = droid.getIntent().result[u'extras'][u'%ARDUINO']
print command
droid.bluetoothWrite(command)
droid.bluetoothWrite('\n')

You need to place the file with the python code in the \sdcard\sl4a folder

Next is to setup Tasker – please read through a few tutorials if you’re not familiar with tasker. The basic steps for this are:

  1. Add profile -> Event -> Phone -> missed call
  2. Add Action:
    1. Net -> Bluetooth -> On
    2. Variable Set -> %ARDUINO to MISSED
    3. Script -> Run Sl4A Script -> arduino1.py; pass variable %ARDUINO
  3. Add profile -> Application -> Phone
  4. Add Action:
    1. Net -> Bluetooth -> On
    2. Variable Set -> %ARDUINO to MISSEDOFF
    3. Script -> Run Sl4A Script -> arduino1.py; pass variable %ARDUINO

Now if only someone with Android programming experience would make a tasker plugin that sends a serial command to the bluetooth module – we can get rid of the entire scripting layer and make the work easier (hint hint)

The final product:

IMG_20131201_101421

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 Responses

  1. 1
    Yash Garg 

    Superb!

  2. 2
    Yash Garg 

    Also, +1 for the very helpful links! 🙂 Please keep sharing such stuff. I’m also working on something similar (using the MIT App Inventor tho), I’ll try and head back here once it’s done. Cheers!

  3. 3
    Danny 

    Hey there,
    Your code has helped me to be able to send a serial command to my Arduino from my Android phone. I’m still surprised there isn’t a Tasker plugin for this yet. My only problem is that the script works fine when it’s first ran. After that, the script just exits quickly after being ran and usually says “Bluetooth not ready”, I think. If I unplug and plug my Arduino back in, the script will run just once again. Any idea why this is happening?

    Thanks!

  4. 4
    Dushyant 

    Hi Danny, Try using Bluetooth Auto-Connect. This app auto-connects to the BT module; that may help. Can you open the script in the terminal and see what errors come up?

  5. 5
    Thein Lin Aung 

    Mine says “SyntaxError: invalid syntax” at
    command = droid.getIntent().result[u’extras’][u’%ARDUINO’] b
    line. I have the same code as yours and I have filled the mac address.
    I have Phython3 installed with version Bin: 10 Extra: 11 Scripts: 11
    and SL4A r6*03.

  6. 6
    Dushyant 

    Somehow the code is broken now – have no idea why.. Can anyone help me in creating a tasker plugin…

  7. 7
    Shawn Bashir 

    I take it this tutorial no longer works? I really wish there was a tasker plugin for serial bluetooth (i.e. Arduino Android) communication.

Leave a Reply