Bluetooth Serial Data Write...

2,934 views
Skip to first unread message

Philipp Protschka

unread,
Feb 27, 2013, 6:58:56 AM2/27/13
to tas...@googlegroups.com
Hi guys I am trying to gather some information on how I could use tasker to send serial data/messages via bluetooth.

Background: I have an arduino with a bt shield and want to send it serial commands. Now this is working in various terminal emulators but obviously it would be nice to have some tasker interaction.

Short of opening the app and emulating key and pad presses I don't know what to-do...that approach is pretty slow and lame. 

If anybody has ideas please let me know...


TomL

unread,
Feb 27, 2013, 7:31:11 AM2/27/13
to tas...@googlegroups.com
Have a look at this thread.  It's long, but I think it's a step in the right direction.


The basic idea is:

1. use Tasker's RunShell Action to ...
2. launch an installed ssh client that ...
3. connects to a remote host (your Arduino) and sends a string of keystrokes

Tom

Philipp Protschka

unread,
Feb 28, 2013, 8:58:16 PM2/28/13
to tas...@googlegroups.com
thx for your reply. I red the topic and only understood how they call BTEP...but it didnt make it clear to me how to connect to a bluetooth module.. :(

i have everything set up on the arduino and manually connecting and typing it various bt terminal software works fine. just lookin for a way to speed that process up or completely automate it.


Jonathan Ulmer

unread,
Jul 3, 2013, 7:26:25 PM7/3/13
to tas...@googlegroups.com
Check out SL4A (scripting language for android). If you download SL4A for android and the python module it has a serial buetooth chat example where you can send seial strings/bytes via bluteooth. From tasker you can call SL4A scripts. I have been able to successfully send bluetooth serial commands from tasker and have it received by a teensy 3 micro controlling a high output RGB led.

Here is an example of my python SL4A code:

import android
import time

droid = android.Android()
onDuration = 10

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

droid.toggleBluetoothState(True) #turn on bluetooth
connId=droid.bluetoothConnect(uuid,mac).result #connect to bluetooth moduel mac specified above
droid.bluetoothWrite('RAINBOWHSV 4095 150\n') #Write this string out via serial RAINBOWHSV=command 4095=S(saturation) 150=V(brightness) 
droid.makeToast("Rainbow on for" + str(onDuration)) #pop up an android "toast" saying the duration
time.sleep(onDuration) #Rainbow on for onDuration
droid.bluetoothWrite('ALLOFF\n') #Send ALLOFF command over serial to turn stop RAINBOWHSV function

******************************************************************************************** 

Here is the teensy 3 code using the excellent SerialCommand library  (should also work on arduino with minor changes (different pins, 8bit pwm instead of 12bit, Serial instead of Serial1)

#include <SerialCommand.h>
SerialCommand sCmd;     // The SerialCommand object

const int RedLED = 3;              // red led connected to PWM pin
const int GreenLED = 4;           // green led connected to PWM pin
const int BlueLED = 5;            // blue led connected to PWM pin
const int UVLED = 6;            // uv led connected to PWM pin

void setup() {
  analogWriteResolution(12); //set PWM to 12 bit resolution (Only teensy 3 supports 12bit pwm, you would need to change the formulas for 8bit pwm on arduino)
  Serial1.begin(9600); //only using 9600 buad for now, for arduino you would need to use "Serial" instead of "Serial1"
  pinMode(RedLED, OUTPUT);         // set pin as output 
  pinMode(GreenLED, OUTPUT);       // set pin as output 
  pinMode(BlueLED, OUTPUT);        // set pin as output 
  pinMode(UVLED, OUTPUT);          // set pin as output

  analogWrite(RedLED, 0); //Set all LEDs to off
  analogWrite(GreenLED, 0);
  analogWrite(BlueLED, 0);
  analogWrite(UVLED, 0);

  // Setup callbacks for SerialCommand commands
  sCmd.addCommand("RED",    RED_led); //RED + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
  sCmd.addCommand("GREEN",  GREEN_led); //GREEN + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
  sCmd.addCommand("BLUE",   BLUE_led); //BLUE + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
  sCmd.addCommand("UV",     UV_led); //UV + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
  sCmd.addCommand("ALLOFF", LEDS_off);         // Turns LED off
  sCmd.addCommand("TEST", Test_reply);         // Relpys back "Testing, testing 1, 2, 3" over bluetooth serial
  sCmd.addCommand("RAINBOWSIN", RainbowSin); //Do a rainbow using 3x 120deg phase seperated sine waves (doesn't look quite as good)
  sCmd.addCommand("RAINBOWHSV", RainbowHSV); //Do a rainbow using cycling HUE + Saturation & Brightness serial arguments
  sCmd.setDefaultHandler(unrecognized);      // Handler for command that isn't matched  (says "What?")
}

void loop() {
  sCmd.readSerial(); //not doing much in loop, just listening for serial commands then running attached functions when valid command is received
}   

void RED_led() {
  int aNumber;
  char *arg;
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);
    analogWrite(RedLED, aNumber);
  }
}

void GREEN_led() {
  int aNumber;
  char *arg;
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);
    analogWrite(GreenLED, aNumber);
  }
}

void BLUE_led() {
  int aNumber;
  char *arg;
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);
    analogWrite(BlueLED, aNumber);
  }
}

void UV_led() {
  int aNumber;
  char *arg;
  arg = sCmd.next();
  if (arg != NULL) {
    aNumber = atoi(arg);
    analogWrite(UVLED, aNumber);
  }
}

void LEDS_off() {
  analogWrite(RedLED, 0);
  analogWrite(GreenLED, 0);
  analogWrite(BlueLED, 0);
  analogWrite(UVLED, 0);
}

