Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us
 

Forward Logo (image)      

Bluetooth Low Energy (BLE) made simple with pfodApp™
Custom BLE controls with no Android coding

by Matthew Ford 15th January 2017 (originally posted 26th May 2015)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

Custom BLE – No Programming required
Arduino 101 / Genuino 101, RFduino,
Adafruit Bluefruit, RedBearLab and HM_10 modules.




Introduction – Custom BLE made easy

Update 15th January 2017: Lancaster Uni is changing the micro:bit UART service (issue #259). pfodApp supports both the old and new style services so this change will not effect pfodApp users.
Update 25th August 2016: added BBC micro:bit support.
Update 16th August 2016: added Adafruit Feather LE SPI board support.
Update 1st July 2016: pfodApp V2.0.213+ needs pfodParser library V2.35+. Small change to image dwg BLE examples.
Update 2nd June 2016: Revised All code for latest board libraries and for pfodApp V2, using pfodDesignerV2. The result is improved connection reliability and speed.

This tutorial covers six (7) common BLE modules and for each one shows you how to control them via custom menus from your Android mobile without doing any programming.

The free pfodDesignerV2 Android app lets you create custom menus and then generates all the code for your particular BLE module. pfodApp is then used to display your custom menu on your Android mobile and let you control your module.
No Android or Arduino coding is required.

The modules covered are:-
Arduino 101 / Genuino 101
RFduino
RedBearLab BLE Shield
Adafruit Bluefruit LE UART Friend and Flora Boards
Adafruit Bluefruit LE SPI (i.e. Bluefruit LE Shield, Bluefruit LE Micro, Feather 32u4 Bluefruit LE, Feather M0 Bluefruit LE or Bluefruit LE SPI Friend)
Itead BLE shield and other HM-10 modules
BBC micro:bit – on a separate page, using C++ and Netbeans IDE

Support for more modules will be added as requested.

For each of these modules the hardware and software set up is covered and code for your custom menu is generated. Also a more extensive sample sketch is supplied for each module. That sample sketch covers more features of pfodApp such as:- sub-menus, plotting and data logging, text input, sliders and multi- and single- selection lists. BLE is designed for short packets so design your menus accordingly and optimize the responses to only send the changes not the whole menu. For simplicity the Sample Screens sketches dos not do this optimization.

Finally there are a few BLE trouble shooting tips.

How pfodApp is optimized for short BLE style messages

Bluetooth Low Energy (BLE) or Blutooth V4 is a completely different version of Bluetooth. BLE has been optimized for very low power consumption. pfodApp is a general purpose Android app whose screens, menus, buttons, sliders and plots are completely defined by the device you connect to.

BLE only sends 20 bytes in each message. Fortunately the pfod Specification was designed around very small messages. Almost all of pfod's command are less then 20 bytes. The usual exception is the initial main menu message which specifies what text, menus, buttons, etc pfodApp should display to the user, but the size of this message is completely controlled by you and you can use sub-menus to reduce the size of the main menu.

The pfod specification also has a number of features to reduce the message size. While the BLE device must respond to every command the pfodApp sends, the response can be as simple as {} (an empty response). If you need to update the menu the user is viewing in response to a command or due to a re-request, you need only send back the changes in the existing menu, rather then resending the entire menu. These features keep the almost all messages to less than 20 bytes.

pfodApp V2 menus have been re-designed to further reduce the message sizes. pfodApp V2 caches menus across re-connections so that the whole menu only needs to be sent once. Thereafter short menu updates can be sent.

Creating the Custom Android Menus and Generating the Code

Before looking at each of these BLE modules, pfodDesignerV2 will first be used to create a custom menu to turn a Led on and off. pfodDesignerV2 can then generate code tailored to the particular hardware you select.

You can skip over this step and come back to it later if you like. The section on each module, below, includes the completed code sketch for this example menu generated for that module.

The free pfodDesignerV2 is used to create the menu and show you an accurate preview of how the menu will look on your mobile. The pfodDesignerV2 allows you to create menus and sub-menus with buttons and sliders optionally connected to I/O pins and generate the sketch code for you (see the pfodDesigner example tutorials) but the pfodDesignerV2 does not cover all the features pfodApp supports. See the pfodSpecification.pdf for a complete list including data logging and plotting, multi- and single- selections screens, sliders, text input, etc. Each BLE device below has a sample screens sketch illustrating some of these other features.

Create the Custom menu to turn the Arduino LED on and off

The tutorial Design a Custom menu to turn the Arduino Led on and off has step by step instructions for creating this menu using pfodDesignerV2.



If you don't like the colours of font sizes or the text, you can easily edit them in pfodDesignerV2 to whatever you want and see a WYSIWYG (What You See Is What You Get) display of the designed menu.

The rest of this tutorial will use that menu design to control the LED via various BLE modules. pfodDesignerV2 lets you generate the Arduino code for this menu for each of these BLE modules as well as WiFi, Ethernet, ESP8266, SMS and Bluetooth shields.

Using the Itead BLE shield (and other HM-10 devices)



The Itead BLE shield is the simplest BLE shield to use. It provides AT commands to configure it and then just runs as a Serial to BLE bridge.

Setting up the Itead BLE shield.

In this tutorial we are using an Arduino UNO as the base board. Only two things need to be done to the Itead BLE shield to configure its hardware for use. No AT commands are need.
i) set the TX RX links as shown above, D0 linked to the TX side and D1 linked to the RX side
ii) set the 5V / 3V switch to 5V, since the UNO is a 5V board



