vincent wong
Published © GPL3+

Arduino 101 + BLE + Magnetic Door Swith

Use an Android app to read Magnetic Door Switch state thru Arduino 101 BLE.

BeginnerFull instructions provided2,212
Arduino 101 + BLE + Magnetic Door Swith

Things used in this project

Hardware components

Arduino 101
Arduino 101
×1
Magnetic Door Switch
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Intel Curie Board library

Story

Read more

Code

arduino_101_ble_magnetic_door_sensor.ino

C/C++
#include <CurieBLE.h>

BLEPeripheral blePeripheral;                // BLE Peripheral Device (the board you're programming)

BLEService doorSensorService("180A");


BLECharacteristic doorSensorChar("2A56",  // standard 16-bit characteristic UUID
    BLERead | BLENotify, 2);     // remote clients will be able to
// get notifications if this characteristic changes

long previousMillis = 0;    // last time the blood pressure was checked, in ms

// magnetic door sensor
const int sensor = 4;
int oldState = 1;

void setup() {
  Serial.begin(9600);       // initialize serial communication
  pinMode(13, OUTPUT);      // initialize the LED on pin 13 to indicate when a central is connected

  pinMode(sensor, INPUT_PULLUP);

  blePeripheral.setLocalName("DoorSensorSketch");
  blePeripheral.setAdvertisedServiceUuid(doorSensorService.uuid());  // add the service UUID
  blePeripheral.addAttribute(doorSensorService);   // Add the BLE Blood Pressure service
  blePeripheral.addAttribute(doorSensorChar); // add the blood pressure characteristic
  
  const unsigned char charArray[2] = { 0, (unsigned char)oldState };
  doorSensorChar.setValue(charArray, 2);   // initial value for this characteristic

  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(13, HIGH);

    // check the door sensor state every 200ms
    // as long as the central is still connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check the blood pressure mesurement:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateDoorSensor();
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(13, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateDoorSensor() {
  int state = digitalRead(sensor);   // 0 close - 1 open switch

  if (state != oldState) {      
    Serial.print("The current state is: ");
    Serial.println(state);
    const unsigned char stateCharArray[2] = { 0, (unsigned char)state };
    // Update the door sensor state characteristic
    doorSensorChar.setValue(stateCharArray, 2);
    oldState = state;           
  }
}

Credits

vincent wong
81 projects • 205 followers
Contact

Comments

Please log in or sign up to comment.