Skip to content

Instantly share code, notes, and snippets.

@rubcuadra
Created July 20, 2017 17:48
Show Gist options
  • Save rubcuadra/7b28ca279f72ff2f66258ec41ea79d39 to your computer and use it in GitHub Desktop.
Save rubcuadra/7b28ca279f72ff2f66258ec41ea79d39 to your computer and use it in GitHub Desktop.
Parte del script usado en Campus Party para detectar ritmo cardiaco y mandarlo por bluetooth a una app Android, de momento solo envía, la lectura de HR es en la linea 72 y ahi debería leer de un sensor
#include <CurieBLE.h>
/* */
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board we're programming)
BLEService HeartRateService("180D"); // heart rate Service
BLECharacteristic HeartRateChar("2A35", // standard 16-bit characteristic UUID
BLERead | BLENotify, 2); // remote clients will be able to
// get notifications if this characteristic changes
int oldHeartRate = 0; // last heart rate reading from analog input
long previousMillis = 0; // last time the heart rate was checked, in ms
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(LED_BUILTIN, OUTPUT); //Output en el default
/* Set a local name for the BLE device
This name will appear in advertising packets
and can be used by remote devices to identify this BLE device
The name can be changed but maybe be truncated based on space left in advertisement packet */
blePeripheral.setLocalName("HeartRateSketch");
blePeripheral.setAdvertisedServiceUuid(HeartRateService.uuid()); // add the service UUID
blePeripheral.addAttribute(HeartRateService); // Add the BLE heart rate service
blePeripheral.addAttribute(HeartRateChar); // add the heart rate characteristic
const unsigned char charArray[2] = { 0, (unsigned char)0 };
HeartRateChar.setValue(charArray, 2); // initial value for this characteristic
/* Now activate the BLE device. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */
blePeripheral.begin();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// listen for BLE peripherals to connect:
BLECentral central = blePeripheral.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
// check the heart rate mesurement every 200ms
// as long as the central is still connected:
while (central.connected())
{
long currentMillis = millis();
// if 200ms have passed, check the heart rate mesurement:
if (currentMillis - previousMillis >= 200)
{
previousMillis = currentMillis;
updateHeartRate();
}
}
// when the central disconnects, turn off the LED:
digitalWrite(13, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
void updateHeartRate()
{
/* Read the current voltage mesurement on the A0 analog input pin.
This is used here to simulate the heart rate.
*/
int pressure = random(50,120);//analogRead(A0);
int HeartRate = map(pressure, 0, 1023, 0, 100);
// If the heart rate has changed
if (HeartRate != oldHeartRate)
{
Serial.print("The current heart rate is: ");
Serial.println(HeartRate);
const unsigned char HeartRateCharArray[2] = { 0, (unsigned char)HeartRate };
// Update the heart rate measurement characteristic
HeartRateChar.setValue(HeartRateCharArray, 2);
// Save the measurement for next comparison
oldHeartRate = HeartRate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment