Bluetooth Low Energy (BLE) beacon with nRF24L01(+). Data is received and displayed by Android device (Android app source code is attached).

Dependencies:   DS1820 mbed

nRF24L01 as Bluetooth Low Energy (BLE) Broadcaster/Beacon

Temperature measured by a DS1820 sensor is broadcasted by a nRF24L01(+) module as Bluetooth Low Energy signal. Data is then received and displayed on an Android device (smartphone/tablet) with Bluetooth Low Energy (Bluetooth Smart) enabled. In order to have Bluetooth LE available your device should run Android 4.3 or more recent.

Needed parts:

Zoom in /media/uploads/hudakz/img_20150314_115402.jpg
Figure 1: The hookup and the Android app in action

It was Dmitry Grinberg who figured out how to use nRF24L01 for BLE.
Read his amazing "Bit-banging" Bluetooth Low Energy. Thank you Dmitry!
I ported the code to mbed from Lijun's repository. Read his very neat article on the topic. Thank you Lijun!


It takes just few steps to build a Temperature Beacon

  1. Connect the nRF24L01(+) module and the DS1820 sensor to the mbed board according to the pin assignment defined in main.cpp. Don't forget to connect a 4.7k Ohm resistor between the DS1820's data pin and the +3.3V pin.

    /media/uploads/hudakz/nrf24l01.png
    Figure 2: nRF24L01(+) pinout

  2. Compile the project and save the binary file to your mbed module.
  3. Enable Bluetooth on the Android device.
  4. To view the raw data, install Nordic's nRF Master Control Panel (available at Google Play for free) to your Android device. Run the app and wait until a new nRF24L01 device is found. Do not tap the CONNECT button. This device is a broadcaster and enables only one way data flow (from the mbed to the Android). To see more details, tap the found nRF24L01 device on the left side of the screen and then the small RAW button which appears on the right side just below the CONNECT button. Now you should be able to see and check the raw bytes received from the mbed.
  5. Install the Android app:
    - Download the TemperatureBeacon app to your computer's folder of choice.
    - Open the folder and copy (send via Bluetooth or USB cable) the downloaded file to your Android device.
    - To install the app, open the folder on the Android with the file downloaded in the previous step and tap it.
  6. Once the app is installed and running:
    - After a while you should see the temperature displayed on Android (See in Figure 1).
    - Data is periodically updated. To verify that, touch the DS1820 sensor and you should see some new values.

If you'd like to adapt the Android app to your taste

  • Install Android Studio onto your computer (Window, Mac, Linux). It's a fantastic IDE from Google for free.
  • Download the Android app project to your computer's folder of choice and unzip.
  • Start Android Studio, open the project downloaded in the previous step and have fun.

I have learnt a lot about Android and Bluetooth Low Energy here:
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html,
https://thenewcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial

The Android app is based on:
https://github.com/devunwired/accessory-samples/tree/master/BluetoothGatt.
Thank you Dave!

Revision:
4:b3c5c54cfd21
Parent:
3:96153a5d95f6
Child:
5:448e87318747
--- a/main.cpp	Sun Mar 15 14:47:45 2015 +0000
+++ b/main.cpp	Mon Mar 16 08:35:22 2015 +0000
@@ -37,23 +37,23 @@
 #if defined(TARGET_LPC1768)
 SPI         spi(p11, p12, p13);     // MOSI, MISO, SCK
 DigitalOut  cs(p8);                 // CSN  (select SPI chip/slave)
-DigitalOut  ce(p14);                // CE   (enable nRF24L01+ chip)
-DS1820      ds1820(p15);            // creates a ds1820 sensor
+DigitalOut  ce(p14);                // CE   (enable nRF24L01 chip)
+DS1820      ds1820(p15);            // create a ds1820 sensor
 #elif defined(TARGET_FRDM_KL25Z)
 SPI         spi(PTD2, PTD3, PTD1);  // MOSI, MISO, SCK
 DigitalOut  cs(PTD0);               // CSN  (select SPI chip/slave)
-DigitalOut  ce(PTD5);               // CE   (enable nRF24L01+ chip)
-DS1820      ds1820(PTA13);          // creates a ds1820 sensor
+DigitalOut  ce(PTD5);               // CE   (enable nRF24L01 chip)
+DS1820      ds1820(PTA13);          // create a ds1820 sensor
 #elif defined(TARGET_FRDM_KL46Z)
 SPI         spi(PTD2, PTD3, PTD1);  // MOSI, MISO, SCK
 DigitalOut  cs(PTD0);               // CSN  (select SPI chip/slave)
