My Heart Rate on the (Cosm) Cloud!

Recently Seeedstudio (many thanks!) has provided me with a Grove Heart Rate ear-clip sensor:

This cool (and very low price) sensor is attached on your ear and can detect your heart’s pulse through transmitting infrared light and checking the absorption variation caused by the blood flow on your ear lobe. The site of the products provides also the Arduino code for detecting the beats and calculating an average heart rate (in bpm  – beats per minute). The sensor comes with a grove connector, so setting up and running the code took less than 5 mins! (thanks again @seeedstudio for providing me with a complete Grove kit).

After playing with it a while I realized that I could make a cool Cloud-based heart rate tracker by simply using an ADK board and my Android phone. This way I could be completely mobile (given that the 9V battery that powers the ADK board can last!).

I modified the Arduino code to send the heart rate to the Android using the ADB and made also a simple Android app that takes the heart rate and sends it to Cosm  (former Pachube) using the jpachube library.

This is the graph generated from Cosm: (EDIT: Battery has died, will put on again sometime tomorrow ) EDIT (14/05): I’ve put it on again for a few hours!)

EDIT: I will occasionally put on the sensor during the following days.

Despite being very mobile (the cable is long enough to reach my pocket where both boards and mobile phone are) I am sure the graph-feed will stop being live quite soon (will either get bored, battery will die or will take it off to go to sleep…)

The code for the Arduino is the following:

/************************* 2011 Seeedstudio **************************
* File Name : Heart rate sensor.pde
* Author : Seeedteam
* Version : V1.0
* Date : 30/12/2011
* Description : This program can be used to measure heart rate,
the lowest pulse in the program be set to 30.

//Modified by @BuildingIoT
//for communication with Android

#include <SPI.h>
#include <Adb.h>

// Adb connection.
Connection * connection;

// Elapsed time for ADC sampling
long lastTime;

unsigned char pin = 13;
unsigned char counter=0;
unsigned int heart_rate=0;
unsigned long temp[21];
unsigned long sub=0;
volatile unsigned char state = LOW;
bool data_effect=true;
const int max_heartpluse_duty=2000;//you can change it follow your system's request.2000 meams 2 seconds. System return error if the duty overtrip 2 second.

void setup() {
pinMode(pin, OUTPUT);
//Serial.println("Please put on the ear clip.");
//Serial.println("Heart rate test begin.");
attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2

// Initialise the ADB subsystem.

// Open an ADB stream to the phone's shell. Auto-reconnect
connection = ADB::addConnection("tcp:4567", true, adbEventHandler);

void loop() {
digitalWrite(pin, state);


void sum()//calculate the heart rate
connection->write(2, (uint8_t*)&heart_rate);
data_effect=1;//sign bit
void interrupt()
state = !state;
if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
data_effect=0;//sign bit
Serial.println("Heart rate measure error,test will restart!" );
if (counter==20&&data_effect)
else if(counter!=20&&data_effect)
void array_init()
for(unsigned char i=0;i!=20;++i)
// Event handler for the shell connection.
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)


For the Android app all is needed is an Activity that implements the ADB server and communicates with the Arduino board:

package buildingiot.heartrate;


import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.microbridge.server.Server;
import org.microbridge.server.AbstractServerListener;

public class HeartRateOnCloudActivity extends Activity {

// Create TCP server (based on MicroBridge LightWeight Server).
// Note: This Server runs in a separate thread.
Server server = null;

int heartrate = 0;

TextView textView1;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {


//Create TCP server (based on MicroBridge LightWeight Server)
try {
server = new Server(4568); //Use the same port number used in ADK Main Board firmware
textView1.setText("Starting server..");
textView1.setText("server started!");

} catch (IOException e){
Log.e("Seeeduino ADK", "Unable to start TCP server", e);
textView1.setText("server not started!!");


server.addListener(new AbstractServerListener() {

public void onReceive(org.microbridge.server.Client client, byte[] data){
textView1.setText("got arduino data!");
String bpm = new String(data);
textView1.setText(bpm+" bpm");



To make it all work you need to have an ADB-enabled Arduino board like this one.

More examples on Android and Arduino communication can be found in my book.

11 thoughts on “My Heart Rate on the (Cosm) Cloud!

  1. Pingback: Question about obtaining information from a pulse oximeter for program

  2. Pingback: Android Phone Home, Phone EVRYTHNG | EVRYTHNG

  3. My parts are now arriving in the post to have a go at this project but I just had a thought wondering if because I’m using a different microcontroller will the pin assignments be different?

    • temp[] stores the time at which a heartbeat (interrupt) has been received. Used to calculate the total time it takes to receive 21 beats in the beginning for calculating the average beat per minute. It’s from Seeedstudio code for the heart rate sensor :-)

Leave a Reply

Your email address will not be published. Required fields are marked *