In this tutorial, we explain how to receive BlueTooth messages from iPhone or Android.
About BlueTooth Low Energy
BlueTooth Low Energy (also known as BLE) is a radio protocol which beacame widely used in recent years. Most of the new devices are BLE compliant.
REOBoard's bluetooth module is preloaded with a firmware making it very easy to deal with.
- You don't have to take care about the connection with the phone or tablet, the module does it for you.
- You can use it exactly as if it was a serial port except that the length of the messages must be within 20 bytes. In this tutorial, we are going to receive 1-byte messages and put LEDs on and off
To keep things simple, we are going to use 2 LEDs, on red, one green. As we have already explained how to use motors, I'm sure you will be able to adapt to any of the 3 kinds of motor we have introduced, DC motors, servos and steppers.
As we will use 2 LEDs, we can decide for example that LED1 is bit 1, LED2 is bit 2. Receiving 0x01 will lit the first LED, 0x02 the second and 0x03 both. And of course 0 will reset everything.
The Android / iOS application
Although this tutorial is aimed at explaining how to receive messages, we have to get one solution to send messages. This solution exists and is free.
On Android, there is a free downloadable application called BLE Tool which allows sending arbitrary messages in hexadecimal. There is also a tool on iOS, called BLExplr, unfortunately not free, but affordable. 2 oe 3 Euros.
The only new thing in this program is the UART initialization. Some attention should be paid to the fact that REOBoard's BlueTooth module needs flow control.
Here is the initialization code:
P8SEL |= 0x0C; // Config P8.2 & P8.3 for UART
P8DIR |= BT_CTS; // Config P8.0 for output
P8DIR &= ~BT_RTS; // Config P8.1 for input
P5DIR |= BT_RESET; // Configure bluetooth reset pin
UCA1CTL1 |= UCSWRST; // Put state machine in reset
UCA1CTL1 |= UCSSEL_2; // CLK = SMCLK
UCA1BRW = 208; // 24MHz / 115.2k
UCA1CTL1 &= ~UCSWRST; // Initialize USCI state machine
UCA1IE |= UCRXIE; // Enable interrupts
P5OUT &= ~BT_RESET; // Reset bluetooth
Delay(10); // Delay
P5OUT |= BT_RESET; // Free reset. Ready to go.
P8OUT &= ~BT_CTS; // Clear to send
There is nothing mysterious in this code. This initialization can also be found in Texas Instruments examples. Only flow control has been added. UART initialization needs the bit rate to be set. Dividing the clock frequency by the bit rate gives the number of clocks to be set to the UART module so that it listens at the proper rate. And of course, we have to enable receive interrupts.
The interrupt routine is basically the same as any other. Its a void func(void) type of function in which we are going to change P2 status according to what has been received.
Using the application
I have used it with BLExplr, and here are the screen copies.
Open BLExplr, you should see LeoBot (former name of REOBoard) appearing on the screen. Tap this item. REOBoard's BlueTooth module blue LED should be lit. Tap the unknown item under LeoBot20, then again the 2nd unknown item and a panel with "Write HEX, Write String and Enable Indicate" menu. Choose Write HEX. If you write 00, the LEDS must clear. If you write 01, one must be lit, if you write 02, the other must be lit and the first one cleared, and if you write 03, the 2 LEDs must be lit.
Download the C code
A summary of the first tutorials has been added as a downloadable zip file here.