Breath Bra

Breathing is surprisingly underrated.  I made this example for students in my Rest of You class to be able see their breathing on their cellphone and to  log it over the course of a day.  In the course of this I live the dream of wearing a bra on the outside of my clothes during class.

I used a stretch sensor attached to a bra.  I put in a temperature sensor while I was at it.  Both of them are simple variable resistors that are very easy to use with microcontrollers.  I used a Lilypad microcontroller which is specially designed to be sewn into clothes.  There is a voltage regulator because I only had a 9 volt battery for power but it is not really necessary if you  used something like this or this instead.  Anyway instead of using a breadboard, I connected everything together by using a row of female headers bending all the ground connection to one side and the power to the other, data connections down the middle, eventually hot glue on everything.  It probably would have been easier to use this

On the software side 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.   The cellphone code and also the tiny arduino code for the Lilypad is below.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

  BTThread blueTooth;

  int xpos;
  PFont font ;
  int recordNum  ;
  String accumulatedFromBT = “”;

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

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

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

  }
  public void draw(){

  }
  void blueToothEvent(int input){
    accumulatedFromBT = accumulatedFromBT + (char) input;
    if (input == 10) {
      println(“In From BlueTooth: ” + accumulatedFromBT);
      String[] parts = split(accumulatedFromBT,”,”);
      accumulatedFromBT = “”;  //reset accumulation
      //send a byte out in case other side is waiting to be prompted
      blueTooth.send(65);

      if (getLength(parts) > 1) {
        try {
          int breath = Integer.parseInt((parts[0].trim()))-100;
          int heat = Integer.parseInt((parts[1].trim()))-400;
          //  background(0);
          fill(255, 255, 255);
          rect(0,0,width,30); //clear the place where you are writing text
          text (breath + ” ” + heat , 10,20);
          /// saveStrings(“HBVal”+recordNum,parts);
          // saveStrings(“HBSavedPlace”,new String[] {
          //  String.valueOf(recordNum)                                                                                          }
          // );  //recorde the last place you saved
          recordNum++;
          fill(0, 255, 255);
          ellipse(xpos, breath, 2, 2);
          fill(255, 0, 255);
          ellipse(xpos, heat , 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();
          ///send a byte out in case other side is waiting to be prompted
          send(65);
          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{
        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.”);

      }

    }

  }
}

ARDUINO:

int analogReading;  //variable int size because adc number potentially as big as 1024

void setup()
{
   beginSerial(9600);  //set up communication back to pc
   //don’t forget to press “serial monitor button”
}

void loop()
{
  analogReading = analogRead(0); //can only use pins 0-5
  Serial.print(analogReading,DEC);
  Serial.print(44,BYTE);
  analogReading = analogRead(1); //can only use pins 0-5
  Serial.print(analogReading,DEC);
  Serial.println(10,BYTE); //talk back to pc
}

Leave a Reply