That's all! Now remove the BLE shield from the UNO to program.

In the pfodDesignerV2 choose the Generate Code button for the menu you have designed (see Design a Custom menu to turn the Arduino Led on and off has step by step instructions). To generate the code, choose from the Bluetooth Low Energy sub-menu, the Itead BLE Shield.



For the Itead BLE shield, a Serial connection at 9600 baud is set since the
Itead BLE shield, as set up above, just connects to the Tx/Rx of Uno's Serial connection. Click on “Write Code to file” to generate the code and write it to the pfodDesignerV2.txt file.




Then transfer the /pfodAppRawData/pfodDesignerV2.txt file from your mobile to your computer as described in Design a Custom menu to turn the Arduino Led on and off. Copy and paste the code into the Arduino IDE and program the UNO board. Here is the completed sketch, Itead_BLE_led_control.ino

Remember to remove the Itead BLE shield before programming the UNO, as the Itead BLE shield is connected the the same pins (D0 and D1) as the USB.

Plug the Itead BLE shield back in and open pfodApp and create a BLE connection for it as described in pfodAppForAndroidGettingStarted.pdf. Then connect, and pfodApp will display the menu you have designed and allow you to control the LED on the Arduino Uno board.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch SampleScreensSerial.ino provides examples of various screens, plotting and data logging. Most of the screens have no formatting to keep the messages clear and simple. The color selector under Sliders is an exception. You can add your own colours and font styles, using the pfodDesignerV2 as a guide. Also see the pfodDemo app for other examples.




Using the Adafruit Bluefruit LE UART Friend and Flora Boards



The Adafruit Bluefruit LE UART Friend and Flora Bluefruit LE boards are the next simplest BLE to use, although you would not guess that from reading Adafruit's introduction to this board. It also provides AT commands to configure it, but as it comes is will run as a Serial to BLE bridge at 9600 baud (fixed). However unlike Itead's BLE offering, you need to do some soldering to get Adafruit's board connected.

Setting up the Adafruit Bluefruit LE UART Friend.

Solder in the header provided with the board and set the UART / CMD switch to UART.


Then connect the board to an Arduino UNO using the following connections :-
TXO to Arduino RX (Pin 0) (Green lead in photo)
RXI to Arduino TX (Pin 1) (Brown lead in photo)
VIN to 5V (Red lead in photo)
CTS to GND (Yellow lead in photo)
GND to GND (Black lead in photo)

That's it set up done.

In the pfodDesignerV2 choose the Generate Code button for the menu you have designed (see Design a Custom menu to turn the Arduino Led on and off has step by step instructions). To generate the code, choose from the Bluetooth Low Energy sub-menu, the Adafruit's Bluefruit LE UART. Go back and click on “Write Code to file” to generate the code and write it to the pfodDesignerV2.txt file.

Then transfer the /pfodAppRawData/pfodDesignerV2.txt file from your mobile to your computer as described in Design a Custom menu to turn the Arduino Led on and off. Copy and paste the code into the Arduino IDE and program the UNO board. Here is the completed sketch, Adafruit_BLE_UART_led_control.ino

Remember to remove the TX and RX leads to the Bluefruit LE UART before programming the UNO, as the Bluefruit LE UART is connected the the same pins (D0 and D1) as the USB.

Plug the TX and RX leads back in and open pfodApp and create a BLE connection for it as described in pfodAppForAndroidGettingStarted.pdf. Then connect and pfodApp will display the menu you have designed and allow you to control the LED on the Arduino Uno board.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch SampleScreensSerial.ino provides examples of various screens, plotting and data logging. Most of the screens have no formatting to keep the messages clear and simple. The color selector under Sliders is an exception. You can add your own colours and font styles, using the pfodDesignerV2 as a guide. Also see the pfodDemo app for other examples. See above for some screen shots from the SampleScreensSerial.ino

