Project tutorial
Arduino 101 + BLE + Magnetic Door Swith

Arduino 101 + BLE + Magnetic Door Swith © GPL3+

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

  • 990 views
  • 0 comments
  • 3 respects

Components and supplies

Apps and online services

Ide web
Arduino IDE
Intel Curie Board library

About this project

Introduction

This is a simple project that I make to demonstrate the BLE capabilities of Arduino 101.

.

Hardware

I make this project based on the tutorial titled Arduino 101 Bluetooth Low Energy. In the tutorial, a Grove kit and rotary angle sensor are used. I don't have those hardware ready, but I use the magnetic door sensor.


For details, you can refer the said tutorial.

.

Arduino IDE sketch

Here is a screen capture of the Arduino IDE with the DoorSensorSketch. Full sketch is presented in the code section below.

.

.

Android device App

.

I downloaded and installed the nRF Connect from app store. There are other similar BLE Android apps which you can use. Start the app and scan and connect to the DoorSensorSketch.

Summary

This is a simple project that demonstrates the read capability of the Arduino 101 device.

Code

arduino_101_ble_magnetic_door_sensor.inoC/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;           
  }
}

Comments

Similar projects you might like

Arduino 101 Home BLE System

Project tutorial by Alexis Santiago Allende

  • 3,671 views
  • 1 comment
  • 14 respects

Arduino/Genuino 101 BLE Thermometer With TMP102 and Blynk

Project tutorial by Konstantin Dimitrov

  • 9,519 views
  • 2 comments
  • 28 respects

Arduino 101 BLE RGB Lamp

Project tutorial by Gustavo Reynaga

  • 2,597 views
  • 2 comments
  • 10 respects

SMS alerts for arduino 101 BLE

Project tutorial by Alexis Santiago Allende

  • 2,944 views
  • 0 comments
  • 10 respects

Arduino 101 BLE App

Project in progress by Alexis Santiago Allende

  • 16,630 views
  • 29 comments
  • 65 respects
Add projectSign up / Login