void RainbowSin() {
  LEDS_off();
  while(!Serial1.available()){
    int LED1; // these variables will be used to hold the led PWM values
    int LED2;
    int LED3;

    float x; /*this a variable that will receive the angle value from variable i. This value is converted to radians in the sine function and will be used to generate the PWM values */
    float r; // these variables will receive the PWM values calculated by the three sine functions
    float g;
    float b;
    for (int i=0; i<360; i++){

      //convert i into a floating point variable that can be used with PI
      x=float(i);
      /* to calculate r,g,b the sine function is modified to increase amplitute (127*) to create a phase shift (x+1/2*PI) and (x+3/2*PI) finally the sine wave is raised to illiminate negative values below zero by adding 1*/
      r=127*(sin(x/180*PI)+1);
      g=127*(sin(x/180*PI+3/2*PI)+1);
      b=127*(sin(x/180*PI+0.5*PI)+1);

      //convert flaot r,g,b to integers that can be assigned to LED PWM numbers

      LED1= int(r);
      LED2= int(g);
      LED3= int(b);

      //write LED levels to p0, p1, p4 (ASSIGN PWM values to LEDs)

      analogWrite (RedLED,LED1);
      analogWrite (GreenLED,LED2);
      analogWrite (BlueLED,LED3);

      //wait for 1/100 of a second

      delay(50);

      if(i>=360){
        i=0;
      }
    }
  }
}

void RainbowHSV(){
  LEDS_off();
  for (int i=0; i <= 12284; i++){ //cycle hue from 0 to 12284

  if (i >= 12284){
      i=0;
    }
hue = i;
    int32_t hue;
    int32_t sat;
    int32_t bri;
    int32_t red;
    int32_t green;
    int32_t blue;
    int32_t red_val;
    int32_t green_val;
    int32_t blue_val;


    char *arg;
    arg = sCmd.next();
    if (arg != NULL) {
      sat = atoi(arg);
    }

    arg = sCmd.next();
    if (arg != NULL) {
      bri = atoi(arg);
    }

    while (hue > 12284) hue -= 12285;
    while (hue < 0) hue += 12285;

    if (hue < 4095) {
      red_val = (5591040 - sat * (hue - 2730)) >> 12;
      green_val = (5591040 - sat * (1365 - hue)) >> 12;
      blue_val = (5591040 - sat * 1365) >> 12;
    }
    else if (hue < 8190) {
      red_val = (5591040 - sat * 1365) >> 12;
      green_val = (5591040 - sat * (hue - 6825)) >> 12;
      blue_val = (5591040 - sat * (5460 - hue)) >> 12;
    }
    else {
      red_val = (5591040 - sat * (9555 - hue)) >> 12;
      green_val = (5591040 - sat * 1365) >> 12;
      blue_val = (5591040 - sat * (hue - 10920)) >> 12;
    }


    red = ((bri + 1) * red_val) >> 12;
    green = ((bri + 1) * green_val) >> 12;
    blue = ((bri + 1) * blue_val) >> 12;

    analogWrite(RedLED, red);
    analogWrite(GreenLED, green);
    analogWrite(BlueLED, blue);
    
    if (Serial1.available()){ //if serial received break out of this loop you it can receive other commands
      break;
    }
    delay(5);
    //Serial1.print("Hue=");
    //Serial1.println(hue);
  }
}



void Test_reply() {
  Serial1.println("Testing, testing 1, 2, 3");
}

void unrecognized(const char *command) {
  Serial1.println("What?");
}
bluetooth_lamp_on_rainbowhsv_mac_commented.py
t3_rgbled_serialcommand_commented.ino

Dushyant Ahuja

unread,
Nov 25, 2013, 9:45:43 PM11/25/13
to tas...@googlegroups.com
Thanks Jonathan,

Using your example, I have finally been able to create a missed call notifier. Will post the link to my blog post soon.

Regards,
Dushyant Ahuja

Dushyant Ahuja

unread,
Dec 7, 2013, 11:46:39 AM12/7/13
to tas...@googlegroups.com

sgtpepperaut

unread,
Dec 7, 2013, 5:16:24 PM12/7/13
to tas...@googlegroups.com
thanks for the write up. i must have some issues because not even the droid.makeToast is showing anything. 

hmm.

when i compiled my own android app i also used the same uuid and it works fine. changed the mac but got nothing.

what android version are you running>

sgtpepperaut

unread,
Dec 7, 2013, 6:02:26 PM12/7/13
to tas...@googlegroups.com
ok the script is being called ( i see the icon in the top and....'scipt.py exited') 

however its not doing anything  the simple:

"
import android
import time

droid = android.Android()
droid.makeToast("test")

"

does not even show a toast. any ideas where to go from here ..?
Message has been deleted

Chris Hoffecker

unread,
Dec 13, 2013, 8:11:21 PM12/13/13
to tas...@googlegroups.com
Thank you for this! I'm working on building a carputer to control my footwell lights and add automated headlights, integrated garage door opener, etc. but I'm trying to see if I can do it without having to install buttons and instead control the arduino or mbed over bluetooth from my Android, probably via Tasker.

james AJ

unread,
Jan 17, 2016, 9:15:54 PM1/17/16
to Tasker
For Bluetooth serial to arduino,I did great with thishttps://www.androidpit.com/app/appinventor.ai_rblackmore245.Arduino_Bluetooth

Have a try,not fake!!

Richard Blackmore

unread,
Jan 19, 2016, 2:44:45 PM1/19/16
to Tasker
Link to the app on the playstore

https://play.google.com/store/apps/details?id=appinventor.ai_rblackmore245.Arduino_Bluetooth

I wrote the app years ago needs a lot of updating which i'll get around to soon including adding a USB function :-)

Reply all
Reply to author
Forward
0 new messages