Using the Adafruit Bluefruit LE SPI Boards



The Adafruit Bluefruit LE SPI boards include:- Bluefruit LE Shield, Bluefruit LE Micro, Feather 32u4 Bluefruit LE, Feather M0 Bluefruit LE or Bluefruit LE SPI Friend The board shown here is the Feather 32u4 Bluefruit LE.

Setting up the Adafruit Bluefruit LE SPI Boards

No hardware set up is needed but you need to install an extra board configuration and an extra support library (in addition to the pfodParser Library)

Open Arduino V1.6.8's File->Preferences and add this url to the “Additional Boards Manager URLs'” (comma separated)
https://adafruit.github.io/arduino-board-index/package_adafruit_index.json

You also need to download the arduino-BLEPeripheral-master.zip from https://github.com/adafruit/Adafruit_BluefruitLE_nRF51 (click the Clone or Download button and choose Download Zip)
If you are on Windows, you may also need to download and install some drivers from https://github.com/adafruit/Adafruit_Windows_Drivers/releases/download/1.0.0.0/adafruit_drivers.exe
Adafruit has detailed instructions on setting up your Arduino IDE to program these boards.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch Bluefruit_SPI.ino provides a simple example of turning the LED on and off, setting PWM output and charting the ADC A0 voltage reading. This Android interface was completely designed by pfodDesignerV2, which then generated the complete Arduino sketch, Bluefruit_SPI.ino You can use pfodDesignerV2 to create your own interface for these boards.

Using the RFduino BLE board



The RFduino is my next favourite board. It is small and simple and has commands to go into low power mode and has neat battery shield you can just plug-in, shown above with the optional Led, push button shield on top. There is also a relay shields, a servo shield, etc, available.

To program it you need to add the USB shield shown above (under the main board). While the USB shield is designed to plug directly into your USB port, I found that very inconvenient on my laptop so I add a USB extension cable.

Setting up the RFduino

No hardware set up is needed but you need to install an extra board configuration. Open Arduino V1.6.8's File->Preferences and add this url to the “Additional Boards Manager URLs'” (comma separated)
http://rfduino.com/package_rfduino_index.json
Then open the Tools->Board->Board Manager and type RFduino into the search bar. Then install the RFduino board support. (V2.3.1 was used for these examples)

You will get an error message Invalid library found in ...\AppData\Local\Arduino15\packages\RFduino\hardware\RFduino\2.3.1\libraries\RFduinoNonBLE: because that 'library' has no library code, just examples. Just ignore this error.
When you compile you will also get a warning
Warning: platform.txt from core 'RFduino Boards' contains deprecated recipe but the compile and load seems to work just fine.

The RFduino board does not have an LED on D13, but instead as tri-colour leds on its Led push button shield driven by D2,D3 and D4. So go back into the pfodDesignerV2 and edit your Led Control menu you have designed (see Design a Custom menu to turn the Arduino Led on and off has step by step instructions) to change the output connected to the LED On/Off slider from D13 to D3 (say) for the Green led



Then in the Generate Code menu, choose from the Bluetooth Low Energy sub-menu, the
RFduino setting. Go back and click on “Write Code to file” to generate the code and write it to the pfodDesignerV2.txt file.

Then transfer the /pfodAppRawData/pfodDesignerV2.txt file from your mobile to your computer as described in Design a Custom menu to turn the Arduino Led on and off. Copy and paste the code into the Arduino IDE and program the RFduino board. Here is the completed sketch, RFduino_led_control.ino

Open pfodApp and create a BLE connection for it as described in pfodAppForAndroidGettingStarted.pdf. Then connect and pfodApp will display the menu you have designed and allow you to control the Green LED on the RFduino Led shield. You can use the pfodDesignerV2 to add two other controls for the Red and Blue leds also.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch SampleScreensRFduino.ino provides examples of various screens, plotting and data logging. Most of the screens have no formatting to keep the messages clear and simple. The color selector under Sliders is an exception. You can add your own colours and font styles, using the pfodDesignerV2 as a guide. Also see the pfodDemo app for other examples.

The SampleScreensRFduino.ino has been customized to the RFduino. Instead of plotting A0 against time, it plots the internal temperature in deg C. Also the Colour Selector Example actually changes the colour of the RGB led on the optional Led push button shield. You need to reflect the led output off a white card to mix the colour output of the three leds.

