Phone Heart

It is kind of a kick to see your heart beat.  Polar is a company that makes lots of stuff for athletes to watch their heart beat.  The data usually goes to something like a watch and i think it is pretty hard to get your own hands on the it.  I made this example for students in my Rest of You class to be able to use their heart control somthing or just be able to see the same data in their own visualization. 

 

You can this fantastic module from Sparkfun or alternatively this cheaper chip.   If you get the fancier one, the serial output pins line up perfectly the “Blue Smirf” bluetooth module (also from Sparkfun).  Sparkfun is making this stuff so easy I somehow feel guilty.  Anyway I wrote some code to get the readings in and show them on the cell phone.  I did the example in mobile proccessing but didn’t use their library for the bluetooth connection but instead just pasted in my own class called BTThread.  To keep the code simple I wanted to avoid the whole part where one device “discovers” other devices and services.  Instead I use my laptop to find the address of the bluetooth module (it will look like “00A096179A85”) and 1 hardcoded it.  I also put some lines in there for logging the data but I commented it out because if might slow you down.   Here is the code:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class MobileHeart extends PMIDlet{
  String BT_ADDRESS = “00A096179A85”;  //use your laptop to figure out the hardware address of the blue smirf.

  BTThread blueTooth;

  int xpos;
  PFont font ;
  int recordNum  ;
  String accumulatedFromBT = “”;
  String  filePrefix = “mhb”;  //distinguishes log file from other apps.

  int timeOfLastReading = 0;
  int interval = 1000;  //how often you want to read

  public void setup(){
    font = loadFont(“ArialMT-12.mvlw”);

    String[] savedPlace = loadStrings( filePrefix + “SavedPlace”);
    //if (getLength(savedPlace) > 0) {
    // recordNum =  Integer.parseInt( savedPlace[0].trim());
    //}

    blueTooth = new BTThread();
    blueTooth.start();

  }

  public void draw(){
    if (millis() – timeOfLastReading > interval && blueTooth != null){
      blueTooth.send(71);
      blueTooth.send(49);
      blueTooth.send(13);
      timeOfLastReading = millis();
    }

  }
  void blueToothEvent(int input){
    accumulatedFromBT = accumulatedFromBT + (char) input;

    if (input == 13) {
      println(“In From BlueTooth: ” + accumulatedFromBT);
      String[] parts = split(accumulatedFromBT,” “);
      accumulatedFromBT = “”;  //reset accumulation
      if (getLength(parts) > 2) {
        try {
          int status1 = Integer.parseInt((parts[0].trim()));
          int status2 = Integer.parseInt((parts[1].trim()));
          int heart = Integer.parseInt((parts[2].trim()));
          //  background(0);
          fill(255, 255, 255);
          rect(0,0,width,130); //clear the place where you are writing text
          text (“Heart” + heart, 10,120);
          /*  saveStrings( filePrefix + “Val”+recordNum,parts);
           saveStrings( filePrefix + “SavedPlace”,new String[] {
           String.valueOf(recordNum)                                                                                          }
           );  //recorde the last place you saved
           */
          //recordNum++;

          fill(255, 0, 255);
          ellipse(xpos, heart+ height/2, 2, 2);
          xpos = xpos + 2;
          if (xpos >= width) {
            xpos = 0;
            background(255,0,0);
          }
        }
        catch (NumberFormatException e) {
          println(“Not a line of Numbers”);
        }
      }

    }
  }

  int getLength(Object[] o)  { //don’t pay attention, servicing a bug in m.p.
    return o.length; //ideally you could just say yourObject.length
  }
  class BTThread extends Thread {
    StreamConnection btConnection = null;
    OutputStream btOutputStream = null;
    /* thread to accumulate bytes  */

    public void run() {
      textFont(font);

      try {
        //surrounds hardware address in bluetooth stuff. “:01” is for the serial service
        String BTurl = “btspp://” + BT_ADDRESS + “:01;authenticate=false;encrypt=false;master=false”;
        btConnection = (StreamConnection) Connector.open(BTurl);
        try {
          InputStream btInputStream = btConnection.openInputStream();
          btOutputStream = btConnection.openOutputStream();
          while (true) {
            //read in a byte at a time, this stops for the byte, that is why we are in a thread
            int input = btInputStream.read();
            blueToothEvent(input);
          }
        }
        catch (IOException e) {
          println(“Stopped Listening To BlueTooth”);
        }
      }
      catch (IOException e) {
        println(“Sorry but could not connect. to ” + BT_ADDRESS);
      }
      disconnect();
    }

    void send(int _output){
      try{
        if (btOutputStream != null){
          btOutputStream.write( _output);
          btOutputStream.flush();
        }
      }
      catch (IOException e) {
        println(“Stopped Listening To BlueTooth”);
      }
    }

    void disconnect() {
      try {

        btConnection.close();
      }
      catch (Exception e) {
        println(“Trouble Closing Connection.”);

      }

    }

  }
}

Leave a Reply