-DigitalOut  ce(PTD5);               // CE   (enable nRF24L01+ chip)
-DS1820      ds1820(PTA13);          // creates a ds1820 sensor
+DigitalOut  ce(PTD5);               // CE   (enable nRF24L01 chip)
+DS1820      ds1820(PTA13);          // create a ds1820 sensor
 #elif defined(TARGET_LPC11U24)
 SPI         spi(P16, P15, P13);     // MOSI, MISO, SCK
 DigitalOut  cs(P17);                // CSN  (select SPI chip/slave)
-DigitalOut  ce(P18);                // CE   (enable nRF24L01+ chip)
-DS1820      ds1820(P10);            // creates a ds1820 sensor
+DigitalOut  ce(P18);                // CE   (enable nRF24L01 chip)
+DS1820      ds1820(P10);            // create a ds1820 sensor
 #elif defined(TARGET_NUCLEO_F030R8)
 SPI         spi(PB_5, PB_4, PB_3);  // MOSI, MISO, SCK
 DigitalOut  cs(PB_10);              // CSN  (select SPI chip/slave)
@@ -62,46 +62,46 @@
 #elif defined(TARGET_NUCLEO_F072RB)
 SPI         spi(PB_5, PB_4, PB_3);  // MOSI, MISO, SCK
 DigitalOut  cs(PB_10);              // CSN  (select SPI chip/slave)
-DigitalOut  ce(PA_8);               // CE   (enable nRF24L01+ chip)
+DigitalOut  ce(PA_8);               // CE   (enable nRF24L01 chip)
 DS1820      ds1820(PA_9);           // creates a ds1820 sensor
 #elif defined(TARGET_NUCLEO_F103RB)
 SPI         spi(PB_5, PB_4, PB_3);  // MOSI, MISO, SCK
 DigitalOut  cs(PB_10);              // CSN  (select SPI chip/slave)
-DigitalOut  ce(PA_8);               // CE   (enable nRF24L01+ chip)
-DS1820      ds1820(PA_9);           // creates a ds1820 sensor
+DigitalOut  ce(PA_8);               // CE   (enable nRF24L01 chip)
+DS1820      ds1820(PA_9);           // create a ds1820 sensor
 #elif defined(TARGET_NUCLEO_F401RE)
 SPI         spi(PB_5, PB_4, PB_3);  // MOSI, MISO, SCK
 DigitalOut  cs(PB_10);              // CSN  (select SPI chip/slave)
-DigitalOut  ce(PA_8);               // CE   (enable nRF24L01+ chip)
+DigitalOut  ce(PA_8);               // CE   (enable nRF24L01 chip)
 DS1820      ds1820(PA_9);           // creates a ds1820 sensor
 #elif defined(TARGET_NUCLEO_F411RE)
 SPI         spi(PB_5, PB_4, PB_3);  // MOSI, MISO, SCK
 DigitalOut  cs(PB_10);              // CSN  (select SPI chip/slave)
-DigitalOut  ce(PA_8);               // CE   (enable nRF24L01+ chip)
+DigitalOut  ce(PA_8);               // CE   (enable nRF24L01 chip)
 DS1820      ds1820(PA_9);           // creates a ds1820 sensor
 
 // If your board/plaform is not present yet then uncomment
-// the following four lines and replace TARGET_YOUR_BOARD, SPI_MOSI, SPI_MISO, SPI_SCK, SPIS_CS, CE_PIN and DS1820_DATA_PIN as appropriate.
+// the following five lines and replace TARGET_YOUR_BOARD, SPI_MOSI, SPI_MISO, SPI_SCK, SPIS_CS, CE_PIN and DS1820_DATA_PIN as appropriate.
 
 //#elif defined(TARGET_YOUR_BOARD)
 //SPI         spi(SPI_MOSI, SPI_MISO, SPI_SCK);
 //DigitalOut  cs(SPI_CS);             // CSN  (select SPI chip/slave)
 //DigitalOut  cs(CE_PIN);             // CE   (enable nRF24L01+ chip)