Using Arduino 101 / Genuino 101 BLE board



Arduino has recently released
Arduino 101 / (Genuino 101 outside USA) which has an Intel Curie processor, accelerometer and clock as well as BLE connectivity in an UNO form factor. The built in accelerometer and clock are a plus, but the size is not as convenient for a wearable application, however the standard UNO form factor means you can easily add a wide variety of other shields. This board can be coded to appear as any of the 'standard' BLE services. The pfodDesignerV2 generated code configures the Arduino 101 / Genuino 101 as a Nordic BLE UART service.

Setting up the Arduino 101 / Genuino 101

You need to install Arduino V1.6.8 IDE and then from the Board Manager add the Curie board support (V1.0.5+). This adds install added USB virtual COM drivers as well. There is no hardware set up to be done on the board itself.

In the pfodDesignerV2 choose the Generate Code button for the menu you have designed (see Design a Custom menu to turn the Arduino Led on and off has step by step instructions). To generate the code, choose from the Bluetooth Low Energy sub-menu, Arduino/Genuino 101. Go back and click on “Write Code to file” to generate the code and write it to the pfodDesignerV2.txt file.

Then transfer the /pfodAppRawData/pfodDesignerV2.txt file from your mobile to your computer as described in Design a Custom menu to turn the Arduino Led on and off. Copy and paste the code into the Arduino IDE and program the 101 board. Here is the completed sketch, Arduino101_led_control.ino Note: It takes a few seconds for the 101's serial port to come up after programming. Just keep clicking on Tools in the IDE until the port shows up.

Open pfodApp and create a BLE connection for it as described in pfodAppForAndroidGettingStarted.pdf. Then connect and pfodApp will display the menu you have designed and allow you to turn the Arduino/Genuino 101's led on and off.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch SampleScreensArduino101.ino provides examples of various screens, plotting and data logging. Most of the screens have no formatting to keep the messages clear and simple. The color selector under Sliders is an exception. You can add your own colours and font styles, using the pfodDesignerV2 as a guide. Also see the pfodDemo app for other examples.

The SampleScreensArduino101.ino has been customized to the Arduino 101 / Genuino 101. Instead of plotting A0 against time, it plots the 3 axis accelerometer outputs scaled so that 1024 is about 1G.




Using the RedBearLab's BLE shield



The RedBearLab's Bluetooth Low Energy Shield V2.1 is
one of a number of RedBearLab modules that uses a Nordic nRF8001 chip. The picture above shows this shield plugged in on top of an Arduino UNO, but the larger memory of a Arduino Mega 256 is needed for the latest library and pfodDesignerV2 generated code. RedBearLab provides a library to program the shield, but here I have used the BLEPeripheral library V0.2.0 instead because it is almost exactly the same as code used for the Arduino 101/Genuino 101 board.

Setting up the RedBearLab Bluetooth Low Energy Shield V2.1

You need to install the BLEPeripheral library. Open the Library Manager in the IDE and type BLEPeripheral in the search bar. Select BLEPeripheral by Sandeep Mistry V0.2.0 and install it.

Configure the boards jumpers as shown below. Pins 13,12,11 SPI to ICSP side. Pin 9 to REQN side and Pin 8 to RDYN side.

That's it, configuration complete. Plug the shield into the Arduino UNO board.

In the pfodDesignerV2 choose the Generate Code button for the menu you have designed (see Design a Custom menu to turn the Arduino Led on and off has step by step instructions). To generate the code, choose from the Bluetooth Low Energy sub-menu, the
RedBearLab BLE Shield. Go back and click on “Write Code to file” to generate the code and write it to the pfodDesignerV2.txt file.

Then transfer the /pfodAppRawData/pfodDesignerV2.txt file from your mobile to your computer as described in Design a Custom menu to turn the Arduino Led on and off. Copy and paste the code into the Arduino IDE and program the Mega 256 board. Here is the completed sketch, RedBearLab_led_control.ino

Open pfodApp and create a BLE connection for it as described in pfodAppForAndroidGettingStarted.pdf. Then connect and pfodApp will display the menu you have designed and allow you to control the LED on the Arduino Mega 256 board.

Sample Screens

The pfodDesignerV2 only supports a sub-set of the screens that pfodApp supports. For a complete list see the pfodSpecification.pdf. The sketch SampleScreensRedBearLab.ino provides examples of various screens, plotting and data logging. Most of the screens have no formatting to keep the messages clear and simple. The color selector under Sliders is an exception. You can add your own colours and font styles, using the pfodDesignerV2 as a guide. Also see the pfodDemo app for other examples.

