A simple mbed demo app for the BC127 Bluetooth Audio and Data module.

Dependencies:   mbed

BC127 Melody mbed Echo App

This is a demo application for the BC127 module available from BlueCreation and and the mbed. You will need an mbed, a BC127, and a phone that support Bluetooth and SPP and has the Melody application installed. The Melody app is available on the Play Store here:

The BC127 and mbed

Connecting BC127 to mbed

BC127 Pinmbed Pin
1 - GNDGND
33 - VDDVout
34 - VDDVout
41 - TXp9
42 - RXp10

Connect and power the BC127 module according to the Manual and Data sheet. Connect Power, Gnd, and connect TX on the module to Pin 9 on the mbed and Rx to pin 10. Also connect ground from the module to the mbed.

  1. Compile and load the BC127 Melody mbed Echo App.
  2. Reset the mbed to load the app.
  3. Reset or power up the BC127.
    • You will see LED1 on the mbed light up.
    • The BC127 is now discoverable.
  4. List Find BC127 using your phone and pair to it.
    • The device name will be of the type BlueCreation-XXXXXX.
  5. Open the Melody application and connect to the BC127.
    • LED2 on the mbed should light up in addition to LED1. You are now connected!
    • When LED3 lights up the mbed has configured the BC127.
  6. Follow the instructions you just received in the Melody app!

Note: If you reset the BC127 you will need to reset the mbed to enable it to connect and configure the BC127. If you just disconnect and reconnect the phone, you will be sent back the SPP open indicator event coming from the BC127.

