Introduction: Simple WiFi to BLE (Bluetooth Low Energy) Bridge
Update 6th March 2023: The Adafruit nRF52832 sketches no longer compile with the latest Adafruit nRF52832 board support. This page is maintained for interest only.
See ESP32 C3 BLE to WiFi bridge for an alternative
Update 4th December 2017 -- revised Feather nRF52 sketches and debugging tips. Added pictures of bridge mounted in box.
This simple project provides WiFi access to any Bluetooth Low Energy (BLE) module that implements Nordic's UART with TX Notify. The Wifi2BLE bridge is completely transparent. It just passes the WiFi data to the BLE device and passes the BLE device's data back to the WiFi connection.
The free pfodDesignerV3 generates code to implement a Nordic UART Service on many BLE modules including, Adafruit Bluefruit Feather nRF52, Arduino/Genuino 101, RedBearLab BLE Nano V2 and V1-V1.5, 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) and any other boards which let you program your own service.
This Wifi2BLE bridge is suitable for inexperienced constructor who can solder. It uses only two boards, one ribbon cable and two resistors.
As well as detailed testing for each board, this instructable includes a Help – It Does Not Work step with more fault finding tips.
These instructions are also available online
Why this Project?
This project solves a number of problems that BLE (Bluetooth Low Energy) has.
- Only recent mobiles and computers support BLE. The Wifi2BLE bridge allows any mobile or computer with a WiFi connection to connect to and control the BLE device.
- BLE has limited range. The Wifi2BLE bridge allows you to access the BLE device from anywhere in the house (where there is a WiFi connection) and from outside via the Internet.
- Creating BLE apps require you learn Android or iOS. The Wifi2BLE bridge provides a universal telnet connection via any terminal program. Further more you can readily add a web page to the WiFi module to create your own custom interface.
- BLE V5 promises mesh networking to connect all your home BLE devices, once the software catches up with the latest Bluetooth specification. The Wifi2BLE bridge uses a BLE V5 device and so will provide internet access to this home network when it arrives.
My immediate use for this project is to add the lounge room BLE light control to the WiFi network to allow it to be controlled from anywhere in the house. Although this tutorial uses pfodApp as the example of controlling the BLE boards via WiFi, you do not need to purchase pfodApp to complete this project.
Step 1: Parts List
Cost ~US$60 as at 30th Nov 2017, excluding shipping + a small plastic enclosure
Adafruit Feather nRF52 Bluefruit LE – nRF52832 – ~US$25 https://www.adafruit.com/product/3406
Adafruit HUZZAH ESP8266 Breakout - ~US$10 https://www.adafruit.com/product/2471
Ribbon Cable with separate female ends – Bus Pirate Cable ~US$5 https://www.sparkfun.com/products/9556 OR 10-pin IDC Socket Rainbow Breakout Cable ~US$4 https://www.sparkfun.com/products/9556 OR similar
Break Away Male Headers - Right Angle – ~US$2 https://www.sparkfun.com/products/553
USB to TTL 3V3 Serial Cable – ~US$10 https://www.sparkfun.com/products/12977 (Preferred as it has the pins labelled) OR https://www.sparkfun.com/products/12977 (pins are NOT labelled)
USB A to Micro B cable – ~US$4 https://www.sparkfun.com/products/12977 (3 foot long) OR ~US$3 https://www.sparkfun.com/products/12977 (6 inches long) OR ~US$2 https://www.sparkfun.com/products/12977 (6 inches long) OR ~US$5 https://www.sparkfun.com/products/12977 (6 foot long) OR similar
2 x 100 ohm resistors – ~US$1 https://www.sparkfun.com/products/12977
USB power supply (500mA or more) – ~US$6 https://www.sparkfun.com/products/12890 OR ~US$7 https://www.adafruit.com/product/1994 OR similar
Arduino IDE V1.8.5 and a computer to run it on.
For a plastic box I used one from Jaycar UB5 (blue) 83mm x 54mm x 31mm ~A$4
Step 2: Wifi2BLE Circuit
The Wifi2BLE circuit is shown above. A pdf version is here. As you can see the circuit is very simple. Just 4 wires and two 100 ohm protection resistors. The protection resistors are in case you miss-connect the TX / RX lines after programming the HUZZAH ESP8266 or the Feather nRF52.
NOTE: The Feather nRF52 board marking for the TX and RX pins are in-correct. The TX pin is actually the one next to the DFU pin and the RX pin is the one next to the MISO pin.
Make sure you connect the TX/RX lines as shown above. Fortunately the protection resistors did their job and boards were not damaged while I sorted out why the boards were not taking to each other.
Step 3: Wifi2BLE Construction
Right-angled header pins where soldered onto the HUZZAH ESP8266 to allow it to be unplugged for programming. Four wires of the ribbon cable are used to connect the two boards. Keep the female pin connectors and cut off the other end of the ribbon cable. My ribbon cable had the same colours as the Sparkfun USB to TTL programming cable so I chose the wire to match it. Black for GND, Red for 5V+, Orange for TX (connects to ESP8266 RX) and Brown for RX (connects to ESP8266 TX)
I wired the protections resistors on the back of the Feather nRF52 board. (Observant constructors who know their resistor codes will see I have used two 68 ohm resistors I hand on hand instead of 100 ohm ones) and then insulated them with some shrink wrap.
The Red cable is soldered to the Feather nRF52 USB pin to pick up the USB 5V to power the HUZZAH ESP8266 module and the Black cable is soldered to the Feather nRF52 GND pin.
That all there is except for programming the modules, as described below, putting it in a plastic box and plugging a USB power supply into the Feather nRF52.
Step 4: Wifi2BLE Programming
Programming the HUZZAH ESP8266
To program the shield follow the instructions given on https://github.com/esp8266/Arduino under Installing With Boards Manager. When opening the Boards Manager from the Tools → Board menu and select Type Contributed and install the esp8266 platform. This project was compiled using the ESP8266 version 2.3.0. Other versions will have their own set of bugs and may not work with this code.
NOTE: DO NOT use the Adafruit Board install as the sketch used here will not compile under that code.
Close and re-open the Arduino IDE and you can now select “Adafruit HUZZAH ESP8266” from the Tools → Board menu.
You also need to install the latest version of pfodESP8266WiFiBufferedClient library. This library works with ESP8266.com IDE plug-in V2.3. (If you have previously installed the pfodESP2866WiFi library, delete that library directory completely.)
- a) Download this pfodESP8266WiFiBufferedClient.zip file to your computer, move it to your desktop or some other folder you can easily find
- b) Then use Arduino 1.8.5 IDE menu option Sketch → Import Library → Add Library to install it. (If Arduino does not let you install it because the library already exists then find and delete the older pfodESP8266BufferedClient folder and then import this one)
- c) Stop and restart the Arduino IDE and under File->Examples you should now see pfodESP8266BufferedClient.
Setting the Network ssid and password and IP and port
After you have installed the pfodESP8266BufferedClient library, open the Arduino IDE and copy this sketch, Wifi_Bridge.ino, into the IDE. Before you program the module, you need to set your network's ssid and password and choose an unused IP.
Edit these three lines near the top of Wifi_Bridge.ino
char ssid[] = "**** ***"; <br>char password[] = "**** *****"; <br>char staticIP[] = "10.1.1.180";
I use the Fing (Android or iOS) app to scan my local network and identify the IP that are already used. It is usually safe to choose an unused IP in the range .180 to .254
To start with you can leave the portNo as 23, the standard port for a telnet connection.
Once you have made those changes you can program the ESP8266.
Programming the HUZZAH ESP8266
To program the HUZZAH ESP8266, connect the USB to Serial cable as shown in the photo. Check the photo and your wiring. Also see ESP8266 Programming Tips (espcomm failed)
I use the Sparkfun USB to TTL 3V3 Serial Cable because it has the TX and RX leads marked. Make sure the TX lead is plugged into the RX pin and RX lead is plugged into the TX pin as shown above. If you are using the Adafruit cable, it does not have the terminals marked but is colour coded, red is power, black is ground, green is TX and white is RX.
Plug in the USB to Serial cable into your computer and select it COM port in the Tools → Port menu. Leave the CPU Frequency, Flash Size and Upload Speed at their default settings.
Then put the Adafruit HUZZAH ESP2866 module into programming mode by holding down the GPIO0 push button and clicking the Reset push button and then releasing the GPIO0 push button. The GPIO0 led should remain dimly lit. Then select File → Upload or use the Right Arrow button to compile and upload the program. If you get an error message uploading check your cable connections are plugged in the correct pins and try again.
Once the programming is complete the Red Led on the module should light up. That indicates it has successfully connected to your local network and started the server to accept a connection.
Testing the HUZZAH ESP2866
To test the HUZZAH ESP2866, leave the programming cable connected and open the Arduino IDE Tools → Serial Monitor and set 9600 baud (bottom right corner). Then open a terminal program on your computer, I use TeraTerm for Windows and CoolTerm for Mac, and connect to the IP and portNo you set in the Wifi_Bridge.ino sketch.
When you connect the Red Led on the module should start flashing, indicating there is a connection. You should now be able to type into your computers terminal window and the characters should appear in the Arduino IDE's monitor window and vis versa.
Wifi Connection Timeout
The Wifi_Bridge.ino code has a connection time.
uint32_t connectionTimeout = 60000; // 60sec connection time out
If there is no WiFi data received by the HUZZAH ESP8266 module for 60 secs then the modules closes the connection and waits for a new one. This ensures the module recovers from 'half closed' connections which happen went the client just disappears due to bad WiFi connection, power loss at the router or forced shut down of the client. See Detection of Half-Open (Dropped) TCP/IP Socket Connections for more details.
This connection time out is set to 60 sec. but can decreased or increased as needed. Setting it to 0 means never time out which is not recommended.
Programming the Feather nRF52
To program the Feather nRF52, follow the instructions on downloading and installing the Arduino Board support for the Feather nRF52. Check you can connect to, and program the board via the USB cable.
NOTE: Unplug the HUZZAH ESP8266 module from the Feather nRF52 before trying to program the Feather.
Connecting the Feather nRF52 to a BLE device consists of two steps. First identifying the MAC address (and type) on the device and checking it supports the Nordic UART service with TX Notify and then transferring that address and type to the bridge sketch.
Step 5: Connection to a BLE Device
Once the HUZZAH ESP8266 is programmed, you need to code the BLE address and type of the device you want to connect to into the Feather nRF52 sketch. This is a two step process. I) Run the scanning program to find nearby BLE devices that implement the Nordic UART Service (TX Notify) and then copy that scanner's output into the Feather bridge sketch to get it to connect to that device.
Scanning for BLE Peripherals
To identify compatible BLE peripherals, load the central_bleuart_scanner.ino program into your Feather nRF52. This program continually scans for new devices and then checks if they support the Nordic UART service with TX Notify.
Nordic UART Service
The Nordic UART Service consists of three parts, the Service UUID and the RX and TX characteristics. Here is some sample code generated by pfodDesignerV3 for the RedBear NanoV2
BLEService uartService = BLEService("6E400001B5A3F393E0A9E50E24DCCA9E");
BLECharacteristic rxCharacteristic = BLECharacteristic("6E400002B5A3F393E0A9E50E24DCCA9E", BLEWrite, BLE_MAX_LENGTH);
BLECharacteristic txCharacteristic = BLECharacteristic("6E400003B5A3F393E0A9E50E24DCCA9E", BLENotify , BLE_MAX_LENGTH);
pfodDesignerV3 can generate sample code for a number of BLE modules. However because there is no BLE specification for a 'standard' UART connection not all modules use the Nordic UART service. For example HM-10 modules (Itead BLE shield), RFduno and Romeo BLE use their own unique uart services and so will not connect to the Feather nRF52.
The following boards will connect:- Adafruit Bluefruit Feather nRF52, Arduino/Genuino 101, RedBearLab BLE Nano V2 and V1-V1.5, 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) As well as boards that can be coded with custom Services or boards that can use BLEPeripheral library.
The example board used here is the Arduino/Genuino 101 with code generated by pfodDesignerV3. The free pfodDesignerV3 allows you to create pfodApp menus and then generates the Arduino code necessary to show that exact menu on your Android mobile using (paid) pfodApp. pfodApp will also connect via Wifi, as well as Bluetooth Classic, BLE and SMS, and so can connect via this Wifi2BLE bridge. However even if you don't want to use pfodApp as your control app, you can still use the pfodDesignerV3 to generate a Nordic UART service code for your BLE board.
This tutorial link describes setting up the Arduino/Genuino 101 BLE and creating a simple LED on/off control with pfodDesignerV3. The sketch produced by the pfodDesignerV3 is Arduino101_led_control.ino
Programming the Arduino/Genuino 101 with the Arduino101_led_control.ino will let you connect with pfodApp and display the following screen on your Android mobile.
However this is just an example and you do not need to purchase pfodApp to complete this project. The important part is that the free pfodDesignerV3 app has generate code for the Arduino/Genuino 101 that implements a Nordic UART Service. So lets scan for it.
However this is just an example and you do not need to purchase pfodApp to complete this project. The important part is that the free pfodDesignerV3 app has generate code for the Arduino/Genuino 101 that implements a Nordic UART Service. So lets scan for it.
Scanning for a Nordic UART Service
With the HUZZAH ESP8266 module disconnected from the Feather nRF52, program the Feather nRF52 with the central_bleuart_scanner.ino and then open the Arduino IDE Tools → Serial Monitor and set the baud rate to 9600.
Powering up the Arduino/Genuino 101 gives the output shown in the first screen shot above.
As you can see the scanner found two BLE devices, a Arduino/Genuino 101 with found a Nordic UART service on it, BUT the TX characteristic did not support Notify. The scanner also found an RFduino BLE module but RFduino does not use the Nordic UART Service but instead uses its own one. The Bluetooth Committee bares the blame for this for not specifying a 'standard' UART Service, instead leaving each manufacturer to make up their one.
As covered above the 101 code was generated by pfodDesignerV3 primarily for use with pfodApp. pfodApp is not as picking about TX Notify as the Feather nRF52 uart code is. pfodApp works with the 101 as programmed but the Feather nRF52 insists on TX Notify so a small change to the code is needed to satisfy the Feather.
Open the Arduino101_led_control.ino in the Arduino IDE and near the top you will find these two lines. The second line is just a description of the characteristic and does not effect the operation.
BLECharacteristic txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLEIndicate, BLE_MAX_LENGTH);<br>BLEDescriptor txNameDescriptor = BLEDescriptor("2901", "TX – (Indicate)");
Change them to
BLECharacteristic txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", <strong>BLENotify</strong>, BLE_MAX_LENGTH);
BLEDescriptor txNameDescriptor = BLEDescriptor("2901", "TX - (Notify)");
Then re-program the 101 with this edited version, Arduino101_Notify_led_control.ino. Then when you re-run the Feather nRF52 scanner (central_bleuart_scanner.ino), the Serial Monitor shows the second screen shot above.
No errors for the Genuino 101 now and the scanner prints out the code needed for the Feather nRF52 bridge sketch so that it will connect to the 101. The scanner will check every device it finds in range. The Red led flashes once for each device found with Nordic UART Service (TX Notify).
Step 6: Connecting to a BLE Peripheral
LED Indications
The Feather nRF52 has two leds, Blue and Red. When the board is connected to a BLE device, the Blue Led is constant, otherwise it is flashing. The scanner does not stay connected to the device so its Blue led is usually flashing. The Red led flashes once for each Nordic UART Service (TX Notify) device. When running as a scanner the Red Led counts the number of Nordic UART Service (TX Notify) devices found. When running as a bridge, the Red Led flashes once when connected to the code BLE address.
Connecting to a BLE Peripheral
Now that the scanner sketch has provided the details for the BLE device you want to connect to, you can copy the output into the central_bleuart_bridge.ino sketch. Near the top of that sketch you will find the following code.
// These next three lines of code come from the central_bleuart_scanner.ino output
const char *BLE_NAME = "GENUINO 101-FC8F";
const char *BLE_ADDRESS = "98:4F:EE:0C:FC:8F";
const uint8_t BLE_ADDRESS_TYPE = 0;
Replace the three lines of code with the output from the scanner for the BLE device you want to bridge to. Then program the Feather nRF52 with the edited central_bleuart_bridge.ino sketch.
Testing the central_bleuart_bridge sketch
Before plug the HUZZAH ESP8266 back into the Feather nRF52, test the connection to your BLE device. Leaving the Feather nRF52 connected to you Arduino IDE, open the Tools → Serial Monitor at 9600 baud and then check your target BLE device is powered up.
When the connection is made to your BLE device, as described above, the Blue led will light up constantly and the Red led will flash once every 10sec or so. This lets you not the connection has been make and being maintained.
Then in the Serial Monitor you can enter the commands you expect to send to your BLE device and watch it operate and monitor any response it send back. If all is well you can power down and plugin the HUZZAH ESP8266 module.
Step 7: Connecting to the BLE Device Via WiFi – Putting It All Together
Once you have programmed and tested the HUZZAH ESP8266 module with its bridge code (Wifi_Bridge.ino) configured for your network ssid and password and IP AND programmed and tested the Feather nRF52 with its bridge code (central_bleuart_bridge.ino) configured with the BLE device's address and type, then you can plug them together and plug in a USB supply to the Feather module to power them both up.
The HUZZAH module Red led should light up solid as it connects to your local network router and the Feather Blue led should light up solid as it connects to your BLE device and the Feather Red led should flash once every 10 sec or so to indicate just on BLE device is connected.
The open your telnet program and connect to the HUZZAH's IP and port. The HUZZAH Red led should flash slowly to indicate your program has connected and you should be send commands to your BLE device via WiFi and watch the BLE device operate and see any response in your telnet window. Remember if your BLE device does not send any data back for 60sec, the HUZZAH code will time out the connection and disconnect and the HUZZAH Red led will go solid again.
Step 8: Help – It Does Not Work
First do the testing steps detailed above, Testing the HUZZAH ESP2866 and Testing the central_bleuart_bridge sketch.
Scanning Problems
If the scanner cannot find you BLE device it is either too far away or not advertising or already connected to something. Try moving the scanner closer and power cycling the device and turning off all other near by mobile devices that may have a connection to the BLE device.
If you get a scanner output like this.
Connected to 98:4F:EE:0C:FC:8F Looking for Nordic UART service ... None found OR Not discoverable!
!!!! Could Not Identify a Nordic UART Service !!!!
!!!! If you were expecting a Nordic UART Service, move the Scanner closer to the BLE device !!!!
It may be that you are close enough to the BLE device to detect it but not close enough to successfully do a Service discovery to find the Nordic UART service. Try moving closer to the BLE device.
As a last resort you can use the free Nordic nRF Connect app for Android or iOS. That app has better range and sensitivity. It will tell you if the BLE device has a Nordic UART Service. However you will still need to get the scanner to run and connect and identify the Nordic UART Serivice before you attempt to use the Wifi2BLE bridge as it uses code similar to the scanner.
If you want to dig deeper you can use the Adafruit Bluefruit LE Sniffer module to see what is in the air.
BLE Connection Problems
If you managed to scan the BLE device then the most likely causes of connection problems are
I) the BLE device is too far away,
II) something else has already connected to the BLE device
WiFi Connection Problems
If the HUZZAH ESP8266 Red led does not light up solid then it does not have a connection to your network via your router. Check the ssid and password you have code into Wifi_Bridge.ino and use the Fing app Android or iOS to check that the IP you allocated is not already being used. Try powering down all your computers and devices and power cycling the router (leave it off for 20sec) and then scan with Fing again.
Finally if the connection keeps dropping out, remember the connection timeout setting in the Wifi_Bridge.ino code.
Step 9: Extensions and Conclusion
The project presented here is the simplest version. There are a number of possible extensions such as:-
The Feather nRF52 is capable of connecting to up to 4 BLE devices at once so you could modify the code to add more valid addresses and control up to 4 devices from the one WiFi connection. See the example code that comes with the Feather nRF52 board install.
You could punch a hole in your router to provide access to the BLE device from anywhere on the internet. See Connecting DIY IoT devices to the Internet with pfodApp.
You could use the code from Cheap/Simple Wifi Shield to allow you to configure the WiFi network parameters for the HUZZAH via a web page, without reprogramming. You would need to add a configuration push button to the circuit.
You could add a configuration push button (using the same button as above) that makes the Feather nRF52 scan for BLE devices with a Nordic UART (TX Notify) and then save the connection details for the one with the strongest signal. To save the result, you will need to use the nffs library that comes with the Adafruit nRF52 board install.
You could add a web page to the HUZZAH ESP8266 code to provide a custom interface for your BLE device. You could also use pfodDesignerV3 and pfodApp add a custom interface with no changes to this project at all.
Conclusion
This simple project provide WiFi access to any Bluetooth Low Energy (BLE) module that implements Nordic's UART with TX Notify. The Wifi2BLE bridge is completely transparent. It just passes the WiFi data to the BLE device and passes the BLE device's data back to the WiFi connection.
The simple construction and the detailed testing instructions makes this a suitable project for beginners who want to access their BLE device from their computer or from outside the BLE's t normal range.
This project will become more important once the BLE device software catches up with the new Bluetooth V5 Mesh specification. Then Wifi2BLE will provide internet access to your whole house automation.