Playing with the Bluetooth Low Energy Click board (BLE2 based on the RN4020) on a Curiosity board is a fun way to get acquainted with this technology. My first humble step was to get the simplest example offered in the RN4020 PICTail (one of the earliest demo boards available for evaluation of the little radio module) user guide and port it over with minor modifications to my new hardware setup.
Using the MCC, I performed the following simple initialisations:
- SYSTEM: 16MHz, MCLR ENABLED, LVP ENABLED
- GPIOs: CMD on RC5 (out), WAKE_SW on RA4(out), CONN on RC2 (in)
- TMR2: 4ms period
- ADC: channel POT on RC0, left aligned, ~40k smps
- EUSART: Asynchronous 115,200 baud, 8-bit, none, stdio redirect
And voila’ the application was created and ready for me to fill in the main() function.
I added an initialisation of the RN4020 module:
// initialize RN4020 WAKE_SW_SetHigh(); // enter command mode mygets(); sendCMD( "SF,1"); // factory reset sendCMD( "SS,C0000000"); // enable DeviceInformation and batter sendCMD( "SR,00000000"); // act as a peripheral sendCMD( "S-,Curiosity");// change name sendCMD( "R,1"); // reboot with new settings mygets(); __delay_ms( cmdLONGDELAY); // wait for CMD prompt sendCMD( "A"); // start advertising while( !CONN_GetValue());// wait for a connection
Followed by a very simple main loop:
while (1) { uint8_t time; // time is measured in multiples of TMR2 period (4ms) if ( TMR2_HasOverflowOccured()) { time++; } if ( time >= 50) // 200ms { uint8_t battery = ADC1_GetConversion( POT)>>9; time = 0; // update the battery level characteristic printf( "SHW,0018,%02x\n", (battery>100)? 100: battery); mygets(); } }
Notice how this demo does not use the MLDP mode – which emulates a sort of Serial profile similar to the (RN41) Standard Bluetooth module – which would have felt like cheating here. (See my previous project using the RN41 Bluetooth Click [link])
My intention is to learn how to define GATT Services and Characteristics and make full use of the BLE proper features.
You will find the new BLE-Battery project added to the Curiosity Rocket repository on GitHub [link].
Also I can strongly recommend the following applications, for iPhone and iPad users:
For Android smart phone, users: Android App Demo
To be continued…