Yún and Grove - Ear-clip Heart Rate Sensor

Hi there, this is my first post to the forum. I have a bit of experience using the Uno, but not so much with the Yún.

I'm having an issue where my sketch works in the Uno but not on the Yún. I am basically trying to get my Grove ear clip heart rate sensor to run on the Yún. It should calculate my heart rate over 20 seconds, and then display the result when I get the data from the Yún. I don't think the sketch even begins running though. I am using the attachInterrupt function and attaching the sensor to digital pin 2.

I am using a modified version of this sketch: http://www.seeedstudio.com/wiki/Grove_-_Heart_rate_ear_clip_kit

Any help would be greatly appreciated. Thanks much!!

#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>

// LED
int LED = 13;
//state of LED, each time an external interrupt 
// will change the state of LED
boolean led_state = LOW; 
	
// Heart rate variables							
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect = true;
//the measurement result of heart rate
unsigned int heart_rate;
// you can change it follow your system's request.
// 2000 meams 2 seconds. System return error 
// if the duty overtrip 2 second.
const int max_heartpluse_duty = 2000;          

YunServer server;
String startString(8080);
 
void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  
  // Initialize serial communication
  Bridge.begin();
  
  // Listen for console logging
  Console.begin();
  
  while (!Console) {
    ; // wait for console port to connect
  }

  Console.println("Please ready your heart rate sensor.");

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.noListenOnLocalhost();
  server.begin();
  
  arrayInit();
  Console.println("Heart rate test begin.");
  // set interrupt 1, digital port 2
  attachInterrupt(1, interrupt, RISING);  
  
}
 
void loop() {
  // Get clients coming from server
  YunClient client = server.accept();
 
  // Is there a new client?
  if (client) {
    client.setTimeout(5);
    // Process request
    
    // read the command
    String command = client.readString();
    // kill whitespace  
    command.trim();        
    Console.println(command);
    
    // API command Get Heart Rate values
    if (command == "getheartrate") {
      client.println("Status:200");
      client.println("Content-Type: application/javascript");
      client.println();
      Console.print("Heart_rate_is:\t");
      Console.println(heart_rate);
    }
 
    // Close connection and free resources.
    client.stop();
  }
 
  delay(50); // Poll every 50ms
}
// Function: calculate the heart rate
void sum()
{
 if(data_effect)
    {
      heart_rate=1200000/(temp[20]-temp[0]);//60*20*1000/20_total_time 
      Console.print("Heart_rate_is:\t");
      Console.println(heart_rate);
    }
   data_effect=1;//sign bit
}
// Function: Interrupt service routine. Get the sigal from the external interrupt
void interrupt()
{
    temp[counter]=millis();
    Console.println(counter,DEC);
    Console.println(temp[counter]);
    switch(counter)
	{
		case 0:
			sub=temp[counter]-temp[20];
			Console.println(sub);
			break;
		default:
			sub=temp[counter]-temp[counter-1];
			Console.println(sub);
			break;
	}
    if(sub>max_heartpluse_duty) // set 2 seconds as max heart pluse duty
	{
		data_effect=0; // sign bit
		counter=0;
		Console.println("Heart rate measure error, test will restart!" );
		arrayInit();
	}
    if (counter==20&&data_effect)
    {
		counter=0;
		sum();
    }
    else if(counter!=20&&data_effect)
    counter++;
    else 
    {
		counter=0;
		data_effect=1;
    }
    
}
// Function: Initialization for the array(temp)
void arrayInit()
{
	for(unsigned char i=0;i < 20;i ++)
	{
		temp[i]=0;
	}
	temp[20]=millis();
}
 while (!Console) {
    ; // wait for console port to connect
  }

I think there is the problem: this sketch requires you to open the serial monitor over IP address (select the IP of the Yun from the ports menu, then open serial monitor)

Hi Federico, thank you for your quick response!

This seemed to be the issue. I needed to learn the difference between console and serial, and when it is appropriate to use them. After doing a bit of research it makes sense now and my prototype is working well.

Thanks so much for your help!!

You're welcome!

Console works over network/wifi whereas Serial works over usb cable.