Committer:
bckln
Date:
Fri Feb 15 16:18:05 2013 +0000
Revision:
4:c17c10e9862e
Parent:
3:b8f28af864fe
fixed some typos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bckln 0:10292bb158f3 1 /*==============================================================================
bckln 0:10292bb158f3 2 BC127 Melody mbed Echo App
bckln 1:0644b8d3df40 3 Copyright (c) 2013 BlueCreation, MIT License
bckln 0:10292bb158f3 4
bckln 4:c17c10e9862e 5 This is a demo application for the BC127 module available from BlueCreation
bckln 0:10292bb158f3 6 and and the mbed.
bckln 0:10292bb158f3 7 You will need an mbed, a BC127, and a phone that support Bluetooth and SPP and
bckln 4:c17c10e9862e 8 has the Melody application installed. Application is available on the Play
bckln 0:10292bb158f3 9 Store here:
bckln 0:10292bb158f3 10
bckln 4:c17c10e9862e 11 Connect and power the BC127 module according to the Manual and Data sheet.
bckln 0:10292bb158f3 12 Connect Power, Gnd, and connect TX on the module to Pin 9 on the mbed and
bckln 4:c17c10e9862e 13 Rx to pin 10. Also connect ground from the module to the mbed.
bckln 0:10292bb158f3 14
bckln 0:10292bb158f3 15 Getting this to work:
bckln 0:10292bb158f3 16
bckln 0:10292bb158f3 17 1) Compile and load the BC127 Melody mbed Echo App.
bckln 0:10292bb158f3 18 2) Reset the mbed to load the app.
bckln 0:10292bb158f3 19 3) Reset or power up the BC127.
bckln 0:10292bb158f3 20 You will see LED1 on the mbed light up.
bckln 0:10292bb158f3 21 The BC127 is now discoverable.
bckln 0:10292bb158f3 22 4)Find BC127 using your phone and pair to it.
bckln 0:10292bb158f3 23 The device name will be of the type BlueCreation-XXXXXX.
bckln 0:10292bb158f3 24 5) Open the Melody application and connect to the BC127.
bckln 4:c17c10e9862e 25 LED2 on the mbed should light up in addition to LED1. You are now connected!
bckln 0:10292bb158f3 26 When LED3 lights up the mbed has configured the BC127.
bckln 4:c17c10e9862e 27 6) Follow the instructions you just received in the Melody app!
bckln 0:10292bb158f3 28
bckln 4:c17c10e9862e 29 Note: If you reset the BC127 you will need to reset the mbed to enable it to
bckln 0:10292bb158f3 30 connect and configure the BC127.
bckln 0:10292bb158f3 31 If you just disconnect and reconnect the phone, you will be sent back the SPP
bckln 0:10292bb158f3 32 open indicator event coming from the BC127.
bckln 0:10292bb158f3 33
bckln 0:10292bb158f3 34 *******************************************************************************/
bckln 4:c17c10e9862e 35
bckln 4:c17c10e9862e 36
bckln 0:10292bb158f3 37 #include "mbed.h"
bckln 0:10292bb158f3 38 #include "Serial.h"
bckln 0:10292bb158f3 39 #include "string.h"
bckln 4:c17c10e9862e 40
bckln 0:10292bb158f3 41 #define SIZE 13
bckln 2:206fd245b81b 42 char UARTBuff[SIZE];
bckln 4:c17c10e9862e 43
bckln 0:10292bb158f3 44 /* Responses from Melody, in order characters are stored in FIFO */
bckln 0:10292bb158f3 45 char SppConn[SIZE] = {'\r','\n','P','P','S',' ','K','O','_','N','E','P','O'};
bckln 0:10292bb158f3 46 char Ok[4] = {'\r','\n','K','O'};
bckln 4:c17c10e9862e 47
bckln 4:c17c10e9862e 48 /* Use LED to indicate mbed state */
bckln 0:10292bb158f3 49 DigitalOut myled1(LED1); //started parsing
bckln 0:10292bb158f3 50 DigitalOut myled2(LED2); // connected
bckln 0:10292bb158f3 51 DigitalOut myled3(LED3); // configured and in echo mode
bckln 0:10292bb158f3 52 DigitalOut myled4(LED4); // toggles for every character received in echo mode
bckln 4:c17c10e9862e 53
bckln 0:10292bb158f3 54 /* track state internally with these flags */
bckln 0:10292bb158f3 55 int connected = 0;
bckln 0:10292bb158f3 56 int loopback = 0;
bckln 4:c17c10e9862e 57
bckln 0:10292bb158f3 58 /* Create a serial instance using pins 9 and 10 for mbed tx and rx */
bckln 0:10292bb158f3 59 Serial melody(p9,p10);
bckln 4:c17c10e9862e 60
bckln 4:c17c10e9862e 61
bckln 0:10292bb158f3 62 /* -=Interrupt handler for Serial Rx interrupt=-
bckln 4:c17c10e9862e 63
bckln 0:10292bb158f3 64 This function is called when an interrupt is generated on the UART RX.
bckln 0:10292bb158f3 65 This happens for every character received.
bckln 4:c17c10e9862e 66
bckln 0:10292bb158f3 67 This function stores received characters in a buffer, compares it to known
bckln 0:10292bb158f3 68 responses and passes commands to the BC127 Melody module based on the
bckln 4:c17c10e9862e 69 received responses.
bckln 0:10292bb158f3 70
bckln 0:10292bb158f3 71 Once it configures the BC127 it stops storing characters and goes into echo
bckln 0:10292bb158f3 72 (loopback) mode and echoes back any character received.
bckln 4:c17c10e9862e 73
bckln 0:10292bb158f3 74 */
bckln 4:c17c10e9862e 75
bckln 0:10292bb158f3 76 void RxParse(void)
bckln 0:10292bb158f3 77 {
bckln 0:10292bb158f3 78 if(!loopback) {
bckln 0:10292bb158f3 79 /* A character has been received and we are not in loopback.
bckln 0:10292bb158f3 80 Starting to look for a SPP connection indication */
bckln 4:c17c10e9862e 81
bckln 0:10292bb158f3 82 myled1 = 1; //indicate we have started parsing
bckln 4:c17c10e9862e 83
bckln 0:10292bb158f3 84 /* Shifts characters in the FIFO buffer.
bckln 0:10292bb158f3 85 Note: a circular buffer would have been much faster, however,
bckln 0:10292bb158f3 86 we would not have been able to use memcmp() so it is a good
bckln 4:c17c10e9862e 87 trade-off in terms of speed. */
bckln 4:c17c10e9862e 88
bckln 3:b8f28af864fe 89 for(int i = SIZE-2; i>0; i--)
bckln 3:b8f28af864fe 90 {
bckln 0:10292bb158f3 91 UARTBuff[i+1] = UARTBuff[i];
bckln 0:10292bb158f3 92 }
bckln 0:10292bb158f3 93 UARTBuff[1] = UARTBuff[0];
bckln 0:10292bb158f3 94 UARTBuff[0] = melody.getc(); // store current character
bckln 4:c17c10e9862e 95
bckln 4:c17c10e9862e 96 if(!connected) { // SPP connection has not been detected yet
bckln 0:10292bb158f3 97 /* Check buffer for SPP connection indication */
bckln 3:b8f28af864fe 98 if(memcmp(&UARTBuff[0],&SppConn[0],SIZE)==0)
bckln 3:b8f28af864fe 99 {
bckln 0:10292bb158f3 100 /* SPP connection detected */
bckln 0:10292bb158f3 101 myled2 = 1; // indicate to the world
bckln 0:10292bb158f3 102 melody.printf("ENTER_DATA\r\n"); // Configure BC127
bckln 0:10292bb158f3 103 connected = 1; // change internal state
bckln 4:c17c10e9862e 104
bckln 0:10292bb158f3 105 return;
bckln 0:10292bb158f3 106 }
bckln 0:10292bb158f3 107 return;
bckln 4:c17c10e9862e 108
bckln 0:10292bb158f3 109 }
bckln 0:10292bb158f3 110 /* We are connected and waiting for a confirmation that our
bckln 0:10292bb158f3 111 configuration has been accepted and the BC127 has entered
bckln 0:10292bb158f3 112 DATA_MODE */
bckln 3:b8f28af864fe 113 else if(memcmp(&UARTBuff[0],&Ok[0],4) == 0)
bckln 3:b8f28af864fe 114 {
bckln 4:c17c10e9862e 115 /* Success! We have configured the BC127 and we can now
bckln 0:10292bb158f3 116 send some instruction to Bluetooth terminal via BC127 */
bckln 4:c17c10e9862e 117
bckln 4:c17c10e9862e 118 myled3 = 1; /* indicate state to the world */
bckln 0:10292bb158f3 119 melody.printf("\r\n\r\n");
bckln 0:10292bb158f3 120 melody.printf("*********************************\n\r");
bckln 0:10292bb158f3 121 melody.printf("* Melody mbed Cookbook Echo App *\r\n");
bckln 0:10292bb158f3 122 melody.printf("* (c) BlueCreation 2013 *\r\n");
bckln 0:10292bb158f3 123 melody.printf("*********************************\n\r");
bckln 0:10292bb158f3 124 melody.printf("\r\n");
bckln 0:10292bb158f3 125 melody.printf(">> mbed now in SPP Echo mode.\r\n");
bckln 0:10292bb158f3 126 melody.printf(">> Type any character:\r\n>> ");
bckln 0:10292bb158f3 127 loopback = 1; // change internal state
bckln 4:c17c10e9862e 128
bckln 0:10292bb158f3 129 return;
bckln 0:10292bb158f3 130 }
bckln 3:b8f28af864fe 131 }
bckln 3:b8f28af864fe 132 else
bckln 3:b8f28af864fe 133 {
bckln 0:10292bb158f3 134 /* We are in loopback mode. Get character received from UART and
bckln 3:b8f28af864fe 135 send it back out indicating we have echoed it */
bckln 0:10292bb158f3 136 melody.printf("\n\r<< mbed echo: %c\n\r>> ", melody.getc());
bckln 0:10292bb158f3 137 myled4 = !myled4; // toggle LED4 for each character received
bckln 0:10292bb158f3 138 return;
bckln 0:10292bb158f3 139 }
bckln 4:c17c10e9862e 140
bckln 0:10292bb158f3 141 return;
bckln 0:10292bb158f3 142 }
bckln 4:c17c10e9862e 143
bckln 0:10292bb158f3 144 int main()
bckln 0:10292bb158f3 145 {
bckln 0:10292bb158f3 146 /* The Melody firmware on the BC127 runs at 9600kpbs, 8 data bits,
bckln 0:10292bb158f3 147 no parity bits, 1 stop bit and no HW flow control.
bckln 0:10292bb158f3 148 These are the default serial settings for mbed, so we do not
bckln 0:10292bb158f3 149 need to set those on the serial interface.
bckln 4:c17c10e9862e 150
bckln 0:10292bb158f3 151 We just need to handle incoming characters.
bckln 0:10292bb158f3 152 Therefore we attach and interrupt handler to the Rx interrupt.
bckln 0:10292bb158f3 153 The mbed firmware will then call our RxParse function every
bckln 0:10292bb158f3 154 time a character is received on the serial interface */
bckln 4:c17c10e9862e 155
bckln 0:10292bb158f3 156 melody.attach(RxParse);
bckln 4:c17c10e9862e 157
bckln 0:10292bb158f3 158 }