iOS simplecontrol on Mbed via BLE 4.0

Objective

Demonstrate a simple iOS controller on mbed via a low-cost, easy to use bluetooth low energy 4.0 chip(RedBearLab BLE Mini).

Why should I care?

This page is trying to show you it's very easy to setup a project to do remote control work with mbed via bluetooth low energy.

  • Drop in low lost bluetooth low energy chip
  • Serial communication between BLE chip and mbed
  • Any BLE4.0 ready phone is capable of communicating with the chip. (iPhone model after iPhone 4, and most mid-high end Android after early 2013.)
  • Open sourced framework for the BLE chip and iOS/Android mobile application.

Why BLE 4.0?

Compare to classical bluetooth, BLE 4.0(bluetooth low energy) consume significantly less energy and has a much lower latency. It's very nice and handy for developing small embedded gadget.

Demo

This is a video showing how to control the meed's internal LED light from an iPhone.

Implementation

Components

  1. mbed LPC1768 https://mbed.org/platforms/mbed-LPC1768/
  2. iphone 5 running ios7 https://www.apple.com/iphone/
  3. redbearlab BLE Mini http://redbearlab.com/blemini/

http://static.squarespace.com/static/5039e08be4b00cf0e8cf88cd/t/510a0096e4b000ead7e06ff7/1359610007453/BLE%20mini1.jpg?format=300w

Wirings

The lower bottom right of the BLE mini chip there is a J4 6pin. (Left is BLE mini and right is Mbed)

  • VIN > 5V(VU)
  • GND > GND
  • TX > RX(p14)
  • RX >TX (p13)

(You can use any tx/rx lines on Mbed. I just use p14 and p13 due to more convenient for my setup on breadboard.)

When you first got the chip , you need to solder the connector pinout that comes with the BLE mini when you bought it. http://static.squarespace.com/static/5039e08be4b00cf0e8cf88cd/t/510a0115e4b008f90f02e654/1359610134520/BLE%20mini.jpg?format=300w

Then you can just plug in to the breakout board and wire it with mbed following my above wiring instruction.

http://i.imgur.com/yhvbf4C.png

http://i.imgur.com/u1XhDlY.png

Code Download

mbed part

Import programSerial_BLE

simple hello world on BLE serial communication

iOS part

https://github.com/Otherworldman/iOS/tree/4180_mini_lab

My Code Implementation Brief Explanation

The sole reason why I choose this redbearlab ble chip is it come with pre-installed firmware and it's using serial port communication. It's very simple to use. For this simple control demo, I predefined some data messages that toggle mbed LED on and off.

ble_mini.h

#define ON_INTERNEL_LED1 0x01 
#define OFF_INTERNEL_LED1 0x02
#define ON_INTERNEL_LED2 0x03 
#define OFF_INTERNEL_LED2 0x04
#define ON_INTERNEL_LED3 0x05 
#define OFF_INTERNEL_LED3 0x06
#define ON_INTERNEL_LED4 0x07 
#define OFF_INTERNEL_LED4 0x08

On the actual program:

main.c

   device.baud(57600);
    //simple polling demonstrate serial communication
    while(1) {
        if(device.readable()) {
            buf[i]=device.getc();
            if(buf[i]==ON_INTERNEL_LED1)myled1=1;
            if(buf[i]==OFF_INTERNEL_LED1)myled1=0;
            if(buf[i]==ON_INTERNEL_LED2)myled2=1;
            if(buf[i]==OFF_INTERNEL_LED2)myled2=0;            
            if(buf[i]==ON_INTERNEL_LED3)myled3=1;
            if(buf[i]==OFF_INTERNEL_LED3)myled3=0;            
            if(buf[i]==ON_INTERNEL_LED4)myled4=1;
            if(buf[i]==OFF_INTERNEL_LED4)myled4=0;      

The above are just two code snippet, the completed code please check the above program link i posted. Basically on mbed after setting up the baud rate to 57600, I am just using a simple polling to listen to serial communication.

What is interested is the iOS part implementation. redbearlab(the company that manufactured this piece of hardware) open sourced all their code, including the firmware of this BLE mini chip, and some sample iOS and Android application. https://github.com/RedBearLab/

I basically forked their iOS example and I tweaked some of the UI elements and I send some serial bytes according to my above pre-defined messages.

Once you download my code or their code from the two github pages I listed above, you will get a simple IOS app(theirs(for arduno) or mine(for mbed)) and their BLE framework. They setup the connection and you just need to write and read data from the stream.

send data to toggle LED1

- (IBAction)sendLED1:(id)sender {
    
    UInt8 buf[]={0x00};
    
    if (sw_LED1.on) {
        buf[0]=0x01;
    }
    else
        buf[0]=0x02;
    NSData *data = [[NSData alloc] initWithBytes:buf length:1];
    [ble write:data];
    
}

RedBearLab IOS BLE framework important functions

read from the ble stream

-(void) read
{
    CBUUID *uuid_service = [CBUUID UUIDWithString:@RBL_SERVICE_UUID];
    CBUUID *uuid_char = [CBUUID UUIDWithString:@RBL_CHAR_TX_UUID];
    
    [self readValue:uuid_service characteristicUUID:uuid_char p:activePeripheral];
}

write to the ble stream

-(void) write:(NSData *)d
{
    CBUUID *uuid_service = [CBUUID UUIDWithString:@RBL_SERVICE_UUID];
    CBUUID *uuid_char = [CBUUID UUIDWithString:@RBL_CHAR_RX_UUID];
    
    [self writeValue:uuid_service characteristicUUID:uuid_char p:activePeripheral data:d];
}

Bluetooth Peripheral Discovery

- (int) findBLEPeripherals:(int) timeout
{
    if (self.CM.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth not correctly initialized !");
        NSLog(@"State = %d (%s)\r\n", self.CM.state, [self centralManagerStateToString:self.CM.state]);
        return -1;
    }
    
    [NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];
    
#if TARGET_OS_IPHONE
    [self.CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@RBL_SERVICE_UUID]] options:nil];
#else
    [self.CM scanForPeripheralsWithServices:nil options:nil]; // Start scanning
#endif
    
    NSLog(@"scanForPeripheralsWithServices");
    
    return 0; // Started scanning OK !
}

Establish Connection

- (void) connectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Connecting to peripheral with UUID : %@", peripheral.identifier.UUIDString);
    
    self.activePeripheral = peripheral;
    self.activePeripheral.delegate = self;
    [self.CM connectPeripheral:self.activePeripheral
                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}

Further Work

As I demonstrated above, it's very easy to use this BLE chip on mbed and control via a smartphone. There are many other projects that wish to have a simple, drop in solution for remote control via a phone, this little handy chip will be useful.


4 comments on iOS simplecontrol on Mbed via BLE 4.0:

08 Oct 2014

.

17 Nov 2015

Do you need an IOS developer certification to customize the RedBear IOS app to your desired inputs and outputs? Can you only use toggle switches as in your demo or are there other options such as buttons and slider values? How much freedom do you have with changing the app?

11 Oct 2018

It is a good effort to reduce the price of Bluetooth. It should have a cost-effective for Iphone and it helps me surf the web for finding information about my research project but it is expensive for me. So I this issue has shared to https://applesupportnumber.net/ and they suggest me use an alternative way to reduce the price.

05 Apr 2019

The information you shared is very useful, I like to copy your some paragraph for people interest. But you did not mention here when the ultimate update coming. More Visit: http://www.emailhelpdesk.co/aol-mail-support-number

Please log in to post comments.