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!

Committer:
hudakz
Date:
Sat Mar 14 07:34:03 2015 +0000
Revision:
0:6f3139e3410e
Child:
1:ef3cc9be1f60
nRF24L01+ as a Bluetooth Low Energy Broadcaster/Beacon
; by hacking an nRF24L01+ module (which is under $1 per unit on eBay).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:6f3139e3410e 1 /*
hudakz 0:6f3139e3410e 2 nRF24L01+ as a Bluetooth Low Energy Broadcaster/Beacon
hudakz 0:6f3139e3410e 3 by hacking an nRF24L01+ module (which is under $1 per unit on eBay).
hudakz 0:6f3139e3410e 4
hudakz 0:6f3139e3410e 5 Note: It works also with the older nRF24L01 modules (without the +).
hudakz 0:6f3139e3410e 6
hudakz 0:6f3139e3410e 7 In this example we broadcast temperature readings measured by a DS1820 chip.
hudakz 0:6f3139e3410e 8 (You can easily modify the code to broadcast some other custom data.
hudakz 0:6f3139e3410e 9 Only make sure not to exceed the 32 bytes packet length.)
hudakz 0:6f3139e3410e 10
hudakz 0:6f3139e3410e 11 The data can be received and displayed on an Android device (smartphone/tablet)
hudakz 0:6f3139e3410e 12 with Bluetooth Low Energy (Bluetooth Smart) enabled.
hudakz 0:6f3139e3410e 13 In order to have Bluetooth 4.0 available (which implements Bluetooth LE)
hudakz 0:6f3139e3410e 14 your device should run Android 4.3 or later.
hudakz 0:6f3139e3410e 15
hudakz 0:6f3139e3410e 16 I ported this code to mbed from Lijun's repository <https://github.com/lijunxyz/nRF24_BLE>
hudakz 0:6f3139e3410e 17 Read his very neat article on the topic <http://wiki.lijun.xyz/misc-nrf24-ble.html>
hudakz 0:6f3139e3410e 18 and notice the nRF24L01(+) module pinout.
hudakz 0:6f3139e3410e 19 Thank you Lijun.
hudakz 0:6f3139e3410e 20
hudakz 0:6f3139e3410e 21 However, it all began with Dmitry Ginsberg's amazing "Bit-banging" Bluetooth Low Energy
hudakz 0:6f3139e3410e 22 <http://dmitry.gr/index.php?r=05.Projects&proj=11.%20Bluetooth%20LE%20fakery>
hudakz 0:6f3139e3410e 23 Thank you Dmitry.
hudakz 0:6f3139e3410e 24
hudakz 0:6f3139e3410e 25 After programming the mbed, enable Bluetooth on the Android device (smartphone/tablet).
hudakz 0:6f3139e3410e 26 To view the custom data, install "nRF Master Control Panel" from Nordic (available at Google Play Store).
hudakz 0:6f3139e3410e 27 Run the app and wait until a new device "nRF24L01" is found. Do not tap the "CONNECT" button.
hudakz 0:6f3139e3410e 28 This device is an advertiser and enables only one way data flow (from the mbed to the Android).
hudakz 0:6f3139e3410e 29 To see more details, tap the found device "nRF24L01" on the left side of the screen
hudakz 0:6f3139e3410e 30 and then the small "RAW" button which appears on the right side just below the "CONNECT" button.
hudakz 0:6f3139e3410e 31 Then you should be able to see and check the raw bytes received from the mbed.
hudakz 0:6f3139e3410e 32 That's nice. But if you would like to have more fun and see the temperature readings
hudakz 0:6f3139e3410e 33 then download "Temperature Beacon" from Google Play Store or to have even bigger fun you better build your own Android app.
hudakz 0:6f3139e3410e 34 It's easy (as everithing, once you know how :)
hudakz 0:6f3139e3410e 35 - Install "Android Studio" onto your PC/Mac (it's a fantastic IDE from Google for free)
hudakz 0:6f3139e3410e 36 - Download the app project from "..." to your PC/Mac folder of choice
hudakz 0:6f3139e3410e 37 - Start "Android Studio" and open the project downloaded in the previous step
hudakz 0:6f3139e3410e 38 - Connect your Android device to the PC/Mac via USB (make sure that the USB driver is installed on your PC/Mac
hudakz 0:6f3139e3410e 39 and "USB debugging" is enabled in "Developer Options" on your Android device)
hudakz 0:6f3139e3410e 40 - Run the project in "Android Studio". When you are asked, choose your Android device (phone/tablet) from the list as running device
hudakz 0:6f3139e3410e 41 - Once the app is installed and running, after a few seconds you should see on your Android the temperature readings
hudakz 0:6f3139e3410e 42 - The data is periodically updated to have the illusion of a connected device
hudakz 0:6f3139e3410e 43 - To verify that, touch the DS1820 sensor with your finger and you should see some new readings...
hudakz 0:6f3139e3410e 44
hudakz 0:6f3139e3410e 45 I have learnt a lot from:
hudakz 0:6f3139e3410e 46 <https://developer.android.com/guide/topics/connectivity/bluetooth-le.html>
hudakz 0:6f3139e3410e 47 <https://thenewcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial>
hudakz 0:6f3139e3410e 48 and borrowed the code for Android from here:
hudakz 0:6f3139e3410e 49 <https://github.com/devunwired/accessory-samples/tree/master/BluetoothGatt>
hudakz 0:6f3139e3410e 50 Thank you Dave.
hudakz 0:6f3139e3410e 51
hudakz 0:6f3139e3410e 52 */
hudakz 0:6f3139e3410e 53
hudakz 0:6f3139e3410e 54
hudakz 0:6f3139e3410e 55 #include "mbed.h"
hudakz 0:6f3139e3410e 56 #include "DS1820.h"
hudakz 0:6f3139e3410e 57
hudakz 0:6f3139e3410e 58 Serial serial(USBTX, USBRX);
hudakz 0:6f3139e3410e 59
hudakz 0:6f3139e3410e 60 // The MAC address of BLE advertizer -- just make one up
hudakz 0:6f3139e3410e 61 // If you decide to ceate more Beacons make sure that MAC address (MY_MAC) is unique (differs)
hudakz 0:6f3139e3410e 62
hudakz 0:6f3139e3410e 63 #define MY_MAC_0 0x11
hudakz 0:6f3139e3410e 64 #define MY_MAC_1 0x12
hudakz 0:6f3139e3410e 65 #define MY_MAC_2 0x33
hudakz 0:6f3139e3410e 66 #define MY_MAC_3 0x44
hudakz 0:6f3139e3410e 67 #define MY_MAC_4 0x55
hudakz 0:6f3139e3410e 68 #define MY_MAC_5 0x66
hudakz 0:6f3139e3410e 69
hudakz 0:6f3139e3410e 70 #if defined(TARGET_LPC1768)
hudakz 0:6f3139e3410e 71 SPI spi(p11, p12, p13); // MOSI, MISO, SCK
hudakz 0:6f3139e3410e 72 DigitalOut cs(p8); // CSN (select SPI chip/slave)
hudakz 0:6f3139e3410e 73 DigitalOut ce(p14); // CE (enable nRF24L01+ chip)
hudakz 0:6f3139e3410e 74 DS1820 ds1820(p15);
hudakz 0:6f3139e3410e 75 #elif defined(TARGET_NUCLEO_F103RB)
hudakz 0:6f3139e3410e 76 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:6f3139e3410e 77 DigitalOut cs(PB_10); // CSN (select SPI chip/slave)
hudakz 0:6f3139e3410e 78 DigitalOut ce(PA_8); // CE (enable nRF24L01+ chip)
hudakz 0:6f3139e3410e 79 DS1820 ds1820(PA_9);
hudakz 0:6f3139e3410e 80 #elif defined(TARGET_NUCLEO_F401RE)
hudakz 0:6f3139e3410e 81 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:6f3139e3410e 82 DigitalOut cs(PB_10); // CSN (select SPI chip/slave)
hudakz 0:6f3139e3410e 83 DigitalOut ce(PA_8); // CE (enable nRF24L01+ chip)
hudakz 0:6f3139e3410e 84 DS1820 ds1820(PA_9);
hudakz 0:6f3139e3410e 85 #elif defined(TARGET_NUCLEO_F411RE)
hudakz 0:6f3139e3410e 86 SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCK
hudakz 0:6f3139e3410e 87 DigitalOut cs(PB_10); // CSN (select SPI chip/slave)
hudakz 0:6f3139e3410e 88 DigitalOut ce(PA_8); // CE (enable nRF24L01+ chip)
hudakz 0:6f3139e3410e 89 DS1820 ds1820(PA_9);
hudakz 0:6f3139e3410e 90
hudakz 0:6f3139e3410e 91 // If your board/plaform is not present yet then uncomment
hudakz 0:6f3139e3410e 92 // the following four lines and replace TARGET_YOUR_BOARD and CE_PIN as appropriate.
hudakz 0:6f3139e3410e 93
hudakz 0:6f3139e3410e 94 //#elif defined(TARGET_YOUR_BOARD)
hudakz 0:6f3139e3410e 95 //SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
hudakz 0:6f3139e3410e 96 //DigitalOut cs(SPI_CS); // CSN (select SPI chip/slave)
hudakz 0:6f3139e3410e 97 //DigitalOut cs(CE_PIN); // CE (enable nRF24L01+ chip)
hudakz 0:6f3139e3410e 98 //DS1820 ds1820(DS1820_PIN);
hudakz 0:6f3139e3410e 99
hudakz 0:6f3139e3410e 100 #endif
hudakz 0:6f3139e3410e 101
hudakz 0:6f3139e3410e 102 uint8_t buf[32];
hudakz 0:6f3139e3410e 103
hudakz 0:6f3139e3410e 104 /**
hudakz 0:6f3139e3410e 105 * @brief
hudakz 0:6f3139e3410e 106 * @note
hudakz 0:6f3139e3410e 107 * @param
hudakz 0:6f3139e3410e 108 * @retval
hudakz 0:6f3139e3410e 109 */
hudakz 0:6f3139e3410e 110 void bleCRC(const uint8_t* data, uint8_t len, uint8_t* dst) {
hudakz 0:6f3139e3410e 111
hudakz 0:6f3139e3410e 112 // implementing CRC with LFSR
hudakz 0:6f3139e3410e 113 uint8_t v, t, d;
hudakz 0:6f3139e3410e 114
hudakz 0:6f3139e3410e 115 while(len--) {
hudakz 0:6f3139e3410e 116 d = *data++;
hudakz 0:6f3139e3410e 117 for(v = 0; v < 8; v++, d >>= 1) {
hudakz 0:6f3139e3410e 118 t = dst[0] >> 7;
hudakz 0:6f3139e3410e 119 dst[0] <<= 1;
hudakz 0:6f3139e3410e 120 if(dst[1] & 0x80)
hudakz 0:6f3139e3410e 121 dst[0] |= 1;
hudakz 0:6f3139e3410e 122 dst[1] <<= 1;
hudakz 0:6f3139e3410e 123 if(dst[2] & 0x80)
hudakz 0:6f3139e3410e 124 dst[1] |= 1;
hudakz 0:6f3139e3410e 125 dst[2] <<= 1;
hudakz 0:6f3139e3410e 126
hudakz 0:6f3139e3410e 127 if(t != (d & 1)) {
hudakz 0:6f3139e3410e 128 dst[2] ^= 0x5B;
hudakz 0:6f3139e3410e 129 dst[1] ^= 0x06;
hudakz 0:6f3139e3410e 130 }
hudakz 0:6f3139e3410e 131 }
hudakz 0:6f3139e3410e 132 }
hudakz 0:6f3139e3410e 133 }
hudakz 0:6f3139e3410e 134
hudakz 0:6f3139e3410e 135 /**
hudakz 0:6f3139e3410e 136 * @brief
hudakz 0:6f3139e3410e 137 * @note
hudakz 0:6f3139e3410e 138 * @param
hudakz 0:6f3139e3410e 139 * @retval
hudakz 0:6f3139e3410e 140 */
hudakz 0:6f3139e3410e 141 uint8_t swapBits(uint8_t a) {
hudakz 0:6f3139e3410e 142
hudakz 0:6f3139e3410e 143 // reverse the bit order in a single byte
hudakz 0:6f3139e3410e 144 uint8_t v = 0;
hudakz 0:6f3139e3410e 145 if(a & 0x80)
hudakz 0:6f3139e3410e 146 v |= 0x01;
hudakz 0:6f3139e3410e 147 if(a & 0x40)
hudakz 0:6f3139e3410e 148 v |= 0x02;
hudakz 0:6f3139e3410e 149 if(a & 0x20)
hudakz 0:6f3139e3410e 150 v |= 0x04;
hudakz 0:6f3139e3410e 151 if(a & 0x10)
hudakz 0:6f3139e3410e 152 v |= 0x08;
hudakz 0:6f3139e3410e 153 if(a & 0x08)
hudakz 0:6f3139e3410e 154 v |= 0x10;
hudakz 0:6f3139e3410e 155 if(a & 0x04)
hudakz 0:6f3139e3410e 156 v |= 0x20;
hudakz 0:6f3139e3410e 157 if(a & 0x02)
hudakz 0:6f3139e3410e 158 v |= 0x40;
hudakz 0:6f3139e3410e 159 if(a & 0x01)
hudakz 0:6f3139e3410e 160 v |= 0x80;
hudakz 0:6f3139e3410e 161 return v;
hudakz 0:6f3139e3410e 162 }
hudakz 0:6f3139e3410e 163
hudakz 0:6f3139e3410e 164 /**
hudakz 0:6f3139e3410e 165 * @brief
hudakz 0:6f3139e3410e 166 * @note
hudakz 0:6f3139e3410e 167 * @param
hudakz 0:6f3139e3410e 168 * @retval
hudakz 0:6f3139e3410e 169 */
hudakz 0:6f3139e3410e 170 void bleWhiten(uint8_t* data, uint8_t len, uint8_t whitenCoeff) {
hudakz 0:6f3139e3410e 171
hudakz 0:6f3139e3410e 172 // Implementing whitening with LFSR
hudakz 0:6f3139e3410e 173 uint8_t m;
hudakz 0:6f3139e3410e 174 while(len--) {
hudakz 0:6f3139e3410e 175 for(m = 1; m; m <<= 1) {
hudakz 0:6f3139e3410e 176 if(whitenCoeff & 0x80) {
hudakz 0:6f3139e3410e 177 whitenCoeff ^= 0x11;
hudakz 0:6f3139e3410e 178 (*data) ^= m;
hudakz 0:6f3139e3410e 179 }
hudakz 0:6f3139e3410e 180
hudakz 0:6f3139e3410e 181 whitenCoeff <<= 1;
hudakz 0:6f3139e3410e 182 }
hudakz 0:6f3139e3410e 183
hudakz 0:6f3139e3410e 184 data++;
hudakz 0:6f3139e3410e 185 }
hudakz 0:6f3139e3410e 186 }
hudakz 0:6f3139e3410e 187
hudakz 0:6f3139e3410e 188 /**
hudakz 0:6f3139e3410e 189 * @brief
hudakz 0:6f3139e3410e 190 * @note
hudakz 0:6f3139e3410e 191 * @param
hudakz 0:6f3139e3410e 192 * @retval
hudakz 0:6f3139e3410e 193 */
hudakz 0:6f3139e3410e 194 static inline uint8_t bleWhitenStart(uint8_t chan) {
hudakz 0:6f3139e3410e 195
hudakz 0:6f3139e3410e 196 //the value we actually use is what BT'd use left shifted one...makes our life easier
hudakz 0:6f3139e3410e 197
hudakz 0:6f3139e3410e 198 return swapBits(chan) | 2;
hudakz 0:6f3139e3410e 199 }
hudakz 0:6f3139e3410e 200
hudakz 0:6f3139e3410e 201 /**
hudakz 0:6f3139e3410e 202 * @brief
hudakz 0:6f3139e3410e 203 * @note
hudakz 0:6f3139e3410e 204 * @param
hudakz 0:6f3139e3410e 205 * @retval
hudakz 0:6f3139e3410e 206 */
hudakz 0:6f3139e3410e 207 void blePacketEncode(uint8_t* packet, uint8_t len, uint8_t chan) {
hudakz 0:6f3139e3410e 208
hudakz 0:6f3139e3410e 209 // Assemble the packet to be transmitted
hudakz 0:6f3139e3410e 210
hudakz 0:6f3139e3410e 211 // Length is of packet, including crc. pre-populate crc in packet with initial crc value!
hudakz 0:6f3139e3410e 212 uint8_t i, dataLen = len - 3;
hudakz 0:6f3139e3410e 213 bleCRC(packet, dataLen, packet + dataLen);
hudakz 0:6f3139e3410e 214 for(i = 0; i < 3; i++, dataLen++)
hudakz 0:6f3139e3410e 215 packet[dataLen] = swapBits(packet[dataLen]);
hudakz 0:6f3139e3410e 216 bleWhiten(packet, len, bleWhitenStart(chan));
hudakz 0:6f3139e3410e 217 for(i = 0; i < len; i++)
hudakz 0:6f3139e3410e 218 packet[i] = swapBits(packet[i]); // the byte order of the packet should be reversed as well
hudakz 0:6f3139e3410e 219 }
hudakz 0:6f3139e3410e 220
hudakz 0:6f3139e3410e 221 /**
hudakz 0:6f3139e3410e 222 * @brief
hudakz 0:6f3139e3410e 223 * @note
hudakz 0:6f3139e3410e 224 * @param
hudakz 0:6f3139e3410e 225 * @retval
hudakz 0:6f3139e3410e 226 */
hudakz 0:6f3139e3410e 227 void nrfCmd(uint8_t cmd, uint8_t data) {
hudakz 0:6f3139e3410e 228
hudakz 0:6f3139e3410e 229 // Write to nRF24's register
hudakz 0:6f3139e3410e 230
hudakz 0:6f3139e3410e 231 cs = 0;
hudakz 0:6f3139e3410e 232 spi.write(cmd);
hudakz 0:6f3139e3410e 233 spi.write(data);
hudakz 0:6f3139e3410e 234 cs = 1;
hudakz 0:6f3139e3410e 235 }
hudakz 0:6f3139e3410e 236
hudakz 0:6f3139e3410e 237 /**
hudakz 0:6f3139e3410e 238 * @brief
hudakz 0:6f3139e3410e 239 * @note
hudakz 0:6f3139e3410e 240 * @param
hudakz 0:6f3139e3410e 241 * @retval
hudakz 0:6f3139e3410e 242 */
hudakz 0:6f3139e3410e 243 void nrfWriteByte(uint8_t cmd) {
hudakz 0:6f3139e3410e 244 // transfer only one byte
hudakz 0:6f3139e3410e 245 cs = 0;
hudakz 0:6f3139e3410e 246 spi.write(cmd);
hudakz 0:6f3139e3410e 247 cs = 1;
hudakz 0:6f3139e3410e 248 }
hudakz 0:6f3139e3410e 249
hudakz 0:6f3139e3410e 250 /**
hudakz 0:6f3139e3410e 251 * @brief
hudakz 0:6f3139e3410e 252 * @note
hudakz 0:6f3139e3410e 253 * @param
hudakz 0:6f3139e3410e 254 * @retval
hudakz 0:6f3139e3410e 255 */
hudakz 0:6f3139e3410e 256 void nrfWriteBytes(uint8_t* data, uint8_t len) {
hudakz 0:6f3139e3410e 257 // transfer several bytes in a row
hudakz 0:6f3139e3410e 258 cs = 0;
hudakz 0:6f3139e3410e 259 do
hudakz 0:6f3139e3410e 260 {
hudakz 0:6f3139e3410e 261 spi.write(*data++);
hudakz 0:6f3139e3410e 262 } while(--len);
hudakz 0:6f3139e3410e 263 cs = 1;
hudakz 0:6f3139e3410e 264 }
hudakz 0:6f3139e3410e 265
hudakz 0:6f3139e3410e 266 int main() {
hudakz 0:6f3139e3410e 267 uint8_t data[4];
hudakz 0:6f3139e3410e 268 float* temp = reinterpret_cast < float * > (&data[0]);
hudakz 0:6f3139e3410e 269
hudakz 0:6f3139e3410e 270 // Chip must be deselected
hudakz 0:6f3139e3410e 271 cs = 1;
hudakz 0:6f3139e3410e 272
hudakz 0:6f3139e3410e 273 // Setup the spi for 8 bit data, high steady state clock,
hudakz 0:6f3139e3410e 274 // second edge capture, with a 10MHz clock rate
hudakz 0:6f3139e3410e 275 spi.format(8,0);
hudakz 0:6f3139e3410e 276 spi.frequency(10000000);
hudakz 0:6f3139e3410e 277
hudakz 0:6f3139e3410e 278 cs = 1;
hudakz 0:6f3139e3410e 279 ce = 0;
hudakz 0:6f3139e3410e 280
hudakz 0:6f3139e3410e 281 // Initialize nRF24L01+, setting general parameters
hudakz 0:6f3139e3410e 282 nrfCmd(0x20, 0x12); // on, no crc, int on RX/TX done
hudakz 0:6f3139e3410e 283 nrfCmd(0x21, 0x00); // no auto-acknowledge
hudakz 0:6f3139e3410e 284 nrfCmd(0x22, 0x00); // no RX
hudakz 0:6f3139e3410e 285 nrfCmd(0x23, 0x02); // 4-byte address
hudakz 0:6f3139e3410e 286 nrfCmd(0x24, 0x00); // no auto-retransmit
hudakz 0:6f3139e3410e 287 nrfCmd(0x26, 0x06); // 1MBps at 0dBm
hudakz 0:6f3139e3410e 288 nrfCmd(0x27, 0x3E); // clear various flags
hudakz 0:6f3139e3410e 289 nrfCmd(0x3C, 0x00); // no dynamic payloads
hudakz 0:6f3139e3410e 290 nrfCmd(0x3D, 0x00); // no features
hudakz 0:6f3139e3410e 291 nrfCmd(0x31, 32); // always RX 32 bytes
hudakz 0:6f3139e3410e 292 nrfCmd(0x22, 0x01); // RX on pipe 0
hudakz 0:6f3139e3410e 293
hudakz 0:6f3139e3410e 294 // Set access addresses (TX address in nRF24L01) to BLE advertising 0x8E89BED6
hudakz 0:6f3139e3410e 295 // Remember that both bit and byte orders are reversed for BLE packet format
hudakz 0:6f3139e3410e 296 buf[0] = 0x30;
hudakz 0:6f3139e3410e 297 buf[1] = swapBits(0x8E);
hudakz 0:6f3139e3410e 298 buf[2] = swapBits(0x89);
hudakz 0:6f3139e3410e 299 buf[3] = swapBits(0xBE);
hudakz 0:6f3139e3410e 300 buf[4] = swapBits(0xD6);
hudakz 0:6f3139e3410e 301 nrfWriteBytes(buf, 5);
hudakz 0:6f3139e3410e 302 buf[0] = 0x2A; // set RX address in nRF24L01, doesn't matter because RX is ignored in this case
hudakz 0:6f3139e3410e 303 nrfWriteBytes(buf, 5);
hudakz 0:6f3139e3410e 304 ds1820.begin();
hudakz 0:6f3139e3410e 305
hudakz 0:6f3139e3410e 306 while(1) {
hudakz 0:6f3139e3410e 307 static const uint8_t chRf[] = { 2, 26, 80 };
hudakz 0:6f3139e3410e 308 static const uint8_t chLe[] = { 37, 38, 39 };
hudakz 0:6f3139e3410e 309 uint8_t i = 0;
hudakz 0:6f3139e3410e 310 uint8_t j = 0;
hudakz 0:6f3139e3410e 311 uint8_t ch = 0;
hudakz 0:6f3139e3410e 312
hudakz 0:6f3139e3410e 313 *temp = ds1820.read();
hudakz 0:6f3139e3410e 314
hudakz 0:6f3139e3410e 315 // serial.printf("temp = %3.1f'C\r\n", *temp);
hudakz 0:6f3139e3410e 316
hudakz 0:6f3139e3410e 317 buf[i++] = 0x42; // PDU type, given address is random; 0x42 for Android and 0x40 for iPhone
hudakz 0:6f3139e3410e 318 buf[i++] = 25; // number of following data bytes, max 29 (CRC is not included)
hudakz 0:6f3139e3410e 319
hudakz 0:6f3139e3410e 320 //----------------------------
hudakz 0:6f3139e3410e 321 buf[i++] = MY_MAC_0;
hudakz 0:6f3139e3410e 322 buf[i++] = MY_MAC_1;
hudakz 0:6f3139e3410e 323 buf[i++] = MY_MAC_2;
hudakz 0:6f3139e3410e 324 buf[i++] = MY_MAC_3;
hudakz 0:6f3139e3410e 325 buf[i++] = MY_MAC_4;
hudakz 0:6f3139e3410e 326 buf[i++] = MY_MAC_5;
hudakz 0:6f3139e3410e 327
hudakz 0:6f3139e3410e 328 buf[i++] = 2; // flags (LE-only, limited discovery mode)
hudakz 0:6f3139e3410e 329 buf[i++] = 0x01;
hudakz 0:6f3139e3410e 330 buf[i++] = 0x05;
hudakz 0:6f3139e3410e 331
hudakz 0:6f3139e3410e 332 buf[i++] = 9; // length of the name, including type byte
hudakz 0:6f3139e3410e 333 buf[i++] = 0x08; // TYPE_NAME_SHORT
hudakz 0:6f3139e3410e 334 buf[i++] = 'n';
hudakz 0:6f3139e3410e 335 buf[i++] = 'R';
hudakz 0:6f3139e3410e 336 buf[i++] = 'F';
hudakz 0:6f3139e3410e 337 buf[i++] = '2';
hudakz 0:6f3139e3410e 338 buf[i++] = '4';
hudakz 0:6f3139e3410e 339 buf[i++] = 'L';
hudakz 0:6f3139e3410e 340 buf[i++] = '0';
hudakz 0:6f3139e3410e 341 buf[i++] = '1';
hudakz 0:6f3139e3410e 342
hudakz 0:6f3139e3410e 343 buf[i++] = 5; // length of custom data, including type byte
hudakz 0:6f3139e3410e 344 buf[i++] = 0xff; // TYPE_CUSTOMDATA
hudakz 0:6f3139e3410e 345
hudakz 0:6f3139e3410e 346 buf[i++] = data[0]; // temperature floating point value (four bytes)
hudakz 0:6f3139e3410e 347 buf[i++] = data[1];
hudakz 0:6f3139e3410e 348 buf[i++] = data[2];
hudakz 0:6f3139e3410e 349 buf[i++] = data[3];
hudakz 0:6f3139e3410e 350 //----------------------------
hudakz 0:6f3139e3410e 351
hudakz 0:6f3139e3410e 352 buf[i++] = 0x55; // CRC start value: 0x555555
hudakz 0:6f3139e3410e 353 buf[i++] = 0x55;
hudakz 0:6f3139e3410e 354 buf[i++] = 0x55;
hudakz 0:6f3139e3410e 355
hudakz 0:6f3139e3410e 356 // Channel hopping
hudakz 0:6f3139e3410e 357 if(++ch == sizeof(chRf))
hudakz 0:6f3139e3410e 358 ch = 0;
hudakz 0:6f3139e3410e 359 nrfCmd(0x25, chRf[ch]);
hudakz 0:6f3139e3410e 360 nrfCmd(0x27, 0x6E); // Clear flags
hudakz 0:6f3139e3410e 361 blePacketEncode(buf, i, chLe[ch]);
hudakz 0:6f3139e3410e 362 nrfWriteByte(0xE2); // Clear RX Fifo
hudakz 0:6f3139e3410e 363 nrfWriteByte(0xE1); // Clear TX Fifo
hudakz 0:6f3139e3410e 364 cs = 0;
hudakz 0:6f3139e3410e 365 spi.write(0xA0);
hudakz 0:6f3139e3410e 366 for(j = 0; j < i; j++)
hudakz 0:6f3139e3410e 367 spi.write(buf[j]);
hudakz 0:6f3139e3410e 368 cs = 1;
hudakz 0:6f3139e3410e 369
hudakz 0:6f3139e3410e 370 nrfCmd(0x20, 0x12); // TX on
hudakz 0:6f3139e3410e 371 ce = 1; // Enable Chip
hudakz 0:6f3139e3410e 372 wait_ms(50);
hudakz 0:6f3139e3410e 373 ce = 0; // (in preparation of switching to RX quickly)
hudakz 0:6f3139e3410e 374 ds1820.startConversion(); // Start temperature conversion
hudakz 0:6f3139e3410e 375 wait(1); // Broadcasting interval (change to your needs)
hudakz 0:6f3139e3410e 376 }
hudakz 0:6f3139e3410e 377 }