Ishani Narwankar
Created October 8, 2016 © GPL3+

Calm Mate

A wearable that monitors children with learning disabilities and notifies their parents and teachers of an incoming anxiety attack.

AdvancedWork in progress24 hours136
Calm Mate

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Polar Heart Rate Monitor T31c
×1
Arduino Wifi Shield 101
Arduino Wifi Shield 101
×1
SparkFun arduino heart rate receiver interface
×1
SparkFun MicroSd shield
×1

Software apps and online services

Arduino IDE
Arduino IDE
prowl

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

whole device.m4v

Arduino
It is the code the Arduino uses to track the heart rate and send out the notification.
////
//
// General code from http://www.pushingbox.com for Arduino WiFI shield (official) v1.0
//
////

//heart rate
#include "Wire.h"
#define HRMI_I2C_ADDR      127
#define HRMI_HR_ALG        1   // 1= average sample, 0 = raw sample


//Wifi 
#include <WiFi101.h>
#include <SPI.h>
 
  /////////////////
 // MODIFY HERE //
/////////////////
char wifissid[] = "PACEM";   //  your network SSID (name)
char wifipass[] = "9008452672";    // your WPA network password

char DEVID1[] = "v8585F508DDF169B";        //Scenario : "The mailbox is open"

//Numeric Pin where you connect your switch
//uint8_t pinDevid1 = 2; // Example : the mailbox switch is connect to the Pin 3

//int buttonState = 0;
// Debug mode
boolean DEBUG = true;
  //////////////
 //   End    //
//////////////


char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false;                // Save the last state of the Pin for DEVID1
boolean lastConnected = false;  
int status = WL_IDLE_STATUS;     // the Wifi radio's status

WiFiClient client;

void setup() {
  // initialize serial:
  setupHeartMonitor(HRMI_HR_ALG);
  Serial.begin(9600);
  
 
  // attempt to connect using WPA2 encryption:
  Serial.println("Attempting to connect to WPA network...");
  status = WiFi.begin(wifissid, wifipass);
 
  // if you're not connected, stop here:
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // if you are connected, print out info about the connection:
  else {
    Serial.println("Connected to network");
  }
}
 
void loop() {
    
  int heartRate = getHeartRate();
  Serial.println(heartRate);


  
   ////
   // Listening for the pinDevid1 state
   ////
  if (heartRate >= 65 && pinDevid1State == false){
        if(DEBUG){Serial.println("pinDevid1 is HIGH");}
        pinDevid1State = true;
        //sending request
        sendToPushingBox(DEVID1);
      }
  /* if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
  {
    if(DEBUG){Serial.println("pinDevid1 is LOW");}
    pinDevid1State = false;
    //Sending request to PushingBox when the pin is LOW
    //sendToPushingBox(DEVID1);    //Here you can run an other scenario by creating a DEVID2 variable
  }
 
  //DEBUG part
  // this write the respons from PushingBox Server.
  // You should see a "200 OK"
  if (client.available()) {
    char c = client.read();
    if(DEBUG){Serial.print(c);}
  }
 */
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    if(DEBUG){Serial.println();}
    if(DEBUG){Serial.println("disconnecting.");}
    client.stop();
  }
  lastConnected = client.connected();

  delay(3000);
}


//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]){
  client.stop(); if(DEBUG){Serial.println("connecting...");}
  if(client.connect(serverName, 80)) { 
    if(DEBUG){Serial.println("connected");}
    if(DEBUG){Serial.println("sendind request");}
    client.print("GET /pushingbox?devid=");
    client.print(devid);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverName);
    client.println("User-Agent: Arduino");
    client.println();
  } 
  else { 
    if(DEBUG){Serial.println("connection failed");} 
  } 
}

void setupHeartMonitor(int type){
  //setup the heartrate monitor
  Wire.begin();
  writeRegister(HRMI_I2C_ADDR, 0x53, type); // Configure the HRMI with the requested algorithm mode
}

int getHeartRate(){
  //get and return heart rate
  //returns 0 if we couldnt get the heart rate
  byte i2cRspArray[3]; // I2C response array
  i2cRspArray[2] = 0;

  writeRegister(HRMI_I2C_ADDR,  0x47, 0x1); // Request a set of heart rate values 

  if (hrmiGetData(127, 3, i2cRspArray)) {
    return i2cRspArray[2];
  }
  else{
    return 0;
  }
}

void writeRegister(int deviceAddress, byte address, byte val) {
  //I2C command to send data to a specific address on the device
  Wire.beginTransmission(deviceAddress); // start transmission to device 
  Wire.write(address);       // send register address
  Wire.write(val);         // send value to write
  Wire.endTransmission();     // end transmission
}

boolean hrmiGetData(byte addr, byte numBytes, byte* dataArray){
  //Get data from heart rate monitor and fill dataArray byte with responce
  //Returns true if it was able to get it, false if not
  Wire.requestFrom(addr, numBytes);
  if (Wire.available()) {

    for (int i=0; i<numBytes; i++){
      dataArray[i] = Wire.read();
    }

    return true;
  }
  else{
    return false;
  }
}Binary file (no preview)

Credits

Ishani Narwankar

Ishani Narwankar

2 projects • 0 followers

Comments