-//DS1820      ds1820(DS1820_DATA_PIN);// creates a ds1820 sensor
+//DS1820      ds1820(DS1820_DATA_PIN);// create a ds1820 sensor
 
 #endif
 
 uint8_t     buf[32];
 
 /**
- * @brief
+ * @brief   Implements CRC with LFSR
  * @note
- * @param
+ * @param   data:   packet data
+ *          len:    packet length
+ *          dst:    destination/location of CRC
  * @retval
  */
 void bleCRC(const uint8_t* data, uint8_t len, uint8_t* dst) {
-
-    // implementing CRC with LFSR
     uint8_t v, t, d;
 
     while(len--) {
@@ -125,14 +125,12 @@
 }
 
 /**
- * @brief
+ * @brief   Reverses bit order in a single byte
  * @note
- * @param
- * @retval
+ * @param   a:  byte to be reveresed
+ * @retval  byte with reveresed bit order
  */
 uint8_t swapBits(uint8_t a) {
-
-    // reverse the bit order in a single byte
     uint8_t v = 0;
     if(a & 0x80)
         v |= 0x01;
@@ -154,14 +152,14 @@
 }
 
 /**
- * @brief
+ * @brief   Implements whitening with LFSR
  * @note
- * @param
+ * @param   data:   location of the data to be whiten
+ *          len:    data length
+ *          whitenCoeff:    whitening coefficient
  * @retval
  */
 void bleWhiten(uint8_t* data, uint8_t len, uint8_t whitenCoeff) {
-
-    // Implementing whitening with LFSR
     uint8_t m;
     while(len--) {
         for(m = 1; m; m <<= 1) {
@@ -178,28 +176,24 @@
 }
 
 /**
- * @brief
- * @note
- * @param
- * @retval
+ * @brief   Starts whitening
+ * @note    the value we actually use is what BT'd use left shifted one...makes our life easier
+ * @param   chan:   BT channel
+ * @retval  single byte
  */
 static inline uint8_t bleWhitenStart(uint8_t chan) {
-
-    //the value we actually use is what BT'd use left shifted one...makes our life easier
-
     return swapBits(chan) | 2;
 }
 
 /**
- * @brief
+ * @brief   Assembles the packet to be transmitted
  * @note
- * @param
+ * @param   data:   packet data
+ *          len:    packet length
+ *          dst:    BLE channel
  * @retval
  */
 void blePacketEncode(uint8_t* packet, uint8_t len, uint8_t chan) {
-
-    // Assemble the packet to be transmitted
-
     // Length is of packet, including crc. pre-populate crc in packet with initial crc value!
     uint8_t i, dataLen = len - 3;
     bleCRC(packet, dataLen, packet + dataLen);
@@ -211,9 +205,10 @@
 }
 
 /**
- * @brief
+ * @brief   Sends cmommand to nRF24L01
  * @note
- * @param
+ * @param   cmd:    Command
+ *          data:   Data associated with the command
  * @retval
  */
 void nrfCmd(uint8_t cmd, uint8_t data) {
@@ -227,9 +222,9 @@
 }
 
 /**
- * @brief
+ * @brief   Transfers one byte to nRF24L01
  * @note
- * @param
+ * @param   cmd: the byte to be transferred
  * @retval
  */
 void nrfWriteByte(uint8_t cmd) {
@@ -240,9 +235,10 @@
 }
 
 /**
- * @brief
+ * @brief   Transfers several bytes to nRF24L01
  * @note
- * @param
+ * @param   data:   location of bytes to be transferred
+ *          len:    number of bytes to be transferred
  * @retval
  */
 void nrfWriteBytes(uint8_t* data, uint8_t len) {
@@ -292,13 +288,17 @@
     nrfWriteBytes(buf, 5);
     buf[0] = 0x2A;          // set RX address in nRF24L01, doesn't matter because RX is ignored in this case
     nrfWriteBytes(buf, 5);
+    
     if(!ds1820.begin()) {
 #if DEBUG        
         serial.printf("No DS1820 sensor found!\r\n");
 #endif
+        return 1;
     }
-        
     
+    ds1820.startConversion();   // Start temperature conversion
+    wait(1.0);                  // let DS1820 complete the temperature conversion
+          
     while(1) {
         static const uint8_t    chRf[] = { 2, 26, 80 };
         static const uint8_t    chLe[] = { 37, 38, 39 };
@@ -371,6 +371,6 @@
         wait_ms(50);    
         ce = 0;                     // (in preparation of switching to RX quickly)
         ds1820.startConversion();   // Start temperature conversion
-        wait(1);                    // Broadcasting interval (shall be greater than ds1820 temperture converion time)
+        wait(1.0);                  // Broadcasting interval (shall be > DS1820 temperture converion time)
     }
 }