You'll need to know how UART works for this tutorial, I would suggest having a look at this URL:UART
Before connecting the BLE module to the micro-controller, we need to configure RN4020 as a peripheral and also create a private service and its characteristics. The steps required to do the same are as follows:
Connect RN4020 to an FTDI adapter (USB to TTL converter) like the one below:
Connect 3.3 V Vcc to RN4020 WAKE and Vcc respectively, GND to grounds and RX, TX to TX, and RX respectively.
Open Tera term and select the serial port, enable Local echo and select CR+Linefeed for Transmit and Receive from setup, and also set the correct baud rate under Serial Port option, i.e. 115200.
Now type the following commands, after the execution of each command, an ACK or acknowledgment signal will be received on the terminal.
SF,1 //factory default
SB, 4 // baud rate set as 115200
SS, C0000001 //set device information and battery service
PZ //clears all settings of the private service and characteristics
PS, 11223344556677889900AABBCCDDEEFF //define private service with UUID
PC,010203040506070809000A0B0C0D0E0F, 08, 02 //the specified characteristic with write property is added, size is 2 bytes
SR, 20000000 //auto advertise
R, 1 //reboot
Check the list of services by typing LS, the UUID of Device Information, Battery and our private service and its characteristics with their UUIDs and handles will be displayed.
The characteristic that we created will be used to turn the LED ON/OFF via an android application. On typing 00, the LED will turn OFF, if it was ON and on typing 01, the LED will turn ON (if it was OFF). You may create additional characteristics as per your requirement, refer the RN4020 user-guide to know more about different AT commands used to configure the module.
Now download BLE Scanner Application from the Android store: BLE Scanner App
Connect with the RN4020 module from the list of devices and enter 00 or 01 as the value of the private characteristic, do select byte array and not Text to enter the value successfully. After entering the same, you can observe that tera terminal shows a command WC, handle, value. Here the value is either 00 or 01.
If on connecting the MSP430 micro-controller, you are able to read the value, you would then be able to make a decision. Have a look at the code which is loaded on MSP430G2553 which causes an interrupt to be generated whenever any value is transmitted by the BLE module. The code is self-explanatory.
#include <msp430.h> #include<stdio.h> char buffer[20]; //buffer to receive volatile unsigned int receive = 0; //configure UART void Configure_UART() { P1SEL = BIT1 + BIT2; P1SEL2 = BIT1 + BIT2; UCA0CTL1 |= UCSSEL_2; UCA0BR0 = 0X08; UCA0BR1 = 0X00; UCA0MCTL = UCBRS2 + UCBRS0; UCA0CTL1 &= ~UCSWRST; } //configure pins void Configure_Pins() { P1DIR |= BIT0; //Pin 0, LED connection P1OUT &= ~BIT0; } //main loop void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer BCSCTL1 = CALBC1_1MHZ; // Set range DCOCTL = CALDCO_1MHZ; Configure_UART(); Configure_Pins(); UC0IE |= UCA0RXIE; __bis_SR_register(CPUOFF + GIE); //set cpuoff and enable interrupts } //cpu is turned on whenever interrupt occurs and in main routine its off(stack restored) #pragma vector = USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void) { P1OUT &=~BIT6; buffer[receive++] = UCA0RXBUF; if(UCA0RXBUF == '\n') { receive = 0; __bic_SR_register(GIE); //disable interrupt as we want to make a decision now char conn = buffer[0]; if(conn != 'C') { char c = buffer[9]; //decide number to be accessed switch(c) { case '1': P1OUT |= BIT0; break; case '0': P1OUT &= ~BIT0; break; } } __bis_SR_register(GIE); //okay to talk again } }
Finally, the demo can be found on Youtube: Demo Video