BBC micro:bit

The BBC micro:bit in not Arduino based like the rest of the boards shown here. The micro:bit is programmed via C++ (and other on-line languages). The pfodDesignerV2 generates a pfodMenu.cpp class that when combined with the pfodParserMicroBit support library will connect to pfodApp and let you control and log data from your micro:bit.

The tutorial Android Controlled C++ micro:bit shows you how to set up the Netbeans IDE and support libraries to compile the code generated by pfodDesignerV2 and program your micro:bit to display the above menu and chart.

Trouble Shooting BLE on Android

Note: not all Android mobiles support BLE. You can check if your mobile supports BLE with the free Nordic nRF Master Control Panel (BLE) android app . If your mobile does not support BLE, then pfodApp will not display the +BLE button in the add/edit connections screen.

The BLE hardware is still being ironed out. On the Android side, BLE support was introduced in V4.2 but that version and V4.3 was very buggy. V4.4 improved the BLE support and these modules were tested on an ASUS Zen Phone 5 running V4.4.2. Other phones may vary. Android V5 is reported to be better, but has not been tested yet.

ASUS Zen Phone 5 running V4.4.2 worked fine with few interruptions when using the latest board libraries and code sketches. If there is an interruption, pfodApp automatically reconnects within a few seconds and the data logging is appended to the existing log file so it is still very usable. If the menu initial display is corrupted due to bad data, you will need to clear the menu cache for this connection using the menu option on this connection's the Add/Edit connection screen.

On the module side, there are various chip sets that board manufactures use to provide BLE support. Some are better then others, some scan better, some have less drop outs etc.

If the Bluetooth connection seems to get stuck or will not connect, try exiting pfodApp and Force Close pfodApp from the mobile's settings menu and turning the mobile's Bluetooth off and on and turning the Arduino BLE module off and on. If that does now work try turning the mobile's Bluetooth off and restarting the phone and then turning the Bluetooth back on to completely clear the Android BLE stack.

Why is BLE so hard to use?

Bluetooth used to be easy to use, every Android and iPhone and most computers supported Bluetooth V2. The Bluetooth Serial Port Profile (SPP) was the standard way to get a general data connection via Bluetooth and all Arduino Bluetooth V2.0 shields supported it.

When Bluetooth SIG introduced Bluetooth Low Energy everything changed. For some unknown reason Bluetooth SIG did not define a standard general purpose serial service for Bluetooth V4. This means that each board manufacture defines their own connection service and your mobile app has to recognize each one. pfodApp overcomes this by pre-configuring the connection details for a variety of common boards. If you have a board that is not supported, contact pfod.com.au at support to get its connection details added to pfodApp.

Bluetooth V4 does define a number of 'standard' connections, like heat rate and battery monitoring, etc. pfodApp does not connect to any of these. If you want to log and plot that data with pfodApp, send the data to pfodApp in CSV format.

On the Arduino side there is no standard BLE library, each BLE board manufacture has their own library, each with its own particularities, to access their particular BLE chipset. pfodDesignerV2 helps out here by generating a complete sketch for each of the supported boards to get you up and running quickly.

Given the issues outlined above, why should you use BLE? BLE was designed for low energy use, so if you are building a device that needs to run on a couple of coin cells for 6 months then BLE is ideal. On the other hand if you just want a shield to communicate with your Arduino then a Bluetooth V2 shield like this one or a cheap WiFi shield like this one may be better.

Of course the other reason for using BLE is because you bought one (perhaps by mistake). In any case pfodApp and pfodDesignerV2 will get you up and running with a custom control quickly and easily.

Conclusion

This tutorial has shown how you can easily communicate and control Arduino style boards and modules via Bluetooth Low Energy (Bluetooth LE or BLE).
No Android programming is required. pfodApp handles all of that.
No Arduino coding is required. The (free) pfodDesignerV2 generates complete sketches for each of these modules, as well as for ESP8266 and WiFi, Bluetooth and SMS shields.

The Sample Screens sketches provided for each board show how to code of some of the other screens available with pfodApp.

Although there is no standard for a general purpose UART connection to a BLE device, pfodApp has pre-configured a number of the common BLE boards connection parameters so you can use the same pfodApp for all of them.

AndroidTM is a trademark of Google Inc. For use of the Arduino name see http://arduino.cc/en/Main/FAQ


pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.




Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2017 Forward Computing and Control Pty. Ltd. ACN 003 669 994